Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: mark users that have been made explicit admins via config #1189

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apiclient/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func (u Role) HasRole(role Role) bool {

type User struct {
Metadata
Username string `json:"username,omitempty"`
Role Role `json:"role,omitempty"`
Email string `json:"email,omitempty"`
IconURL string `json:"iconURL,omitempty"`
Timezone string `json:"timezone,omitempty"`
Username string `json:"username,omitempty"`
Role Role `json:"role,omitempty"`
ExplicitAdmin bool `json:"explicitAdmin,omitempty"`
Email string `json:"email,omitempty"`
IconURL string `json:"iconURL,omitempty"`
Timezone string `json:"timezone,omitempty"`
}

type UserList List[User]
2 changes: 1 addition & 1 deletion pkg/api/handlers/authorizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a *AuthorizationHandler) ListAgentAuthorizations(req api.Context) error {
if err != nil {
log.Errorf("failed to get user for authorization list %s: %v", grant.Spec.UserID, err)
} else if user != nil {
auth.User = types2.ConvertUser(user)
auth.User = types2.ConvertUser(user, a.userClient.IsExplicitAdmin(user.Email))
}

result.Items = append(result.Items, auth)
Expand Down
5 changes: 5 additions & 0 deletions pkg/gateway/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (c *Client) Close() error {
return c.db.Close()
}

func (c *Client) IsExplicitAdmin(email string) bool {
_, ok := c.adminEmails[email]
return ok
}

func firstValue(m map[string][]string, key string) string {
values := m[key]
if len(values) == 0 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/gateway/client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ type AlreadyExistsError struct {
func (e *AlreadyExistsError) Error() string {
return e.name + " already exists"
}

type ExplicitAdminError struct {
email string
}

func (e *ExplicitAdminError) Error() string {
return e.email + " has been marked explicitly as an admin"
}
6 changes: 5 additions & 1 deletion pkg/gateway/client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ func (c *Client) UpdateUser(ctx context.Context, actingUserIsAdmin bool, updated

// Only admins can change user roles.
if actingUserIsAdmin {
// If the role is being changed from admin to non-admin, then ensure that this isn't the last admin.
if updatedUser.Role > 0 && existingUser.Role.HasRole(types2.RoleAdmin) && !updatedUser.Role.HasRole(types2.RoleAdmin) {
// If this user has been explicitly marked as an admin, then don't allow changing the role.
if c.IsExplicitAdmin(existingUser.Email) {
return &ExplicitAdminError{email: existingUser.Email}
}
// If the role is being changed from admin to non-admin, then ensure that this isn't the last admin.
var adminCount int64
if err := tx.Model(new(types.User)).Where("role = ?", types2.RoleAdmin).Count(&adminCount).Error; err != nil {
return err
Expand Down
12 changes: 7 additions & 5 deletions pkg/gateway/server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *Server) getCurrentUser(apiContext api.Context) error {
pkgLog.Warnf("failed to update profile icon for user %s: %v", user.Username, err)
}

return apiContext.Write(types.ConvertUser(user))
return apiContext.Write(types.ConvertUser(user, s.client.IsExplicitAdmin(user.Email)))
}

func (s *Server) getUsers(apiContext api.Context) error {
Expand All @@ -40,7 +40,7 @@ func (s *Server) getUsers(apiContext api.Context) error {

items := make([]types2.User, 0, len(users))
for _, user := range users {
items = append(items, *types.ConvertUser(&user))
items = append(items, *types.ConvertUser(&user, s.client.IsExplicitAdmin(user.Email)))
}

return apiContext.Write(types2.UserList{Items: items})
Expand All @@ -60,7 +60,7 @@ func (s *Server) getUser(apiContext api.Context) error {
return fmt.Errorf("failed to get user: %v", err)
}

return apiContext.Write(types.ConvertUser(user))
return apiContext.Write(types.ConvertUser(user, s.client.IsExplicitAdmin(user.Email)))
}

func (s *Server) updateUser(apiContext api.Context) error {
Expand Down Expand Up @@ -94,13 +94,15 @@ func (s *Server) updateUser(apiContext api.Context) error {
status = http.StatusNotFound
} else if lae := (*client.LastAdminError)(nil); errors.As(err, &lae) {
status = http.StatusBadRequest
} else if ea := (*client.ExplicitAdminError)(nil); errors.As(err, &ea) {
status = http.StatusBadRequest
} else if ae := (*client.AlreadyExistsError)(nil); errors.As(err, &ae) {
status = http.StatusConflict
}
return types2.NewErrHttp(status, fmt.Sprintf("failed to update user: %v", err))
}

return apiContext.Write(types.ConvertUser(existingUser))
return apiContext.Write(types.ConvertUser(existingUser, s.client.IsExplicitAdmin(existingUser.Email)))
}

func (s *Server) deleteUser(apiContext api.Context) error {
Expand All @@ -120,5 +122,5 @@ func (s *Server) deleteUser(apiContext api.Context) error {
return types2.NewErrHttp(status, fmt.Sprintf("failed to delete user: %v", err))
}

return apiContext.Write(types.ConvertUser(existingUser))
return apiContext.Write(types.ConvertUser(existingUser, s.client.IsExplicitAdmin(existingUser.Email)))
}
13 changes: 7 additions & 6 deletions pkg/gateway/types/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type User struct {
Timezone string `json:"timezone"`
}

func ConvertUser(u *User) *types2.User {
func ConvertUser(u *User, roleFixed bool) *types2.User {
if u == nil {
return nil
}
Expand All @@ -30,11 +30,12 @@ func ConvertUser(u *User) *types2.User {
ID: fmt.Sprint(u.ID),
Created: *types2.NewTime(u.CreatedAt),
},
Username: u.Username,
Email: u.Email,
Role: u.Role,
IconURL: u.IconURL,
Timezone: u.Timezone,
Username: u.Username,
Email: u.Email,
Role: u.Role,
ExplicitAdmin: roleFixed,
IconURL: u.IconURL,
Timezone: u.Timezone,
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/storage/openapi/generated/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading