2021-12-14 07:19:02 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-10-19 10:19:10 +00:00
|
|
|
"sync"
|
2021-12-14 07:19:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-12-14 07:19:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Memberships struct {
|
|
|
|
SearchResponse
|
|
|
|
Memberships []*Membership
|
|
|
|
}
|
|
|
|
|
|
|
|
type Membership struct {
|
|
|
|
UserID string
|
2023-10-19 10:19:10 +00:00
|
|
|
Roles database.TextArray[string]
|
2021-12-14 07:19:02 +00:00
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
ResourceOwner string
|
|
|
|
|
|
|
|
Org *OrgMembership
|
|
|
|
IAM *IAMMembership
|
|
|
|
Project *ProjectMembership
|
|
|
|
ProjectGrant *ProjectGrantMembership
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgMembership struct {
|
|
|
|
OrgID string
|
2021-12-16 13:25:38 +00:00
|
|
|
Name string
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IAMMembership struct {
|
|
|
|
IAMID string
|
2021-12-16 13:25:38 +00:00
|
|
|
Name string
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectMembership struct {
|
|
|
|
ProjectID string
|
2021-12-16 13:25:38 +00:00
|
|
|
Name string
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGrantMembership struct {
|
2022-07-27 07:55:44 +00:00
|
|
|
ProjectID string
|
|
|
|
ProjectName string
|
|
|
|
GrantID string
|
|
|
|
GrantedOrgID string
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MembershipSearchQuery struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipUserIDQuery(userID string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipUserID.setTable(membershipAlias), userID, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipOrgIDQuery(value string) (SearchQuery, error) {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
return NewTextQuery(OrgMemberOrgID, value, TextEquals)
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:00:24 +00:00
|
|
|
func NewMembershipResourceOwnersSearchQuery(ids ...string) (SearchQuery, error) {
|
|
|
|
list := make([]interface{}, len(ids))
|
|
|
|
for i, value := range ids {
|
|
|
|
list[i] = value
|
|
|
|
}
|
|
|
|
return NewListQuery(membershipResourceOwner, list, ListIn)
|
|
|
|
}
|
|
|
|
|
2022-07-27 07:55:44 +00:00
|
|
|
func NewMembershipGrantedOrgIDSearchQuery(id string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(ProjectGrantColumnGrantedOrgID, id, TextEquals)
|
|
|
|
}
|
|
|
|
|
2021-12-14 07:19:02 +00:00
|
|
|
func NewMembershipProjectIDQuery(value string) (SearchQuery, error) {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
return NewTextQuery(ProjectMemberProjectID, value, TextEquals)
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipProjectGrantIDQuery(value string) (SearchQuery, error) {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
return NewTextQuery(ProjectGrantMemberGrantID, value, TextEquals)
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipIsIAMQuery() (SearchQuery, error) {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
return NewNotNullQuery(InstanceMemberIAMID)
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *MembershipSearchQuery) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
|
|
|
query = q.SearchRequest.toQuery(query)
|
|
|
|
for _, q := range q.Queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func (q *Queries) Memberships(ctx context.Context, queries *MembershipSearchQuery, shouldTrigger bool) (memberships *Memberships, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
if shouldTrigger {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(4)
|
|
|
|
go func() {
|
|
|
|
spanCtx, triggerSpan := tracing.NewNamedSpan(ctx, "TriggerOrgMemberProjection")
|
|
|
|
_, _ = projection.OrgMemberProjection.Trigger(spanCtx, handler.WithAwaitRunning())
|
|
|
|
triggerSpan.End()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
spanCtx, triggerSpan := tracing.NewNamedSpan(ctx, "TriggerInstanceMemberProjection")
|
|
|
|
_, _ = projection.InstanceMemberProjection.Trigger(spanCtx, handler.WithAwaitRunning())
|
|
|
|
triggerSpan.End()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
spanCtx, triggerSpan := tracing.NewNamedSpan(ctx, "TriggerProjectMemberProjection")
|
|
|
|
_, _ = projection.ProjectMemberProjection.Trigger(spanCtx, handler.WithAwaitRunning())
|
|
|
|
triggerSpan.End()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
spanCtx, triggerSpan := tracing.NewNamedSpan(ctx, "TriggerProjectGrantMemberProjection")
|
|
|
|
_, _ = projection.ProjectGrantMemberProjection.Trigger(spanCtx, handler.WithAwaitRunning())
|
|
|
|
triggerSpan.End()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
query, queryArgs, scan := prepareMembershipsQuery(ctx, q.client, queries)
|
2022-11-30 16:01:17 +00:00
|
|
|
eq := sq.Eq{membershipInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()}
|
|
|
|
stmt, args, err := queries.toQuery(query).Where(eq).ToSql()
|
2021-12-14 07:19:02 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-T84X9", "Errors.Query.InvalidRequest")
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
2024-09-06 09:19:19 +00:00
|
|
|
latestState, err := q.latestState(ctx, orgMemberTable, instanceMemberTable, projectMemberTable, projectGrantMemberTable)
|
2021-12-14 07:19:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
queryArgs = append(queryArgs, args...)
|
2021-12-14 07:19:02 +00:00
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
memberships, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, stmt, queryArgs...)
|
2021-12-14 07:19:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-06 09:19:19 +00:00
|
|
|
memberships.State = latestState
|
2021-12-14 07:19:02 +00:00
|
|
|
return memberships, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
//membershipAlias is a hack to satisfy checks in the queries
|
|
|
|
membershipAlias = table{
|
2023-10-19 10:19:10 +00:00
|
|
|
name: "members",
|
2022-10-27 06:08:36 +00:00
|
|
|
instanceIDCol: projection.MemberInstanceID,
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
membershipUserID = Column{
|
|
|
|
name: projection.MemberUserIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipRoles = Column{
|
|
|
|
name: projection.MemberRolesCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipCreationDate = Column{
|
|
|
|
name: projection.MemberCreationDate,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipChangeDate = Column{
|
|
|
|
name: projection.MemberChangeDate,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipSequence = Column{
|
|
|
|
name: projection.MemberSequence,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipResourceOwner = Column{
|
|
|
|
name: projection.MemberResourceOwner,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
membershipInstanceID = Column{
|
|
|
|
name: projection.MemberInstanceID,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2021-12-14 07:19:02 +00:00
|
|
|
membershipOrgID = Column{
|
|
|
|
name: projection.OrgMemberOrgIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipIAMID = Column{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: projection.InstanceMemberIAMIDCol,
|
2021-12-14 07:19:02 +00:00
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipProjectID = Column{
|
|
|
|
name: projection.ProjectMemberProjectIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipGrantID = Column{
|
|
|
|
name: projection.ProjectGrantMemberGrantIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2022-07-27 07:55:44 +00:00
|
|
|
membershipGrantGrantedOrgID = Column{
|
|
|
|
name: projection.ProjectGrantColumnGrantedOrgID,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2021-12-14 07:19:02 +00:00
|
|
|
)
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func getMembershipFromQuery(queries *MembershipSearchQuery) (string, []interface{}) {
|
|
|
|
orgMembers, orgMembersArgs := prepareOrgMember(queries)
|
|
|
|
iamMembers, iamMembersArgs := prepareIAMMember(queries)
|
|
|
|
projectMembers, projectMembersArgs := prepareProjectMember(queries)
|
|
|
|
projectGrantMembers, projectGrantMembersArgs := prepareProjectGrantMember(queries)
|
2022-11-30 16:01:17 +00:00
|
|
|
args := make([]interface{}, 0)
|
|
|
|
args = append(append(append(append(args, orgMembersArgs...), iamMembersArgs...), projectMembersArgs...), projectGrantMembersArgs...)
|
|
|
|
|
|
|
|
return "(" +
|
|
|
|
orgMembers +
|
|
|
|
" UNION ALL " +
|
|
|
|
iamMembers +
|
|
|
|
" UNION ALL " +
|
|
|
|
projectMembers +
|
|
|
|
" UNION ALL " +
|
|
|
|
projectGrantMembers +
|
|
|
|
") AS " + membershipAlias.identifier(),
|
|
|
|
args
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func prepareMembershipsQuery(ctx context.Context, db prepareDatabase, queries *MembershipSearchQuery) (sq.SelectBuilder, []interface{}, func(*sql.Rows) (*Memberships, error)) {
|
|
|
|
query, args := getMembershipFromQuery(queries)
|
2021-12-14 07:19:02 +00:00
|
|
|
return sq.Select(
|
|
|
|
membershipUserID.identifier(),
|
|
|
|
membershipRoles.identifier(),
|
|
|
|
membershipCreationDate.identifier(),
|
|
|
|
membershipChangeDate.identifier(),
|
|
|
|
membershipSequence.identifier(),
|
|
|
|
membershipResourceOwner.identifier(),
|
|
|
|
membershipOrgID.identifier(),
|
|
|
|
membershipIAMID.identifier(),
|
|
|
|
membershipProjectID.identifier(),
|
|
|
|
membershipGrantID.identifier(),
|
2022-07-27 07:55:44 +00:00
|
|
|
ProjectGrantColumnGrantedOrgID.identifier(),
|
2021-12-16 13:25:38 +00:00
|
|
|
ProjectColumnName.identifier(),
|
|
|
|
OrgColumnName.identifier(),
|
2023-10-19 10:19:10 +00:00
|
|
|
InstanceColumnName.identifier(),
|
2021-12-14 07:19:02 +00:00
|
|
|
countColumn.identifier(),
|
2022-11-30 16:01:17 +00:00
|
|
|
).From(query).
|
2021-12-16 13:25:38 +00:00
|
|
|
LeftJoin(join(ProjectColumnID, membershipProjectID)).
|
|
|
|
LeftJoin(join(OrgColumnID, membershipOrgID)).
|
2023-10-19 10:19:10 +00:00
|
|
|
LeftJoin(join(ProjectGrantColumnGrantID, membershipGrantID)).
|
|
|
|
LeftJoin(join(InstanceColumnID, membershipInstanceID) + db.Timetravel(call.Took(ctx))).
|
2021-12-14 07:19:02 +00:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-11-30 16:01:17 +00:00
|
|
|
args,
|
2021-12-14 07:19:02 +00:00
|
|
|
func(rows *sql.Rows) (*Memberships, error) {
|
|
|
|
memberships := make([]*Membership, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
|
|
|
|
var (
|
2022-07-27 07:55:44 +00:00
|
|
|
membership = new(Membership)
|
|
|
|
orgID = sql.NullString{}
|
2023-10-19 10:19:10 +00:00
|
|
|
instanceID = sql.NullString{}
|
2022-07-27 07:55:44 +00:00
|
|
|
projectID = sql.NullString{}
|
|
|
|
grantID = sql.NullString{}
|
|
|
|
grantedOrgID = sql.NullString{}
|
|
|
|
projectName = sql.NullString{}
|
|
|
|
orgName = sql.NullString{}
|
2023-10-19 10:19:10 +00:00
|
|
|
instanceName = sql.NullString{}
|
2021-12-14 07:19:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&membership.UserID,
|
2022-08-31 07:52:43 +00:00
|
|
|
&membership.Roles,
|
2021-12-14 07:19:02 +00:00
|
|
|
&membership.CreationDate,
|
|
|
|
&membership.ChangeDate,
|
|
|
|
&membership.Sequence,
|
|
|
|
&membership.ResourceOwner,
|
|
|
|
&orgID,
|
2023-10-19 10:19:10 +00:00
|
|
|
&instanceID,
|
2021-12-14 07:19:02 +00:00
|
|
|
&projectID,
|
|
|
|
&grantID,
|
2022-07-27 07:55:44 +00:00
|
|
|
&grantedOrgID,
|
2021-12-16 13:25:38 +00:00
|
|
|
&projectName,
|
|
|
|
&orgName,
|
2023-10-19 10:19:10 +00:00
|
|
|
&instanceName,
|
2021-12-14 07:19:02 +00:00
|
|
|
&count,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if orgID.Valid {
|
|
|
|
membership.Org = &OrgMembership{
|
|
|
|
OrgID: orgID.String,
|
2021-12-16 13:25:38 +00:00
|
|
|
Name: orgName.String,
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
} else if instanceID.Valid {
|
2021-12-14 07:19:02 +00:00
|
|
|
membership.IAM = &IAMMembership{
|
2023-10-19 10:19:10 +00:00
|
|
|
IAMID: instanceID.String,
|
|
|
|
Name: instanceName.String,
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
2022-07-27 07:55:44 +00:00
|
|
|
} else if projectID.Valid && grantID.Valid && grantedOrgID.Valid {
|
2021-12-14 07:19:02 +00:00
|
|
|
membership.ProjectGrant = &ProjectGrantMembership{
|
2022-07-27 07:55:44 +00:00
|
|
|
ProjectID: projectID.String,
|
|
|
|
ProjectName: projectName.String,
|
|
|
|
GrantID: grantID.String,
|
|
|
|
GrantedOrgID: grantedOrgID.String,
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
} else if projectID.Valid {
|
|
|
|
membership.Project = &ProjectMembership{
|
|
|
|
ProjectID: projectID.String,
|
2021-12-16 13:25:38 +00:00
|
|
|
Name: projectName.String,
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships = append(memberships, membership)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-N34NV", "Errors.Query.CloseRows")
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Memberships{
|
|
|
|
Memberships: memberships,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func prepareOrgMember(query *MembershipSearchQuery) (string, []interface{}) {
|
2022-11-30 16:01:17 +00:00
|
|
|
builder := sq.Select(
|
2021-12-14 07:19:02 +00:00
|
|
|
OrgMemberUserID.identifier(),
|
|
|
|
OrgMemberRoles.identifier(),
|
|
|
|
OrgMemberCreationDate.identifier(),
|
|
|
|
OrgMemberChangeDate.identifier(),
|
|
|
|
OrgMemberSequence.identifier(),
|
|
|
|
OrgMemberResourceOwner.identifier(),
|
2022-03-29 09:53:19 +00:00
|
|
|
OrgMemberInstanceID.identifier(),
|
2021-12-14 07:19:02 +00:00
|
|
|
OrgMemberOrgID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipIAMID.name,
|
|
|
|
"NULL::TEXT AS "+membershipProjectID.name,
|
|
|
|
"NULL::TEXT AS "+membershipGrantID.name,
|
2022-11-30 16:01:17 +00:00
|
|
|
).From(orgMemberTable.identifier())
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
for _, q := range query.Queries {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
if q.Col().table.name == membershipAlias.name || q.Col().table.name == orgMemberTable.name {
|
2023-10-19 10:19:10 +00:00
|
|
|
builder = q.toQuery(builder)
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return builder.MustSql()
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func prepareIAMMember(query *MembershipSearchQuery) (string, []interface{}) {
|
2022-11-30 16:01:17 +00:00
|
|
|
builder := sq.Select(
|
2022-03-24 16:21:34 +00:00
|
|
|
InstanceMemberUserID.identifier(),
|
|
|
|
InstanceMemberRoles.identifier(),
|
|
|
|
InstanceMemberCreationDate.identifier(),
|
|
|
|
InstanceMemberChangeDate.identifier(),
|
|
|
|
InstanceMemberSequence.identifier(),
|
|
|
|
InstanceMemberResourceOwner.identifier(),
|
2022-03-29 09:53:19 +00:00
|
|
|
InstanceMemberInstanceID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipOrgID.name,
|
2022-03-24 16:21:34 +00:00
|
|
|
InstanceMemberIAMID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipProjectID.name,
|
|
|
|
"NULL::TEXT AS "+membershipGrantID.name,
|
2022-11-30 16:01:17 +00:00
|
|
|
).From(instanceMemberTable.identifier())
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
for _, q := range query.Queries {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
if q.Col().table.name == membershipAlias.name || q.Col().table.name == instanceMemberTable.name {
|
2023-10-19 10:19:10 +00:00
|
|
|
builder = q.toQuery(builder)
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return builder.MustSql()
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func prepareProjectMember(query *MembershipSearchQuery) (string, []interface{}) {
|
2022-11-30 16:01:17 +00:00
|
|
|
builder := sq.Select(
|
2021-12-14 07:19:02 +00:00
|
|
|
ProjectMemberUserID.identifier(),
|
|
|
|
ProjectMemberRoles.identifier(),
|
|
|
|
ProjectMemberCreationDate.identifier(),
|
|
|
|
ProjectMemberChangeDate.identifier(),
|
|
|
|
ProjectMemberSequence.identifier(),
|
|
|
|
ProjectMemberResourceOwner.identifier(),
|
2022-03-29 09:53:19 +00:00
|
|
|
ProjectMemberInstanceID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipOrgID.name,
|
|
|
|
"NULL::TEXT AS "+membershipIAMID.name,
|
2021-12-14 07:19:02 +00:00
|
|
|
ProjectMemberProjectID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipGrantID.name,
|
2022-11-30 16:01:17 +00:00
|
|
|
).From(projectMemberTable.identifier())
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
for _, q := range query.Queries {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
if q.Col().table.name == membershipAlias.name || q.Col().table.name == projectMemberTable.name {
|
2023-10-19 10:19:10 +00:00
|
|
|
builder = q.toQuery(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
return builder.MustSql()
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:21:08 +00:00
|
|
|
func prepareProjectGrantMember(query *MembershipSearchQuery) (string, []interface{}) {
|
2022-11-30 16:01:17 +00:00
|
|
|
builder := sq.Select(
|
2021-12-14 07:19:02 +00:00
|
|
|
ProjectGrantMemberUserID.identifier(),
|
|
|
|
ProjectGrantMemberRoles.identifier(),
|
|
|
|
ProjectGrantMemberCreationDate.identifier(),
|
|
|
|
ProjectGrantMemberChangeDate.identifier(),
|
|
|
|
ProjectGrantMemberSequence.identifier(),
|
|
|
|
ProjectGrantMemberResourceOwner.identifier(),
|
2022-03-29 09:53:19 +00:00
|
|
|
ProjectGrantMemberInstanceID.identifier(),
|
2022-08-31 07:52:43 +00:00
|
|
|
"NULL::TEXT AS "+membershipOrgID.name,
|
|
|
|
"NULL::TEXT AS "+membershipIAMID.name,
|
2021-12-14 07:19:02 +00:00
|
|
|
ProjectGrantMemberProjectID.identifier(),
|
|
|
|
ProjectGrantMemberGrantID.identifier(),
|
2022-11-30 16:01:17 +00:00
|
|
|
).From(projectGrantMemberTable.identifier())
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
for _, q := range query.Queries {
|
fix: remove non-existent colums in getMembershipFromQuery queries (#7869)
In issue #7841 @mahmoodfathy commented an issue when the API call for
Listing My ZITADEL Manager Roles is called with any kind of query
(orgQuery, projectQuery, projectGrantQuery...). A column XXXXXX does not
exist (SQLSTATE 42703) error is thrown.
The issue was focused in getMembershipFromQuery where filtering queries
functions are called: prepareOrgMember, prepareIAMMember,
prepareProjectMember and prepareProjectGrantMember
Those functions allow queries for columns that are not members of the
table to be queried so I've added a conditional clause to avoid using
the queries that cannot be called.
For example, for prepareOrgMember, member.id, member.project_id and
member.grant_id columns are not added to the filter queries
```
for _, q := range query.Queries {
if q.Col().table.name == membershipAlias.name &&
!slices.Contains([]string{membershipIAMID.name, membershipProjectID.name, membershipGrantID.name}, q.Col().name) {
builder = q.toQuery(builder)
}
}
return builder.MustSql()
```
Here I show one screenshot where the error "column XXXXXX does not exist
(SQLSTATE 42703)" is no longer thrown using an orgQuery.
![image](https://github.com/zitadel/zitadel/assets/30386061/77621e69-71df-42de-b3c5-fa9b4dbf1b89)
Should close #7841
### Definition of Ready
- [X] I am happy with the code
- [X] Short description of the feature/issue is added in the pr
description
- [X] PR is linked to the corresponding user story
- [X] Acceptance criteria are met
- [ ] All open todos and follow ups are defined in a new ticket and
justified
- [ ] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [X] No debug or dead code
- [X] My code has no repetitions
- [X] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [ ] Documentation/examples are up-to-date
- [ ] All non-functional requirements are met
- [X] Functionality of the acceptance criteria is checked manually on
the dev system.
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2024-07-02 12:08:28 +00:00
|
|
|
if q.Col().table.name == membershipAlias.name || q.Col().table.name == projectMemberTable.name || q.Col().table.name == projectGrantMemberTable.name {
|
2023-10-19 10:19:10 +00:00
|
|
|
builder = q.toQuery(builder)
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return builder.MustSql()
|
2021-12-14 07:19:02 +00:00
|
|
|
}
|