feat: roles SPA Users, Profile, API keys, Activity
This commit is contained in:
1 parent
3db490a615
commit
34dffe222f
98 files changed
+4957
-434
No files matched your search
@@ -25,7 +25,7 @@ func InviteToModel(i invite.Invite) models.Invite {
|
||||
HashedCode: i.HashedCode,
|
||||
ExpiresAt: i.ExpiresAt,
|
||||
InvitedBy: i.InvitedBy,
|
||||
RoleID: DomainToNullableID(i.RoleID),
|
||||
RoleID: ZeroAsNullID(i.RoleID),
|
||||
AcceptedAt: i.AcceptedAt,
|
||||
CreatedAt: i.CreatedAt,
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func nullableIDToDomain(value *uint32) uint32 {
|
||||
return *value
|
||||
}
|
||||
|
||||
func DomainToNullableID(value uint32) *uint32 {
|
||||
func ZeroAsNullID(value uint32) *uint32 {
|
||||
if value == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -43,6 +43,7 @@ func UserToDomain(m models.User) user.User {
|
||||
DisplayName: m.DisplayName,
|
||||
IsVerified: m.IsVerified,
|
||||
RoleID: nullableIDToDomain(m.RoleID),
|
||||
RoleAssignedByID: nullableIDToDomain(m.RoleAssignedByID),
|
||||
VerificationCode: nullableStringToDomain(m.VerificationCode),
|
||||
VerificationExpiration: m.VerificationExpiration,
|
||||
PasswordResetExpiration: m.PasswordResetExpiration,
|
||||
@@ -60,7 +61,8 @@ func UserToModel(a user.User) models.User {
|
||||
Email: a.Email,
|
||||
DisplayName: a.DisplayName,
|
||||
IsVerified: a.IsVerified,
|
||||
RoleID: DomainToNullableID(a.RoleID),
|
||||
RoleID: ZeroAsNullID(a.RoleID),
|
||||
RoleAssignedByID: ZeroAsNullID(a.RoleAssignedByID),
|
||||
VerificationCode: DomainToNullableString(a.VerificationCode),
|
||||
VerificationExpiration: a.VerificationExpiration,
|
||||
PasswordResetExpiration: a.PasswordResetExpiration,
|
||||
|
||||
@@ -13,6 +13,7 @@ type User struct {
|
||||
DisplayName string `gorm:"not null;default:'';size:255"`
|
||||
IsVerified bool `gorm:"default=false;not null"`
|
||||
RoleID *uint32 `gorm:"index"`
|
||||
RoleAssignedByID *uint32
|
||||
VerificationCode *string `gorm:"unique;size:255"`
|
||||
VerificationExpiration time.Time
|
||||
PasswordResetExpiration time.Time
|
||||
|
||||
@@ -36,7 +36,7 @@ func (u *UserRepository) Create(user domainUser.User) (domainUser.User, error) {
|
||||
PasswordResetCode: mappers.DomainToNullableString(user.PasswordResetCode),
|
||||
Email: user.Email,
|
||||
IsVerified: user.IsVerified,
|
||||
RoleID: mappers.DomainToNullableID(user.RoleID),
|
||||
RoleID: mappers.ZeroAsNullID(user.RoleID),
|
||||
VerificationCode: mappers.DomainToNullableString(user.VerificationCode),
|
||||
VerificationExpiration: user.VerificationExpiration,
|
||||
}
|
||||
@@ -220,7 +220,7 @@ func (u *UserRepository) UpdateDisplayName(userId uint32, displayName string) (d
|
||||
return mappers.UserToDomain(localUser), nil
|
||||
}
|
||||
|
||||
func (u *UserRepository) UpdateRole(id uint32, from uint32, to uint32) (domainUser.User, error) {
|
||||
func (u *UserRepository) UpdateRole(id uint32, from uint32, to uint32, assignedBy uint32) (domainUser.User, error) {
|
||||
err := u.db.Transaction(func(tx *gorm.DB) error {
|
||||
query := tx.Model(&entity.User{}).Where("id = ? AND EXISTS (SELECT 1 FROM roles WHERE id = ?)", id, to)
|
||||
if from == 0 {
|
||||
@@ -228,7 +228,7 @@ func (u *UserRepository) UpdateRole(id uint32, from uint32, to uint32) (domainUs
|
||||
} else {
|
||||
query = query.Where("role_id = ?", from)
|
||||
}
|
||||
result := query.Update("role_id", to)
|
||||
result := query.Updates(map[string]any{"role_id": to, "role_assigned_by_id": mappers.ZeroAsNullID(assignedBy)})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -266,14 +266,14 @@ func (u *UserRepository) TransferOwnership(from uint32, to uint32, ownerRoleID u
|
||||
return domainUser.ErrInvalidTransfer
|
||||
}
|
||||
return u.db.Transaction(func(tx *gorm.DB) error {
|
||||
demoted := tx.Model(&entity.User{}).Where("id = ? AND role_id = ?", from, ownerRoleID).Update("role_id", formerRoleID)
|
||||
demoted := tx.Model(&entity.User{}).Where("id = ? AND role_id = ?", from, ownerRoleID).Updates(map[string]any{"role_id": formerRoleID, "role_assigned_by_id": nil})
|
||||
if demoted.Error != nil {
|
||||
return demoted.Error
|
||||
}
|
||||
if demoted.RowsAffected == 0 {
|
||||
return domainUser.ErrRoleChanged
|
||||
}
|
||||
promoted := tx.Model(&entity.User{}).Where("id = ? AND is_verified = ?", to, true).Update("role_id", ownerRoleID)
|
||||
promoted := tx.Model(&entity.User{}).Where("id = ? AND is_verified = ?", to, true).Updates(map[string]any{"role_id": ownerRoleID, "role_assigned_by_id": from})
|
||||
if promoted.Error != nil {
|
||||
return promoted.Error
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user