changed role permission
This commit is contained in:
1 parent
f0ecf23879
commit
323e131216
74 files changed
+2915
-4713
No files matched your search
@@ -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)]
|
||||
})
|
||||
}
|
||||
@@ -19,10 +19,11 @@ type LocalizedText struct {
|
||||
}
|
||||
|
||||
type ProfileRoleDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
GuardName string `json:"guardName"`
|
||||
Id int64 `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
IsSuper bool `json:"isSuper"`
|
||||
}
|
||||
|
||||
type ProfileBranchDTO struct {
|
||||
@@ -31,17 +32,11 @@ type ProfileBranchDTO struct {
|
||||
Name LocalizedText `json:"name"`
|
||||
}
|
||||
|
||||
// ProfilePermissionDTO is one module the caller can open; the front-end
|
||||
// builds its menu from this list.
|
||||
type ProfilePermissionDTO struct {
|
||||
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"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type UserProfileDTO struct {
|
||||
@@ -53,7 +48,6 @@ 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"`
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/sms"
|
||||
|
||||
@@ -35,16 +36,16 @@ func NewHandler(logger *logging.Logger, repository Repository, sender sms.Sender
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
router.POST(loginURL, h.login)
|
||||
router.POST(tokenURL, h.refreshToken)
|
||||
router.POST(registerURL, h.register)
|
||||
router.POST(verifyURL, h.verify)
|
||||
router.POST(forgotPasswordURL, h.forgotPassword)
|
||||
router.POST(resetPasswordURL, h.resetPassword)
|
||||
rbac.Group(router, loginURL, rbac.Public()).POST("", h.login)
|
||||
rbac.Group(router, tokenURL, rbac.Public()).POST("", h.refreshToken)
|
||||
rbac.Group(router, registerURL, rbac.Public()).POST("", h.register)
|
||||
rbac.Group(router, verifyURL, rbac.Public()).POST("", h.verify)
|
||||
rbac.Group(router, forgotPasswordURL, rbac.Public()).POST("", h.forgotPassword)
|
||||
rbac.Group(router, resetPasswordURL, rbac.Public()).POST("", h.resetPassword)
|
||||
|
||||
protected := router.Group("", appresult.JwtTokenCheck())
|
||||
protected.GET(profileURL, h.getProfile)
|
||||
protected.PUT(profileURL, h.updateProfile)
|
||||
profile := rbac.Group(router, profileURL, rbac.Authenticated())
|
||||
profile.GET("", h.getProfile)
|
||||
profile.PUT("", h.updateProfile)
|
||||
}
|
||||
|
||||
// login godoc
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user