changed role permission
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
guwandyk committed 2026-09-26 16:34:28 +05:00
1 parent f0ecf23879
commit 323e131216
74 files changed
+2915 -4713

No files matched your search

+52 -99
View File
@@ -30,17 +30,10 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) user.Reposi
const userColumns = `u.id, u.email, u.name, u.first_name, u.last_name, u.username, u.phone, u.avatar, u.active, u.created_at, u.updated_at, r.id, r.code`
const userReturningColumns = `id, email, name, first_name, last_name, username, phone, avatar, role, active, created_at, updated_at`
const userReturningColumns = `id, email, name, first_name, last_name, username, phone, avatar, active, created_at, updated_at`
const userFromClause = ` FROM users u
LEFT JOIN LATERAL (
SELECT ru.role_id
FROM role_user ru
WHERE ru.user_id = u.id
ORDER BY ru.id
LIMIT 1
) ru ON true
LEFT JOIN roles r ON r.id = ru.role_id `
LEFT JOIN roles r ON r.id = u.role_id AND r.deleted_at IS NULL `
const userBranchesJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
@@ -57,19 +50,13 @@ const userBranchesJSON = `COALESCE((
) b
), '[]'::json)`
// userPermissionsJSON lists the effective permissions: every one for an
// is_super role, else the role's role_permissions.
const userPermissionsJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
'id', p.id,
'code', COALESCE(p.code, ''),
'name', p.name,
'guardName', p.guard_name,
'module', COALESCE(p.module, '')
) ORDER BY p.module, p.id)
SELECT json_agg(jsonb_build_object('code', p.code, 'name', p.name) ORDER BY p.code)
FROM permissions p
WHERE p.deleted_at IS NULL
AND EXISTS (
SELECT 1 FROM permission_role pr
WHERE pr.role_id = r.id AND pr.permission_id = p.id AND pr.deleted_at IS NULL
WHERE r.is_super OR EXISTS (
SELECT 1 FROM role_permissions rp WHERE rp.role_id = r.id AND rp.permission_code = p.code
)
), '[]'::json)`
@@ -225,7 +212,7 @@ func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error
return item, err
}
func (r *repository) Create(ctx context.Context, dto user.CreateUserDTO) (user.UserDTO, error) {
func (r *repository) Create(ctx context.Context, dto user.CreateUserDTO, roleID int64) (user.UserDTO, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(dto.Password), bcrypt.DefaultCost)
if err != nil {
return user.UserDTO{}, err
@@ -236,78 +223,56 @@ func (r *repository) Create(ctx context.Context, dto user.CreateUserDTO) (user.U
active = *dto.Active
}
role := strings.TrimSpace(dto.Role)
if role == "" {
role = "user"
}
tx, err := r.client.Begin(ctx)
if err != nil {
return user.UserDTO{}, err
}
defer tx.Rollback(ctx)
query := fmt.Sprintf(`
INSERT INTO users (email, password, name, first_name, last_name, username, phone, avatar, role, active)
INSERT INTO users (email, password, name, first_name, last_name, username, phone, avatar, role_id, active)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING %s`, userReturningColumns)
item, err := scanUser(tx.QueryRow(ctx, query,
dto.Email, hashedPassword, dto.Name, dto.FirstName, dto.LastName, dto.Username, dto.Phone, dto.Avatar, role, active,
item, err := scanUser(r.client.QueryRow(ctx, query,
dto.Email, hashedPassword, dto.Name, dto.FirstName, dto.LastName, dto.Username, dto.Phone, dto.Avatar, roleID, active,
))
if err != nil {
return user.UserDTO{}, err
}
if err := assignRoleByCode(ctx, tx, item.Id, role); err != nil {
return user.UserDTO{}, err
}
item.RoleId = roleIDByCode(ctx, tx, role)
if err := tx.Commit(ctx); err != nil {
return user.UserDTO{}, err
}
return item, nil
return r.withRole(ctx, item, roleID)
}
func (r *repository) Update(ctx context.Context, id int64, dto user.UpdateUserDTO) (user.UserDTO, error) {
role := strings.TrimSpace(dto.Role)
if role == "" {
role = "user"
}
tx, err := r.client.Begin(ctx)
if err != nil {
return user.UserDTO{}, err
}
defer tx.Rollback(ctx)
func (r *repository) Update(ctx context.Context, id int64, dto user.UpdateUserDTO, roleID int64) (user.UserDTO, error) {
query := fmt.Sprintf(`
UPDATE users SET
email = $1, name = $2, first_name = $3, last_name = $4, username = $5, phone = $6, avatar = $7,
role = $8, active = $9,
role_id = $8, active = $9,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $10 AND deleted_at IS NULL
RETURNING %s`, userReturningColumns)
item, err := scanUser(tx.QueryRow(ctx, query,
dto.Email, dto.Name, dto.FirstName, dto.LastName, dto.Username, dto.Phone, dto.Avatar, role, dto.Active, id,
item, err := scanUser(r.client.QueryRow(ctx, query,
dto.Email, dto.Name, dto.FirstName, dto.LastName, dto.Username, dto.Phone, dto.Avatar, roleID, dto.Active, id,
))
if errors.Is(err, pgx.ErrNoRows) {
return user.UserDTO{}, err
}
if err != nil {
return user.UserDTO{}, err
}
return r.withRole(ctx, item, roleID)
}
if err := replaceRoleByCode(ctx, tx, item.Id, role); err != nil {
return user.UserDTO{}, err
}
item.RoleId = roleIDByCode(ctx, tx, role)
if err := tx.Commit(ctx); err != nil {
func (r *repository) SetRole(ctx context.Context, id, roleID int64) error {
tag, err := r.client.Exec(ctx, `
UPDATE users SET role_id = $1, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $2 AND deleted_at IS NULL`, roleID, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return pgx.ErrNoRows
}
return nil
}
func (r *repository) withRole(ctx context.Context, item user.UserDTO, roleID int64) (user.UserDTO, error) {
if err := r.client.QueryRow(ctx, `SELECT code FROM roles WHERE id = $1`, roleID).Scan(&item.Role); err != nil {
return user.UserDTO{}, err
}
item.RoleId = &roleID
return item, nil
}
@@ -315,48 +280,36 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
return genericrepo.Delete(ctx, r.client, "users", id)
}
func (r *repository) RoleGuardName(ctx context.Context, code string) (string, error) {
var guardName string
err := r.client.QueryRow(ctx, `SELECT guard_name FROM roles WHERE code = $1`, code).Scan(&guardName)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
return guardName, err
func (r *repository) RoleByCode(ctx context.Context, code string) (*rbac.RoleInfo, error) {
return rbac.ScanRoleInfo(r.client.QueryRow(ctx,
rbac.RoleInfoSelect+`WHERE r.code = $1 AND r.deleted_at IS NULL`, strings.TrimSpace(code)))
}
func assignRoleByCode(ctx context.Context, tx pgx.Tx, userID int64, roleCode string) error {
tag, err := tx.Exec(ctx, `
INSERT INTO role_user (role_id, user_id)
SELECT r.id, $1 FROM roles r WHERE r.code = $2`, userID, roleCode)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return fmt.Errorf("role %q not found", roleCode)
}
return nil
func (r *repository) RoleByID(ctx context.Context, id int64) (*rbac.RoleInfo, error) {
return rbac.ScanRoleInfo(r.client.QueryRow(ctx,
rbac.RoleInfoSelect+`WHERE r.id = $1 AND r.deleted_at IS NULL`, id))
}
func replaceRoleByCode(ctx context.Context, tx pgx.Tx, userID int64, roleCode string) error {
if _, err := tx.Exec(ctx, `UPDATE role_user SET deleted_at = now() WHERE user_id = $1 AND deleted_at IS NULL`, userID); err != nil {
return err
}
return assignRoleByCode(ctx, tx, userID, roleCode)
func (r *repository) RoleOfUser(ctx context.Context, userID int64) (*rbac.RoleInfo, error) {
return rbac.ScanRoleInfo(r.client.QueryRow(ctx,
rbac.RoleInfoSelect+`JOIN users u ON u.role_id = r.id
WHERE u.id = $1 AND u.deleted_at IS NULL AND r.deleted_at IS NULL`, userID))
}
func roleIDByCode(ctx context.Context, tx pgx.Tx, roleCode string) *int64 {
var id int64
if err := tx.QueryRow(ctx, `SELECT id FROM roles WHERE code = $1`, roleCode).Scan(&id); err != nil {
return nil
}
return &id
func (r *repository) CountSuperUsers(ctx context.Context) (int64, error) {
var count int64
err := r.client.QueryRow(ctx, `
SELECT COUNT(*) FROM users u
JOIN roles r ON r.id = u.role_id AND r.deleted_at IS NULL
WHERE r.is_super AND u.deleted_at IS NULL`).Scan(&count)
return count, err
}
func scanUser(row genericrepo.Scanner) (user.UserDTO, error) {
var item user.UserDTO
if err := row.Scan(
&item.Id, &item.Email, &item.Name, &item.FirstName, &item.LastName, &item.Username, &item.Phone, &item.Avatar,
&item.Role, &item.Active, &item.CreatedAt, &item.UpdatedAt,
&item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil {
return user.UserDTO{}, err
}