ch some
Some checks failed
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:
2026-09-04 19:33:26 +05:00
parent 28509eb4ca
commit d1a6fcaadc
102 changed files with 2089 additions and 1957 deletions

View File

@@ -11,6 +11,7 @@ import (
"online-back/internal/identity/auth"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"online-back/pkg/repository/genericrepo"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
@@ -71,15 +72,29 @@ const profilePermissionsJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
'id', p.id,
'code', COALESCE(p.code, ''),
'name', p.name
) ORDER BY p.id)
FROM permission_role pr
JOIN permissions p ON p.id = pr.permission_id
WHERE pr.role_id = r.id
'name', p.name,
'guardName', p.guard_name,
'module', COALESCE(p.module, '')
) ORDER BY p.module, p.id)
FROM permissions p
WHERE NOT EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'deny'
)
AND (
EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'allow'
)
OR EXISTS (
SELECT 1 FROM permission_role pr
WHERE pr.role_id = r.id AND pr.permission_id = p.id
)
)
), '[]'::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, ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1`
r.id, r.code, r.name, r.guard_name, ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1`
func (r *repository) Login(ctx context.Context, dto auth.LoginDTO) (auth.UserCredentials, error) {
phone := strings.TrimSpace(dto.Phone)
@@ -127,14 +142,10 @@ func (r *repository) UpdateProfile(ctx context.Context, id int, dto auth.UpdateP
return r.GetProfile(ctx, id)
}
type scannable interface {
Scan(dest ...interface{}) error
}
func scanProfile(row scannable) (auth.UserProfileDTO, error) {
func scanProfile(row genericrepo.Scanner) (auth.UserProfileDTO, error) {
var profile auth.UserProfileDTO
var roleID sql.NullInt64
var roleCode, roleName sql.NullString
var roleCode, roleName, roleGuard sql.NullString
var branchesJSON []byte
var permissionsJSON []byte
@@ -149,6 +160,7 @@ func scanProfile(row scannable) (auth.UserProfileDTO, error) {
&roleID,
&roleCode,
&roleName,
&roleGuard,
&branchesJSON,
&permissionsJSON,
)
@@ -158,9 +170,10 @@ func scanProfile(row scannable) (auth.UserProfileDTO, error) {
if roleID.Valid {
profile.Role = &auth.ProfileRoleDTO{
Id: roleID.Int64,
Code: roleCode.String,
Name: roleName.String,
Id: roleID.Int64,
Code: roleCode.String,
Name: roleName.String,
GuardName: roleGuard.String,
}
}
@@ -177,6 +190,7 @@ func scanProfile(row scannable) (auth.UserProfileDTO, error) {
return auth.UserProfileDTO{}, err
}
}
profile.Modules = auth.GroupProfileModules(profile.Permissions)
return profile, nil
}

View File

@@ -19,9 +19,10 @@ type LocalizedText struct {
}
type ProfileRoleDTO struct {
Id int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
Id int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
GuardName string `json:"guardName"`
}
type ProfileBranchDTO struct {
@@ -31,9 +32,16 @@ type ProfileBranchDTO struct {
}
type ProfilePermissionDTO struct {
Id int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
Id int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
GuardName string `json:"guardName"`
Module string `json:"module"`
}
type ProfileModuleDTO struct {
Module string `json:"module"`
Permissions []ProfilePermissionDTO `json:"permissions"`
}
type UserProfileDTO struct {
@@ -45,6 +53,7 @@ type UserProfileDTO struct {
Avatar string `json:"avatar"`
Role *ProfileRoleDTO `json:"role"`
Branches []ProfileBranchDTO `json:"branches"`
Modules []ProfileModuleDTO `json:"modules"`
Permissions []ProfilePermissionDTO `json:"permissions"`
CreatedAt time.Time `json:"createdAt"`
}

View File

@@ -242,7 +242,7 @@ func (h *handler) refreshToken(c *gin.Context) {
// getProfile godoc
// @Summary Get profile
// @Description Returns the authenticated user's profile with role, branches, and permissions
// @Description Returns the authenticated user's profile with role, branches, and permissions grouped by saha (module)
// @Tags auth
// @Produce json
// @Security BearerAuth

View File

@@ -0,0 +1,37 @@
package auth
import (
"sort"
"online-back/internal/identity/permission"
)
func GroupProfileModules(perms []ProfilePermissionDTO) []ProfileModuleDTO {
buckets := make(map[string][]ProfilePermissionDTO)
seen := make(map[string]struct{})
for _, p := range perms {
module := p.Module
buckets[module] = append(buckets[module], p)
seen[module] = struct{}{}
}
modules := make([]ProfileModuleDTO, 0, len(seen))
for _, name := range permission.ModuleOrder() {
ps, ok := buckets[name]
if !ok {
continue
}
modules = append(modules, ProfileModuleDTO{Module: name, Permissions: ps})
delete(seen, name)
}
var extra []string
for name := range seen {
extra = append(extra, name)
}
sort.Strings(extra)
for _, name := range extra {
modules = append(modules, ProfileModuleDTO{Module: name, Permissions: buckets[name]})
}
return modules
}