Skip to content

Commit

Permalink
change: role name to role id
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Dec 20, 2024
1 parent e95d1bf commit ef107a4
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 142 deletions.
3 changes: 3 additions & 0 deletions api/http/util/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ var (
// ErrMissingRoleName indicates that role name are empty.
ErrMissingRoleName = errors.New("empty role name")

// ErrMissingRoleID indicates that role name are empty.
ErrMissingRoleID = errors.New("empty role id")

// ErrMissingRoleOperations indicates that role operations are empty.
ErrMissingRoleOperations = errors.New("empty role operations")

Expand Down
52 changes: 26 additions & 26 deletions pkg/roles/provisionmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func (r ProvisionManageService) AddRole(ctx context.Context, session authn.Sessi
return newRoles[0], nil
}

func (r ProvisionManageService) RemoveRole(ctx context.Context, session authn.Session, entityID, roleName string) error {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RemoveRole(ctx context.Context, session authn.Session, entityID, roleID string) error {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return errors.Wrap(svcerr.ErrRemoveEntity, err)
}
Expand All @@ -311,8 +311,8 @@ func (r ProvisionManageService) RemoveRole(ctx context.Context, session authn.Se
return nil
}

func (r ProvisionManageService) UpdateRoleName(ctx context.Context, session authn.Session, entityID, oldRoleName, newRoleName string) (Role, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, oldRoleName)
func (r ProvisionManageService) UpdateRoleName(ctx context.Context, session authn.Session, entityID, roleID, newRoleName string) (Role, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return Role{}, errors.Wrap(svcerr.ErrUpdateEntity, err)
}
Expand All @@ -329,8 +329,8 @@ func (r ProvisionManageService) UpdateRoleName(ctx context.Context, session auth
return ro, nil
}

func (r ProvisionManageService) RetrieveRole(ctx context.Context, session authn.Session, entityID, roleName string) (Role, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RetrieveRole(ctx context.Context, session authn.Session, entityID, roleID string) (Role, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return Role{}, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand All @@ -353,8 +353,8 @@ func (r ProvisionManageService) ListAvailableActions(ctx context.Context, sessio
return acts, nil
}

func (r ProvisionManageService) RoleAddActions(ctx context.Context, session authn.Session, entityID, roleName string, actions []string) (retActs []string, retErr error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleAddActions(ctx context.Context, session authn.Session, entityID, roleID string, actions []string) (retActs []string, retErr error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return []string{}, errors.Wrap(svcerr.ErrUpdateEntity, err)
}
Expand Down Expand Up @@ -401,8 +401,8 @@ func (r ProvisionManageService) RoleAddActions(ctx context.Context, session auth
return resActs, nil
}

func (r ProvisionManageService) RoleListActions(ctx context.Context, session authn.Session, entityID, roleName string) ([]string, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleListActions(ctx context.Context, session authn.Session, entityID, roleID string) ([]string, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return []string{}, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand All @@ -414,8 +414,8 @@ func (r ProvisionManageService) RoleListActions(ctx context.Context, session aut
return acts, nil
}

func (r ProvisionManageService) RoleCheckActionsExists(ctx context.Context, session authn.Session, entityID, roleName string, actions []string) (bool, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleCheckActionsExists(ctx context.Context, session authn.Session, entityID, roleID string, actions []string) (bool, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return false, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand All @@ -427,8 +427,8 @@ func (r ProvisionManageService) RoleCheckActionsExists(ctx context.Context, sess
return result, nil
}

func (r ProvisionManageService) RoleRemoveActions(ctx context.Context, session authn.Session, entityID, roleName string, actions []string) (err error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleRemoveActions(ctx context.Context, session authn.Session, entityID, roleID string, actions []string) (err error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return errors.Wrap(svcerr.ErrRemoveEntity, err)
}
Expand Down Expand Up @@ -460,8 +460,8 @@ func (r ProvisionManageService) RoleRemoveActions(ctx context.Context, session a
return nil
}

func (r ProvisionManageService) RoleRemoveAllActions(ctx context.Context, session authn.Session, entityID, roleName string) error {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleRemoveAllActions(ctx context.Context, session authn.Session, entityID, roleID string) error {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return errors.Wrap(svcerr.ErrRemoveEntity, err)
}
Expand All @@ -484,8 +484,8 @@ func (r ProvisionManageService) RoleRemoveAllActions(ctx context.Context, sessio
return nil
}

func (r ProvisionManageService) RoleAddMembers(ctx context.Context, session authn.Session, entityID, roleName string, members []string) (retMems []string, retErr error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleAddMembers(ctx context.Context, session authn.Session, entityID, roleID string, members []string) (retMems []string, retErr error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return []string{}, errors.Wrap(svcerr.ErrUpdateEntity, err)
}
Expand Down Expand Up @@ -527,8 +527,8 @@ func (r ProvisionManageService) RoleAddMembers(ctx context.Context, session auth
return mems, nil
}

func (r ProvisionManageService) RoleListMembers(ctx context.Context, session authn.Session, entityID, roleName string, limit, offset uint64) (MembersPage, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleListMembers(ctx context.Context, session authn.Session, entityID, roleID string, limit, offset uint64) (MembersPage, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return MembersPage{}, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand All @@ -540,8 +540,8 @@ func (r ProvisionManageService) RoleListMembers(ctx context.Context, session aut
return mp, nil
}

func (r ProvisionManageService) RoleCheckMembersExists(ctx context.Context, session authn.Session, entityID, roleName string, members []string) (bool, error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleCheckMembersExists(ctx context.Context, session authn.Session, entityID, roleID string, members []string) (bool, error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return false, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand All @@ -553,8 +553,8 @@ func (r ProvisionManageService) RoleCheckMembersExists(ctx context.Context, sess
return result, nil
}

func (r ProvisionManageService) RoleRemoveMembers(ctx context.Context, session authn.Session, entityID, roleName string, members []string) (err error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleRemoveMembers(ctx context.Context, session authn.Session, entityID, roleID string, members []string) (err error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return errors.Wrap(svcerr.ErrRemoveEntity, err)
}
Expand Down Expand Up @@ -586,8 +586,8 @@ func (r ProvisionManageService) RoleRemoveMembers(ctx context.Context, session a
return nil
}

func (r ProvisionManageService) RoleRemoveAllMembers(ctx context.Context, session authn.Session, entityID, roleName string) (err error) {
ro, err := r.repo.RetrieveRoleByEntityIDAndName(ctx, entityID, roleName)
func (r ProvisionManageService) RoleRemoveAllMembers(ctx context.Context, session authn.Session, entityID, roleID string) (err error) {
ro, err := r.repo.RetrieveEntityRole(ctx, entityID, roleID)
if err != nil {
return errors.Wrap(svcerr.ErrRemoveEntity, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/roles/repo/postgres/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ func (repo *Repository) RetrieveRole(ctx context.Context, roleID string) (roles.
return roles.Role{}, repoerr.ErrNotFound
}

func (repo *Repository) RetrieveRoleByEntityIDAndName(ctx context.Context, entityID, roleName string) (roles.Role, error) {
func (repo *Repository) RetrieveEntityRole(ctx context.Context, entityID, roleID string) (roles.Role, error) {
q := fmt.Sprintf(`SELECT id, name, entity_id, created_by, created_at, updated_by, updated_at
FROM %s_roles WHERE entity_id = :entity_id and name = :name`, repo.tableNamePrefix)
FROM %s_roles WHERE entity_id = :entity_id and id = :id`, repo.tableNamePrefix)

dbr := dbRole{
EntityID: entityID,
Name: roleName,
ID: roleID,
}

rows, err := repo.db.NamedQueryContext(ctx, q, dbr)
Expand Down
22 changes: 11 additions & 11 deletions pkg/roles/rolemanager/api/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d Decoder) DecodeViewRole(_ context.Context, r *http.Request) (interface{}
req := viewRoleReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
return req, nil
}
Expand All @@ -71,7 +71,7 @@ func (d Decoder) DecodeUpdateRole(_ context.Context, r *http.Request) (interface
req := updateRoleReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
Expand All @@ -83,7 +83,7 @@ func (d Decoder) DecodeDeleteRole(_ context.Context, r *http.Request) (interface
req := deleteRoleReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
return req, nil
}
Expand All @@ -102,7 +102,7 @@ func (d Decoder) DecodeAddRoleActions(_ context.Context, r *http.Request) (inter
req := addRoleActionsReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
Expand All @@ -114,7 +114,7 @@ func (d Decoder) DecodeListRoleActions(_ context.Context, r *http.Request) (inte
req := listRoleActionsReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
return req, nil
}
Expand All @@ -126,7 +126,7 @@ func (d Decoder) DecodeDeleteRoleActions(_ context.Context, r *http.Request) (in
req := deleteRoleActionsReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
Expand All @@ -138,7 +138,7 @@ func (d Decoder) DecodeDeleteAllRoleActions(_ context.Context, r *http.Request)
req := deleteAllRoleActionsReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
return req, nil
}
Expand All @@ -150,7 +150,7 @@ func (d Decoder) DecodeAddRoleMembers(_ context.Context, r *http.Request) (inter
req := addRoleMembersReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
Expand All @@ -170,7 +170,7 @@ func (d Decoder) DecodeListRoleMembers(_ context.Context, r *http.Request) (inte
req := listRoleMembersReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
limit: l,
offset: o,
}
Expand All @@ -184,7 +184,7 @@ func (d Decoder) DecodeDeleteRoleMembers(_ context.Context, r *http.Request) (in
req := deleteRoleMembersReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
Expand All @@ -196,7 +196,7 @@ func (d Decoder) DecodeDeleteAllRoleMembers(_ context.Context, r *http.Request)
req := deleteAllRoleMembersReq{
token: apiutil.ExtractBearerToken(r),
entityID: chi.URLParam(r, d.entityIDTemplate),
roleName: chi.URLParam(r, "roleName"),
roleID: chi.URLParam(r, "roleID"),
}
return req, nil
}
22 changes: 11 additions & 11 deletions pkg/roles/rolemanager/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func ViewRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

ro, err := svc.RetrieveRole(ctx, session, req.entityID, req.roleName)
ro, err := svc.RetrieveRole(ctx, session, req.entityID, req.roleID)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +87,7 @@ func UpdateRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

ro, err := svc.UpdateRoleName(ctx, session, req.entityID, req.roleName, req.Name)
ro, err := svc.UpdateRoleName(ctx, session, req.entityID, req.roleID, req.Name)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func DeleteRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

if err := svc.RemoveRole(ctx, session, req.entityID, req.roleName); err != nil {
if err := svc.RemoveRole(ctx, session, req.entityID, req.roleID); err != nil {
return nil, err
}
return deleteRoleRes{}, nil
Expand Down Expand Up @@ -146,7 +146,7 @@ func AddRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

caps, err := svc.RoleAddActions(ctx, session, req.entityID, req.roleName, req.Actions)
caps, err := svc.RoleAddActions(ctx, session, req.entityID, req.roleID, req.Actions)
if err != nil {
return nil, err
}
Expand All @@ -166,7 +166,7 @@ func ListRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

caps, err := svc.RoleListActions(ctx, session, req.entityID, req.roleName)
caps, err := svc.RoleListActions(ctx, session, req.entityID, req.roleID)
if err != nil {
return nil, err
}
Expand All @@ -186,7 +186,7 @@ func DeleteRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

if err := svc.RoleRemoveActions(ctx, session, req.entityID, req.roleName, req.Actions); err != nil {
if err := svc.RoleRemoveActions(ctx, session, req.entityID, req.roleID, req.Actions); err != nil {
return nil, err
}
return deleteRoleActionsRes{}, nil
Expand All @@ -205,7 +205,7 @@ func DeleteAllRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

if err := svc.RoleRemoveAllActions(ctx, session, req.entityID, req.roleName); err != nil {
if err := svc.RoleRemoveAllActions(ctx, session, req.entityID, req.roleID); err != nil {
return nil, err
}
return deleteAllRoleActionsRes{}, nil
Expand All @@ -224,7 +224,7 @@ func AddRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

members, err := svc.RoleAddMembers(ctx, session, req.entityID, req.roleName, req.Members)
members, err := svc.RoleAddMembers(ctx, session, req.entityID, req.roleID, req.Members)
if err != nil {
return nil, err
}
Expand All @@ -244,7 +244,7 @@ func ListRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

mp, err := svc.RoleListMembers(ctx, session, req.entityID, req.roleName, req.limit, req.offset)
mp, err := svc.RoleListMembers(ctx, session, req.entityID, req.roleID, req.limit, req.offset)
if err != nil {
return nil, err
}
Expand All @@ -264,7 +264,7 @@ func DeleteRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

if err := svc.RoleRemoveMembers(ctx, session, req.entityID, req.roleName, req.Members); err != nil {
if err := svc.RoleRemoveMembers(ctx, session, req.entityID, req.roleID, req.Members); err != nil {
return nil, err
}
return deleteRoleMembersRes{}, nil
Expand All @@ -283,7 +283,7 @@ func DeleteAllRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

if err := svc.RoleRemoveAllMembers(ctx, session, req.entityID, req.roleName); err != nil {
if err := svc.RoleRemoveAllMembers(ctx, session, req.entityID, req.roleID); err != nil {
return nil, err
}
return deleteAllRoleMemberRes{}, nil
Expand Down
Loading

0 comments on commit ef107a4

Please sign in to comment.