mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
feat: Implement list roles & enforce authorize examples (#1273)
This commit is contained in:
@@ -245,6 +245,38 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
|
||||
return tmp, nil
|
||||
}
|
||||
|
||||
func (q *fakeQuerier) GetAllUserRoles(_ context.Context, userID uuid.UUID) (database.GetAllUserRolesRow, error) {
|
||||
q.mutex.RLock()
|
||||
defer q.mutex.RUnlock()
|
||||
|
||||
var user *database.User
|
||||
roles := make([]string, 0)
|
||||
for _, u := range q.users {
|
||||
if u.ID == userID {
|
||||
u := u
|
||||
roles = append(roles, u.RBACRoles...)
|
||||
user = &u
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, mem := range q.organizationMembers {
|
||||
if mem.UserID == userID {
|
||||
roles = append(roles, mem.Roles...)
|
||||
}
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
return database.GetAllUserRolesRow{}, sql.ErrNoRows
|
||||
}
|
||||
|
||||
return database.GetAllUserRolesRow{
|
||||
ID: userID,
|
||||
Username: user.Username,
|
||||
Roles: roles,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *fakeQuerier) GetWorkspacesByTemplateID(_ context.Context, arg database.GetWorkspacesByTemplateIDParams) ([]database.Workspace, error) {
|
||||
q.mutex.RLock()
|
||||
defer q.mutex.RUnlock()
|
||||
|
||||
@@ -21,6 +21,7 @@ type querier interface {
|
||||
DeleteGitSSHKey(ctx context.Context, userID uuid.UUID) error
|
||||
DeleteParameterValueByID(ctx context.Context, id uuid.UUID) error
|
||||
GetAPIKeyByID(ctx context.Context, id string) (APIKey, error)
|
||||
GetAllUserRoles(ctx context.Context, userID uuid.UUID) (GetAllUserRolesRow, error)
|
||||
// GetAuditLogsBefore retrieves `limit` number of audit logs before the provided
|
||||
// ID.
|
||||
GetAuditLogsBefore(ctx context.Context, arg GetAuditLogsBeforeParams) ([]AuditLog, error)
|
||||
|
||||
@@ -2014,6 +2014,31 @@ func (q *sqlQuerier) UpdateTemplateVersionByID(ctx context.Context, arg UpdateTe
|
||||
return err
|
||||
}
|
||||
|
||||
const getAllUserRoles = `-- name: GetAllUserRoles :one
|
||||
SELECT
|
||||
-- username is returned just to help for logging purposes
|
||||
id, username, array_cat(users.rbac_roles, organization_members.roles) :: text[] AS roles
|
||||
FROM
|
||||
users
|
||||
LEFT JOIN organization_members
|
||||
ON id = user_id
|
||||
WHERE
|
||||
id = $1
|
||||
`
|
||||
|
||||
type GetAllUserRolesRow struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Username string `db:"username" json:"username"`
|
||||
Roles []string `db:"roles" json:"roles"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetAllUserRoles(ctx context.Context, userID uuid.UUID) (GetAllUserRolesRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getAllUserRoles, userID)
|
||||
var i GetAllUserRolesRow
|
||||
err := row.Scan(&i.ID, &i.Username, pq.Array(&i.Roles))
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByEmailOrUsername = `-- name: GetUserByEmailOrUsername :one
|
||||
SELECT
|
||||
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles
|
||||
|
||||
@@ -122,3 +122,15 @@ SET
|
||||
updated_at = $3
|
||||
WHERE
|
||||
id = $1 RETURNING *;
|
||||
|
||||
|
||||
-- name: GetAllUserRoles :one
|
||||
SELECT
|
||||
-- username is returned just to help for logging purposes
|
||||
id, username, array_cat(users.rbac_roles, organization_members.roles) :: text[] AS roles
|
||||
FROM
|
||||
users
|
||||
LEFT JOIN organization_members
|
||||
ON id = user_id
|
||||
WHERE
|
||||
id = @user_id;
|
||||
Reference in New Issue
Block a user