MM-32396 Create endpoint for listing all the roles (#16988)

* Create an endpoint for listing roles

* Add function to client model

* Create tests for listing roles endpoint

* Apply code review suggestions

* Restore GetAllRoles app method

* Use AppContext instead of App

* Minor fix

* Refactor according to new changes

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Berke Kalkan
2021-11-08 16:38:41 +03:00
коммит произвёл GitHub
родитель 25257d6ef6
Коммит e70c5a605a
9 изменённых файлов: 114 добавлений и 0 удалений

Просмотреть файл

@@ -543,6 +543,7 @@ type AppIface interface {
GetAllPrivateTeams() ([]*model.Team, *model.AppError)
GetAllPublicTeams() ([]*model.Team, *model.AppError)
GetAllRemoteClusters(filter model.RemoteClusterQueryFilter) ([]*model.RemoteCluster, *model.AppError)
GetAllRoles() ([]*model.Role, *model.AppError)
GetAllStatuses() map[string]*model.Status
GetAllTeams() ([]*model.Team, *model.AppError)
GetAllTeamsPage(offset int, limit int, opts *model.TeamSearch) ([]*model.Team, *model.AppError)

Просмотреть файл

@@ -4369,6 +4369,28 @@ func (a *OpenTracingAppLayer) GetAllRemoteClusters(filter model.RemoteClusterQue
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetAllRoles() ([]*model.Role, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetAllRoles")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetAllRoles()
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetAllStatuses() map[string]*model.Status {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetAllStatuses")

Просмотреть файл

@@ -37,6 +37,20 @@ func (a *App) GetRole(id string) (*model.Role, *model.AppError) {
return role, nil
}
func (a *App) GetAllRoles() ([]*model.Role, *model.AppError) {
roles, err := a.Srv().Store.Role().GetAll()
if err != nil {
return nil, model.NewAppError("GetAllRoles", "app.role.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
appErr := a.Srv().mergeChannelHigherScopedPermissions(roles)
if appErr != nil {
return nil, appErr
}
return roles, nil
}
func (s *Server) GetRoleByName(ctx context.Context, name string) (*model.Role, *model.AppError) {
role, nErr := s.Store.Role().GetByName(ctx, name)
if nErr != nil {

Просмотреть файл

@@ -61,6 +61,20 @@ func TestGetRoleByID(t *testing.T) {
})
}
func TestGetAllRoles(t *testing.T) {
testPermissionInheritance(t, func(t *testing.T, th *TestHelper, testData permissionInheritanceTestData) {
actualRoles, err := th.App.GetAllRoles()
require.Nil(t, err)
for _, actualRole := range actualRoles {
if actualRole.Id == testData.channelRole.Id {
require.NotNil(t, actualRole)
require.Equal(t, testData.channelRole.Id, actualRole.Id)
require.Equal(t, testData.shouldHavePermission, utils.StringInSlice(testData.permission.Id, actualRole.Permissions), "row: %+v", testData.truthTableRow)
}
}
})
}
// testPermissionInheritance tests 48 combinations of scheme, permission, role data.
func testPermissionInheritance(t *testing.T, testCallback func(t *testing.T, th *TestHelper, testData permissionInheritanceTestData)) {
th := Setup(t).InitBasic()