MSP-1756 reimplement

This commit is contained in:
d1slike committed 2023-05-25 12:38:36 +03:00
1 parent 6fcc9e45e3
commit d2f56ca43e
6 files changed
+26 -62

No files matched your search

-1
View File
@@ -72,7 +72,6 @@ func (l Locator) Config(cfg conf.Remote) Config {
tokenService,
sudirService,
auditService,
userRoleRepo,
l.logger,
cfg.AntiBruteforce.DelayLoginRequestInSec,
cfg.AntiBruteforce.MaxInFlightLoginRequests,
+3 -20
View File
@@ -94,24 +94,6 @@ func (r Role) GetRoleByExternalGroup(ctx context.Context, group string) (*entity
}
}
func (r Role) UpsertRoleByName(ctx context.Context, role entity.Role) (int, error) {
sql_metrics.OperationLabelToContext(ctx, "Role.UpsertRoleByName")
query := `
insert into roles (name, permissions, external_group)
values ($1, $2, $3) on conflict (name) do update
set name = excluded.name, external_group = excluded.external_group
returning id
`
id := 0
err := r.db.SelectRow(ctx, &id, query, role.Name, role.Permissions, role.ExternalGroup)
if err != nil {
return 0, errors.WithMessagef(err, "select %s", query)
}
return id, nil
}
func (r Role) All(ctx context.Context) ([]entity.Role, error) {
sql_metrics.OperationLabelToContext(ctx, "Role.All")
@@ -125,8 +107,8 @@ func (r Role) All(ctx context.Context) ([]entity.Role, error) {
func (r Role) InsertRole(ctx context.Context, role entity.Role) (*entity.Role, error) {
q, args, err := query.New().Insert("roles").
Columns("name", "permissions").
Values(role.Name, role.Permissions).
Columns("name", "permissions", "external_group").
Values(role.Name, role.Permissions, role.ExternalGroup).
Suffix("RETURNING *").ToSql()
if err != nil {
return nil, errors.WithMessage(err, "build query")
@@ -145,6 +127,7 @@ func (r Role) Update(ctx context.Context, role entity.Role) (*entity.Role, error
q, args, err := query.New().Update("roles").
Set("name", role.Name).
Set("permissions", role.Permissions).
Set("external_group", role.ExternalGroup).
Where(squirrel.Eq{"id": role.Id}).
Suffix("RETURNING *").ToSql()
if err != nil {
+2 -2
View File
@@ -35,7 +35,7 @@ func (u UserRole) GetRolesByUserIds(ctx context.Context, identity []int) ([]enti
}
}
func (u UserRole) InsertPairs(ctx context.Context, id int, roleIds []int) error {
func (u UserRole) InsertUserRoleLinks(ctx context.Context, id int, roleIds []int) error {
rolesQ := query.New().
Insert("user_roles").
Columns("user_id", "role_id")
@@ -57,7 +57,7 @@ func (u UserRole) InsertPairs(ctx context.Context, id int, roleIds []int) error
return nil
}
func (u UserRole) ForceUpsert(ctx context.Context, id int, roleIds []int) error {
func (u UserRole) UpdateUserRoleLinks(ctx context.Context, id int, roleIds []int) error {
deleteQ, args, err := query.New().
Delete("user_roles").Where(squirrel.Eq{"user_id": id}).ToSql()
if err != nil {
+1 -9
View File
@@ -38,11 +38,6 @@ type tokenService interface {
RevokeAllByUserId(ctx context.Context, userId int64) error
}
type authRoleRepository interface {
GetRolesByUserIds(ctx context.Context, identity []int) ([]entity.UserRole, error)
InsertPairs(ctx context.Context, id int, roleIds []int) error
}
type sudirService interface {
Authenticate(ctx context.Context, authCode string, repo roleRepo) (*entity.SudirUser, error)
}
@@ -53,7 +48,6 @@ type Auth struct {
tokenService tokenService
sudirService sudirService
auditService auditService
authRoleRepository authRoleRepository
logger log.Logger
maxInFlightLoginRequests int
delayLoginRequest time.Duration
@@ -66,7 +60,6 @@ func NewAuth(
tokenService tokenService,
sudirService sudirService,
auditService auditService,
authRoleRepository authRoleRepository,
logger log.Logger,
delayLoginRequestInSec int,
maxInFlightLoginRequests int,
@@ -77,7 +70,6 @@ func NewAuth(
tokenService: tokenService,
sudirService: sudirService,
auditService: auditService,
authRoleRepository: authRoleRepository,
logger: logger,
delayLoginRequest: time.Duration(delayLoginRequestInSec) * time.Second,
maxInFlightLoginRequests: maxInFlightLoginRequests,
@@ -206,7 +198,7 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque
// при создании юзера СУДИР первоначально роли не указаны
if len(userRoles) == 0 {
err = tx.InsertPairs(ctx, int(user.Id), sudirUser.RoleIds)
err = tx.InsertUserRoleLinks(ctx, int(user.Id), sudirUser.RoleIds)
if err != nil {
return errors.WithMessage(err, "insert sudir roles")
}
+15 -25
View File
@@ -21,7 +21,7 @@ type sudirRepo interface {
type roleRepo interface {
GetRoleByExternalGroup(ctx context.Context, group string) (*entity.Role, error)
UpsertRoleByName(ctx context.Context, role entity.Role) (int, error)
InsertRole(ctx context.Context, role entity.Role) (*entity.Role, error)
}
type Sudir struct {
@@ -61,34 +61,24 @@ func (s Sudir) Authenticate(ctx context.Context, authCode string, roleRepo roleR
email = user.Sub
}
isNewRole := false
role, err := roleRepo.GetRoleByExternalGroup(ctx, user.GivenName)
switch {
case errors.Is(err, domain.ErrNotFound):
isNewRole = true
case err != nil:
return nil, errors.WithMessage(err, "")
var role *entity.Role
role, err = roleRepo.GetRoleByExternalGroup(ctx, user.GivenName)
if errors.Is(err, domain.ErrNotFound) {
role, err = roleRepo.InsertRole(ctx, entity.Role{
Name: user.GivenName,
ExternalGroup: user.GivenName, // TODO take group name correctly from user.Group
Permissions: []string{},
})
if err != nil {
return nil, errors.WithMessage(err, "insert role")
}
}
if !isNewRole {
return &entity.SudirUser{
RoleIds: []int{role.Id},
SudirUserId: user.Sub,
FirstName: user.GivenName,
LastName: user.FamilyName,
Email: email,
}, nil
if err != nil {
return nil, errors.WithMessage(err, "get role by external group")
}
roleId, err := roleRepo.UpsertRoleByName(ctx, entity.Role{
Name: user.GivenName,
Permissions: []string{},
ExternalGroup: user.GivenName,
})
if err != nil {
return nil, errors.WithMessage(err, "upsert role")
}
return &entity.SudirUser{
RoleIds: []int{roleId},
RoleIds: []int{role.Id},
SudirUserId: user.Sub,
FirstName: user.GivenName,
LastName: user.FamilyName,
+5 -5
View File
@@ -38,8 +38,8 @@ type TokenRepo interface {
type UserRoleRepo interface {
GetRolesByUserIds(ctx context.Context, identity []int) ([]entity.UserRole, error)
InsertPairs(ctx context.Context, id int, roleIds []int) error
ForceUpsert(ctx context.Context, id int, roleIds []int) error
InsertUserRoleLinks(ctx context.Context, id int, roleIds []int) error
UpdateUserRoleLinks(ctx context.Context, id int, roleIds []int) error
}
type roleRepoUser interface {
@@ -178,7 +178,7 @@ func (u User) CreateUser(ctx context.Context, req domain.CreateUserRequest, admi
}
if len(req.Roles) != 0 {
err = tx.InsertPairs(ctx, id, req.Roles)
err = tx.InsertUserRoleLinks(ctx, id, req.Roles)
if err != nil {
return err
}
@@ -233,9 +233,9 @@ func (u User) UpdateUser(ctx context.Context, req domain.UpdateUserRequest, admi
return errors.WithMessage(err, "update user")
}
err = tx.ForceUpsert(ctx, int(user.Id), req.Roles)
err = tx.UpdateUserRoleLinks(ctx, int(user.Id), req.Roles)
if err != nil {
return errors.WithMessage(err, "force upsert")
return errors.WithMessage(err, "update user role links")
}
return nil