ch some
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user