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 удалений

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

@@ -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()