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

+31 -39
View File
@@ -5,10 +5,12 @@ import (
"database/sql"
"encoding/json"
"errors"
"sort"
"strings"
"unicode"
"online-back/internal/identity/auth"
"online-back/internal/identity/rbac/perm"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"online-back/pkg/repository/genericrepo"
@@ -46,14 +48,7 @@ const (
)
const profileFromClause = ` FROM users u
LEFT JOIN LATERAL (
SELECT ru.role_id
FROM role_user ru
WHERE ru.user_id = u.id AND ru.deleted_at IS NULL
ORDER BY ru.id
LIMIT 1
) ru ON true
LEFT JOIN roles r ON r.id = ru.role_id AND r.deleted_at IS NULL`
LEFT JOIN roles r ON r.id = u.role_id AND r.deleted_at IS NULL`
const profileBranchesJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
@@ -70,24 +65,18 @@ const profileBranchesJSON = `COALESCE((
) b
), '[]'::json)`
// profilePermissionsJSON: every permission for an is_super role, else the
// role's own; ordered as rbac/perm.All by the caller.
const profilePermissionsJSON = `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))
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)`
const profileQuery = `SELECT u.id, u.name, u.first_name, u.last_name, u.phone, u.avatar, u.created_at,
r.id, r.code, r.name, r.guard_name, ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1 AND u.deleted_at IS NULL`
r.id, r.code, r.name, r.scope, COALESCE(r.is_super, false), ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1 AND u.deleted_at IS NULL`
func (r *repository) Login(ctx context.Context, dto auth.LoginDTO) (auth.UserCredentials, error) {
phone := strings.TrimSpace(dto.Phone)
@@ -138,7 +127,8 @@ func (r *repository) UpdateProfile(ctx context.Context, id int, dto auth.UpdateP
func scanProfile(row genericrepo.Scanner) (auth.UserProfileDTO, error) {
var profile auth.UserProfileDTO
var roleID sql.NullInt64
var roleCode, roleName, roleGuard sql.NullString
var roleCode, roleName, roleScope sql.NullString
var roleSuper bool
var branchesJSON []byte
var permissionsJSON []byte
@@ -153,7 +143,8 @@ func scanProfile(row genericrepo.Scanner) (auth.UserProfileDTO, error) {
&roleID,
&roleCode,
&roleName,
&roleGuard,
&roleScope,
&roleSuper,
&branchesJSON,
&permissionsJSON,
)
@@ -163,10 +154,11 @@ func scanProfile(row genericrepo.Scanner) (auth.UserProfileDTO, error) {
if roleID.Valid {
profile.Role = &auth.ProfileRoleDTO{
Id: roleID.Int64,
Code: roleCode.String,
Name: roleName.String,
GuardName: roleGuard.String,
Id: roleID.Int64,
Code: roleCode.String,
Name: roleName.String,
Scope: roleScope.String,
IsSuper: roleSuper,
}
}
@@ -183,7 +175,7 @@ func scanProfile(row genericrepo.Scanner) (auth.UserProfileDTO, error) {
return auth.UserProfileDTO{}, err
}
}
profile.Modules = auth.GroupProfileModules(profile.Permissions)
sortByCatalog(profile.Permissions)
return profile, nil
}
@@ -260,8 +252,8 @@ func (r *repository) CompleteRegistration(ctx context.Context, v auth.Verificati
var userID int
err = tx.QueryRow(ctx, `
INSERT INTO users (name, username, phone, password, role, active, email)
VALUES ($1, $2, $3, $4, 'user', true, '')
INSERT INTO users (name, username, phone, password, role_id, active, email)
VALUES ($1, $2, $3, $4, (SELECT id FROM roles WHERE code = 'user' AND deleted_at IS NULL), true, '')
RETURNING id`,
v.Name, v.Username, v.Phone, v.Password,
).Scan(&userID)
@@ -273,16 +265,6 @@ func (r *repository) CompleteRegistration(ctx context.Context, v auth.Verificati
return 0, err
}
tag, err := tx.Exec(ctx, `
INSERT INTO role_user (role_id, user_id)
SELECT r.id, $1 FROM roles r WHERE r.code = 'user'`, userID)
if err != nil {
return 0, err
}
if tag.RowsAffected() == 0 {
return 0, errors.New(`role "user" not found`)
}
if _, err := tx.Exec(ctx, `DELETE FROM registration_verifications WHERE id = $1`, v.ID); err != nil {
return 0, err
}
@@ -342,3 +324,13 @@ func digitString(s string) string {
}
return b.String()
}
func sortByCatalog(perms []auth.ProfilePermissionDTO) {
order := make(map[perm.Code]int, len(perm.All))
for i, d := range perm.All {
order[d.Code] = i
}
sort.SliceStable(perms, func(i, j int) bool {
return order[perm.Code(perms[i].Code)] < order[perm.Code(perms[j].Code)]
})
}