Rename identity profile on membership profile
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -72,7 +72,7 @@ const (
|
||||
RightsRequestEntityType uint16 = 48
|
||||
StateOfApplicabilityEntityType uint16 = 49
|
||||
StateOfApplicabilityControlEntityType uint16 = 50
|
||||
IdentityProfileEntityType uint16 = 51
|
||||
MembershipProfileEntityType uint16 = 51
|
||||
)
|
||||
|
||||
type EntityInfo struct {
|
||||
@@ -285,9 +285,9 @@ var entityRegistry = map[uint16]EntityInfo{
|
||||
Model: "StateOfApplicabilityControl",
|
||||
Table: "states_of_applicability_controls",
|
||||
},
|
||||
IdentityProfileEntityType: {
|
||||
Model: "IdentityProfile",
|
||||
Table: "iam_identity_profiles",
|
||||
MembershipProfileEntityType: {
|
||||
Model: "MembershipProfile",
|
||||
Table: "iam_membership_profiles",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ type (
|
||||
Identity struct {
|
||||
ID gid.GID `db:"id"`
|
||||
EmailAddress mail.Addr `db:"email_address"`
|
||||
FullName string `db:"full_name"`
|
||||
HashedPassword []byte `db:"hashed_password"`
|
||||
EmailAddressVerified bool `db:"email_address_verified"`
|
||||
SAMLSubject *string `db:"saml_subject"`
|
||||
@@ -63,8 +64,10 @@ func (i *Identities) LoadByOrganizationID(
|
||||
SELECT
|
||||
id,
|
||||
email_address,
|
||||
full_name,
|
||||
hashed_password,
|
||||
email_address_verified,
|
||||
saml_subject,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -139,6 +142,7 @@ func (i *Identity) LoadByEmail(
|
||||
SELECT
|
||||
id,
|
||||
email_address,
|
||||
full_name,
|
||||
hashed_password,
|
||||
email_address_verified,
|
||||
saml_subject,
|
||||
@@ -182,6 +186,7 @@ func (i *Identity) LoadByID(
|
||||
SELECT
|
||||
id,
|
||||
email_address,
|
||||
full_name,
|
||||
hashed_password,
|
||||
email_address_verified,
|
||||
saml_subject,
|
||||
@@ -221,10 +226,11 @@ func (i *Identity) Insert(
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
identities (id, email_address, hashed_password, email_address_verified, saml_subject, created_at, updated_at)
|
||||
identities (id, email_address, full_name, hashed_password, email_address_verified, saml_subject, created_at, updated_at)
|
||||
VALUES (
|
||||
@identity_id,
|
||||
@email_address,
|
||||
@full_name,
|
||||
@hashed_password,
|
||||
@email_address_verified,
|
||||
@saml_subject,
|
||||
@@ -236,6 +242,7 @@ VALUES (
|
||||
args := pgx.StrictNamedArgs{
|
||||
"identity_id": i.ID,
|
||||
"email_address": i.EmailAddress,
|
||||
"full_name": i.FullName,
|
||||
"hashed_password": i.HashedPassword,
|
||||
"saml_subject": i.SAMLSubject,
|
||||
"created_at": i.CreatedAt,
|
||||
@@ -265,6 +272,7 @@ UPDATE
|
||||
identities
|
||||
SET
|
||||
email_address = @email_address,
|
||||
full_name = @full_name,
|
||||
email_address_verified = @email_address_verified,
|
||||
saml_subject = @saml_subject,
|
||||
hashed_password = @hashed_password,
|
||||
@@ -276,6 +284,7 @@ WHERE
|
||||
args := pgx.StrictNamedArgs{
|
||||
"identity_id": i.ID,
|
||||
"email_address": i.EmailAddress,
|
||||
"full_name": i.FullName,
|
||||
"email_address_verified": i.EmailAddressVerified,
|
||||
"saml_subject": i.SAMLSubject,
|
||||
"updated_at": i.UpdatedAt,
|
||||
@@ -304,6 +313,7 @@ func (i *Identity) LoadBySAMLSubject(
|
||||
SELECT
|
||||
id,
|
||||
email_address,
|
||||
full_name,
|
||||
hashed_password,
|
||||
email_address_verified,
|
||||
saml_subject,
|
||||
|
||||
@@ -81,13 +81,14 @@ SELECT
|
||||
mbr.identity_id,
|
||||
mbr.organization_id,
|
||||
mbr.role,
|
||||
i.fullname AS full_name,
|
||||
COALESCE(mp.full_name, i.full_name, '') as full_name,
|
||||
i.email_address,
|
||||
mbr.created_at,
|
||||
mbr.updated_at
|
||||
FROM
|
||||
mbr
|
||||
JOIN identities i ON mbr.identity_id = i.id
|
||||
LEFT JOIN iam_membership_profiles mp ON mp.membership_id = mbr.id
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
@@ -189,7 +190,7 @@ SELECT
|
||||
mbr.identity_id,
|
||||
mbr.organization_id,
|
||||
mbr.role,
|
||||
COALESCE(mp.full_name, dp.full_name, '') as full_name,
|
||||
COALESCE(mp.full_name, i.full_name, '') as full_name,
|
||||
i.email_address,
|
||||
mbr.created_at,
|
||||
mbr.updated_at
|
||||
@@ -198,9 +199,7 @@ FROM
|
||||
JOIN
|
||||
identities i ON mbr.identity_id = i.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles mp ON mp.membership_id = mbr.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles dp ON dp.identity_id = mbr.identity_id AND dp.membership_id IS NULL
|
||||
iam_membership_profiles mp ON mp.membership_id = mbr.id
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
@@ -333,7 +332,7 @@ SELECT
|
||||
mbr.identity_id,
|
||||
mbr.organization_id,
|
||||
mbr.role,
|
||||
COALESCE(mp.full_name, dp.full_name, '') as full_name,
|
||||
COALESCE(mp.full_name, i.full_name, '') as full_name,
|
||||
i.email_address,
|
||||
mbr.created_at,
|
||||
mbr.updated_at
|
||||
@@ -342,9 +341,7 @@ FROM
|
||||
JOIN
|
||||
identities i ON mbr.identity_id = i.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles mp ON mp.membership_id = mbr.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles dp ON dp.identity_id = mbr.identity_id AND dp.membership_id IS NULL
|
||||
iam_membership_profiles mp ON mp.membership_id = mbr.id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
@@ -463,7 +460,7 @@ SELECT
|
||||
mbr.identity_id,
|
||||
mbr.organization_id,
|
||||
mbr.role,
|
||||
COALESCE(mp.full_name, dp.full_name, '') as full_name,
|
||||
COALESCE(mp.full_name, i.full_name, '') as full_name,
|
||||
i.email_address,
|
||||
mbr.created_at,
|
||||
mbr.updated_at
|
||||
@@ -472,19 +469,18 @@ FROM
|
||||
JOIN
|
||||
identities i ON mbr.identity_id = i.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles mp ON mp.membership_id = mbr.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles dp ON dp.identity_id = mbr.identity_id AND dp.membership_id IS NULL
|
||||
ORDER BY
|
||||
mbr.created_at DESC
|
||||
iam_membership_profiles mp ON mp.membership_id = mbr.id
|
||||
WHERE
|
||||
%s
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
query = fmt.Sprintf(query, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"identity_id": identityID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, query, args)
|
||||
if err != nil {
|
||||
@@ -508,18 +504,24 @@ func (m *Memberships) LoadByOrganizationID(
|
||||
cursor *page.Cursor[MembershipOrderField],
|
||||
) error {
|
||||
query := `
|
||||
WITH mbr AS (
|
||||
WITH membership_with_profile AS (
|
||||
SELECT
|
||||
id,
|
||||
identity_id,
|
||||
organization_id,
|
||||
role,
|
||||
created_at,
|
||||
updated_at
|
||||
m.id,
|
||||
m.identity_id,
|
||||
m.organization_id,
|
||||
m.role,
|
||||
COALESCE(mp.full_name, i.full_name, '') AS full_name,
|
||||
i.email_address,
|
||||
m.created_at,
|
||||
m.updated_at
|
||||
FROM
|
||||
iam_memberships
|
||||
iam_memberships m
|
||||
JOIN
|
||||
identities i ON m.identity_id = i.id
|
||||
LEFT JOIN
|
||||
iam_membership_profiles mp ON mp.membership_id = m.id
|
||||
WHERE
|
||||
organization_id = @organization_id
|
||||
m.organization_id = @organization_id
|
||||
AND %s
|
||||
)
|
||||
SELECT
|
||||
@@ -531,26 +533,10 @@ SELECT
|
||||
email_address,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM (
|
||||
SELECT
|
||||
mbr.id,
|
||||
mbr.identity_id,
|
||||
mbr.organization_id,
|
||||
mbr.role,
|
||||
COALESCE(mp.full_name, dp.full_name, '') as full_name,
|
||||
i.email_address,
|
||||
mbr.created_at,
|
||||
mbr.updated_at
|
||||
FROM
|
||||
mbr
|
||||
JOIN
|
||||
identities i ON mbr.identity_id = i.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles mp ON mp.membership_id = mbr.id
|
||||
LEFT JOIN
|
||||
iam_identity_profiles dp ON dp.identity_id = mbr.identity_id AND dp.membership_id IS NULL
|
||||
) AS membership_with_identity
|
||||
WHERE %s
|
||||
FROM
|
||||
membership_with_profile
|
||||
WHERE
|
||||
%s
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
@@ -27,68 +27,16 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
IdentityProfile struct {
|
||||
MembershipProfile struct {
|
||||
ID gid.GID `db:"id"`
|
||||
IdentityID gid.GID `db:"identity_id"`
|
||||
MembershipID *gid.GID `db:"membership_id"`
|
||||
MembershipID gid.GID `db:"membership_id"`
|
||||
FullName string `db:"full_name"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
IdentityProfiles []*IdentityProfile
|
||||
)
|
||||
|
||||
func (p *IdentityProfile) IsDefault() bool {
|
||||
return p.MembershipID == nil
|
||||
}
|
||||
|
||||
// LoadDefaultByIdentityID loads the default profile for an identity (where membership_id is NULL)
|
||||
func (p *IdentityProfile) LoadDefaultByIdentityID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
identityID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
identity_id,
|
||||
membership_id,
|
||||
full_name,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_identity_profiles
|
||||
WHERE
|
||||
tenant_id IS NULL
|
||||
AND identity_id = @identity_id
|
||||
AND membership_id IS NULL
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"identity_id": identityID}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query default identity profile: %w", err)
|
||||
}
|
||||
|
||||
profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[IdentityProfile])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect default identity profile: %w", err)
|
||||
}
|
||||
|
||||
*p = profile
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadByMembershipID loads the profile for a specific membership
|
||||
func (p *IdentityProfile) LoadByMembershipID(
|
||||
func (p *MembershipProfile) LoadByMembershipID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
@@ -97,13 +45,12 @@ func (p *IdentityProfile) LoadByMembershipID(
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
identity_id,
|
||||
membership_id,
|
||||
full_name,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_identity_profiles
|
||||
iam_membership_profiles
|
||||
WHERE
|
||||
%s
|
||||
AND membership_id = @membership_id
|
||||
@@ -120,7 +67,7 @@ LIMIT 1;
|
||||
return fmt.Errorf("cannot query identity profile: %w", err)
|
||||
}
|
||||
|
||||
profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[IdentityProfile])
|
||||
profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MembershipProfile])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
@@ -134,7 +81,7 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IdentityProfile) LoadByID(
|
||||
func (p *MembershipProfile) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
@@ -143,13 +90,12 @@ func (p *IdentityProfile) LoadByID(
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
identity_id,
|
||||
membership_id,
|
||||
full_name,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_identity_profiles
|
||||
iam_membership_profiles
|
||||
WHERE
|
||||
%s
|
||||
AND id = @profile_id
|
||||
@@ -166,7 +112,7 @@ LIMIT 1;
|
||||
return fmt.Errorf("cannot query identity profile: %w", err)
|
||||
}
|
||||
|
||||
profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[IdentityProfile])
|
||||
profile, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[MembershipProfile])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
@@ -180,16 +126,15 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IdentityProfile) Insert(
|
||||
func (p *MembershipProfile) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
iam_identity_profiles (
|
||||
iam_membership_profiles (
|
||||
tenant_id,
|
||||
id,
|
||||
identity_id,
|
||||
membership_id,
|
||||
full_name,
|
||||
created_at,
|
||||
@@ -198,7 +143,6 @@ INSERT INTO
|
||||
VALUES (
|
||||
@tenant_id,
|
||||
@id,
|
||||
@identity_id,
|
||||
@membership_id,
|
||||
@full_name,
|
||||
@created_at,
|
||||
@@ -209,7 +153,6 @@ VALUES (
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": p.ID.TenantID().String(),
|
||||
"id": p.ID,
|
||||
"identity_id": p.IdentityID,
|
||||
"membership_id": p.MembershipID,
|
||||
"full_name": p.FullName,
|
||||
"created_at": p.CreatedAt,
|
||||
@@ -224,14 +167,14 @@ VALUES (
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IdentityProfile) Update(
|
||||
func (p *MembershipProfile) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE
|
||||
iam_identity_profiles
|
||||
iam_membership_profiles
|
||||
SET
|
||||
full_name = @full_name,
|
||||
updated_at = @updated_at
|
||||
@@ -261,7 +204,7 @@ WHERE
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IdentityProfile) Delete(
|
||||
func (p *MembershipProfile) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
@@ -269,7 +212,7 @@ func (p *IdentityProfile) Delete(
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM
|
||||
iam_identity_profiles
|
||||
iam_membership_profiles
|
||||
WHERE
|
||||
id = @profile_id
|
||||
AND %s
|
||||
15
pkg/coredata/migrations/20251222T102632Z.sql
Normal file
15
pkg/coredata/migrations/20251222T102632Z.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
ALTER TABLE identities ADD COLUMN full_name TEXT NOT NULL DEFAULT '';
|
||||
|
||||
UPDATE identities i
|
||||
SET full_name = COALESCE(
|
||||
(SELECT p.full_name FROM iam_identity_profiles p
|
||||
WHERE p.identity_id = i.id AND p.membership_id IS NULL),
|
||||
''
|
||||
);
|
||||
|
||||
DELETE FROM iam_identity_profiles WHERE membership_id IS NULL;
|
||||
DROP INDEX IF EXISTS idx_iam_identity_profiles_default;
|
||||
|
||||
ALTER TABLE iam_identity_profiles DROP COLUMN identity_id;
|
||||
|
||||
ALTER TABLE iam_identity_profiles RENAME TO iam_membership_profiles;
|
||||
Reference in New Issue
Block a user