Signed-off-by: Cole Speelman <crspeelman@gmail.com>
This commit is contained in:
@@ -408,6 +408,17 @@ func (store *Store) OrganizationByID(ctx context.Context, organizationID string)
|
||||
}
|
||||
|
||||
func (store *Store) UpdateOrganization(ctx context.Context, value organizations.Organization, expectedRevision int64, audit organizations.AuditEvent) error {
|
||||
return store.updateOrganization(ctx, value, expectedRevision, "", audit)
|
||||
}
|
||||
|
||||
func (store *Store) UpdateOwnedOrganization(ctx context.Context, value organizations.Organization, expectedRevision int64, ownerRole string, audit organizations.AuditEvent) error {
|
||||
if !safeName(ownerRole) || value.Personal || value.Status != "active" || audit.Action != "organization.update" || audit.ResourceType != "organization" || audit.ResourceID != value.ID {
|
||||
return errors.New("authsqlite: invalid owner-managed organization update")
|
||||
}
|
||||
return store.updateOrganization(ctx, value, expectedRevision, ownerRole, audit)
|
||||
}
|
||||
|
||||
func (store *Store) updateOrganization(ctx context.Context, value organizations.Organization, expectedRevision int64, ownerRole string, audit organizations.AuditEvent) error {
|
||||
if !validOrganization(value) || expectedRevision < 1 || value.Revision != expectedRevision+1 || !validOrganizationAudit(audit, value.ID) {
|
||||
return errors.New("authsqlite: invalid organization update")
|
||||
}
|
||||
@@ -416,6 +427,11 @@ func (store *Store) UpdateOrganization(ctx context.Context, value organizations.
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if ownerRole != "" {
|
||||
if err = lockOrganizationOwner(ctx, tx, value.ID, audit.ActorUserID, ownerRole); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `UPDATE gwf_organizations SET slug=?,name=?,status=?,revision=?,updated_at=? WHERE id=? AND revision=?`, value.Slug, value.Name, value.Status, value.Revision, value.UpdatedAt.Unix(), value.ID, expectedRevision)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -535,6 +551,14 @@ func (store *Store) SetMembershipStatus(ctx context.Context, organizationID, use
|
||||
}
|
||||
|
||||
func (store *Store) ChangeMembershipStatus(ctx context.Context, input organizations.MembershipStatusChange, ownerRole string, audit organizations.AuditEvent) error {
|
||||
return store.changeMembershipStatus(ctx, input, ownerRole, audit, false)
|
||||
}
|
||||
|
||||
func (store *Store) ChangeOwnedMembershipStatus(ctx context.Context, input organizations.MembershipStatusChange, ownerRole string, audit organizations.AuditEvent) error {
|
||||
return store.changeMembershipStatus(ctx, input, ownerRole, audit, true)
|
||||
}
|
||||
|
||||
func (store *Store) changeMembershipStatus(ctx context.Context, input organizations.MembershipStatusChange, ownerRole string, audit organizations.AuditEvent, requireOwner bool) error {
|
||||
if !validMembershipStatusChange(input, ownerRole, audit) {
|
||||
return organizations.ErrMembershipNotFound
|
||||
}
|
||||
@@ -543,7 +567,12 @@ func (store *Store) ChangeMembershipStatus(ctx context.Context, input organizati
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = lockActiveMembershipActor(ctx, tx, input.OrganizationID, input.ActorUserID); err != nil {
|
||||
if requireOwner {
|
||||
err = lockOrganizationOwner(ctx, tx, input.OrganizationID, input.ActorUserID, ownerRole)
|
||||
} else {
|
||||
err = lockActiveMembershipActor(ctx, tx, input.OrganizationID, input.ActorUserID)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := membershipStatus(ctx, tx, input.OrganizationID, input.UserID)
|
||||
@@ -620,6 +649,14 @@ func (store *Store) RemoveMembership(ctx context.Context, organizationID, userID
|
||||
}
|
||||
|
||||
func (store *Store) RemoveMembershipIfCurrent(ctx context.Context, input organizations.MembershipRemoval, ownerRole string, audit organizations.AuditEvent) error {
|
||||
return store.removeMembershipIfCurrent(ctx, input, ownerRole, audit, false)
|
||||
}
|
||||
|
||||
func (store *Store) RemoveOwnedMembershipIfCurrent(ctx context.Context, input organizations.MembershipRemoval, ownerRole string, audit organizations.AuditEvent) error {
|
||||
return store.removeMembershipIfCurrent(ctx, input, ownerRole, audit, true)
|
||||
}
|
||||
|
||||
func (store *Store) removeMembershipIfCurrent(ctx context.Context, input organizations.MembershipRemoval, ownerRole string, audit organizations.AuditEvent, requireOwner bool) error {
|
||||
if !validMembershipRemoval(input, ownerRole, audit) {
|
||||
return organizations.ErrMembershipNotFound
|
||||
}
|
||||
@@ -628,7 +665,12 @@ func (store *Store) RemoveMembershipIfCurrent(ctx context.Context, input organiz
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = lockActiveMembershipActor(ctx, tx, input.OrganizationID, input.ActorUserID); err != nil {
|
||||
if requireOwner {
|
||||
err = lockOrganizationOwner(ctx, tx, input.OrganizationID, input.ActorUserID, ownerRole)
|
||||
} else {
|
||||
err = lockActiveMembershipActor(ctx, tx, input.OrganizationID, input.ActorUserID)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := membershipStatus(ctx, tx, input.OrganizationID, input.UserID)
|
||||
@@ -691,6 +733,27 @@ func lockActiveMembershipActor(ctx context.Context, tx *sql.Tx, organizationID,
|
||||
return nil
|
||||
}
|
||||
|
||||
func lockOrganizationOwner(ctx context.Context, tx *sql.Tx, organizationID, actorUserID, ownerRole string) error {
|
||||
if err := lockActiveMembershipActor(ctx, tx, organizationID, actorUserID); err != nil {
|
||||
return err
|
||||
}
|
||||
var personal bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT personal FROM gwf_organizations WHERE id=?`, organizationID).Scan(&personal); err != nil {
|
||||
return err
|
||||
}
|
||||
if personal {
|
||||
return organizations.ErrPersonalOrganization
|
||||
}
|
||||
owner, err := hasDirectOwnerRole(ctx, tx, organizationID, actorUserID, ownerRole)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owner {
|
||||
return organizations.ErrOwnerAuthority
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func membershipStatus(ctx context.Context, tx *sql.Tx, organizationID, userID string) (string, error) {
|
||||
var status string
|
||||
if err := tx.QueryRowContext(ctx, `SELECT status FROM gwf_organization_memberships WHERE organization_id=? AND user_id=?`, organizationID, userID).Scan(&status); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user