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>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
25257d6ef6
Коммит
e70c5a605a
22
api4/role.go
22
api4/role.go
@@ -19,12 +19,34 @@ var notAllowedPermissions = []string{
|
||||
}
|
||||
|
||||
func (api *API) InitRole() {
|
||||
api.BaseRoutes.Roles.Handle("", api.APISessionRequired(getAllRoles)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/{role_id:[A-Za-z0-9]+}", api.APISessionRequiredTrustRequester(getRole)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/name/{role_name:[a-z0-9_]+}", api.APISessionRequiredTrustRequester(getRoleByName)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/names", api.APISessionRequiredTrustRequester(getRolesByNames)).Methods("POST")
|
||||
api.BaseRoutes.Roles.Handle("/{role_id:[A-Za-z0-9]+}/patch", api.APISessionRequired(patchRole)).Methods("PUT")
|
||||
}
|
||||
|
||||
func getAllRoles(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageSystem) {
|
||||
c.SetPermissionError(model.PermissionManageSystem)
|
||||
return
|
||||
}
|
||||
|
||||
roles, err := c.App.GetAllRoles()
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
js, jsonErr := json.Marshal(roles)
|
||||
if jsonErr != nil {
|
||||
c.Err = model.NewAppError("getAllRoles", "api.marshal_error", nil, jsonErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(js)
|
||||
}
|
||||
|
||||
func getRole(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireRoleId()
|
||||
if c.Err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package api4
|
||||
|
||||
func (api *API) InitRoleLocal() {
|
||||
api.BaseRoutes.Roles.Handle("", api.APILocal(getAllRoles)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/{role_id:[A-Za-z0-9]+}", api.APILocal(getRole)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/name/{role_name:[a-z0-9_]+}", api.APILocal(getRoleByName)).Methods("GET")
|
||||
api.BaseRoutes.Roles.Handle("/names", api.APILocal(getRolesByNames)).Methods("POST")
|
||||
|
||||
@@ -15,6 +15,28 @@ import (
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
)
|
||||
|
||||
func TestGetAllRoles(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
roles, err := th.App.Srv().Store.Role().GetAll()
|
||||
require.NoError(t, err)
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
received, resp, err := client.GetAllRoles()
|
||||
require.NoError(t, err)
|
||||
CheckOKStatus(t, resp)
|
||||
|
||||
assert.EqualValues(t, received, roles)
|
||||
})
|
||||
|
||||
t.Run("NormalClient", func(t *testing.T) {
|
||||
_, resp, err := th.Client.GetAllRoles()
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetRole(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
@@ -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")
|
||||
|
||||
14
app/role.go
14
app/role.go
@@ -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()
|
||||
|
||||
@@ -5915,6 +5915,10 @@
|
||||
"id": "app.role.get.app_error",
|
||||
"translation": "Unable to get role."
|
||||
},
|
||||
{
|
||||
"id": "app.role.get_all.app_error",
|
||||
"translation": "Unable to get all the roles."
|
||||
},
|
||||
{
|
||||
"id": "app.role.get_by_name.app_error",
|
||||
"translation": "Unable to get role."
|
||||
|
||||
@@ -6483,6 +6483,20 @@ func (c *Client4) DownloadJob(jobId string) ([]byte, *Response, error) {
|
||||
|
||||
// Roles Section
|
||||
|
||||
// GetAllRoles returns a list of all the roles.
|
||||
func (c *Client4) GetAllRoles() ([]*Role, *Response, error) {
|
||||
r, err := c.DoAPIGet(c.rolesRoute(), "")
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), err
|
||||
}
|
||||
defer closeBody(r)
|
||||
var list []*Role
|
||||
if jsonErr := json.NewDecoder(r.Body).Decode(&list); jsonErr != nil {
|
||||
return nil, nil, NewAppError("GetAllRoles", "api.unmarshal_error", nil, jsonErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return list, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
// GetRole gets a single role by ID.
|
||||
func (c *Client4) GetRole(id string) (*Role, *Response, error) {
|
||||
r, err := c.DoAPIGet(c.rolesRoute()+fmt.Sprintf("/%v", id), "")
|
||||
|
||||
Ссылка в новой задаче
Block a user