github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/role.go (about) 1 // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "net/http" 8 "reflect" 9 "strings" 10 11 "github.com/mattermost/mattermost-server/model" 12 ) 13 14 func (a *App) GetRole(id string) (*model.Role, *model.AppError) { 15 if result := <-a.Srv.Store.Role().Get(id); result.Err != nil { 16 return nil, result.Err 17 } else { 18 return result.Data.(*model.Role), nil 19 } 20 } 21 22 func (a *App) GetRoleByName(name string) (*model.Role, *model.AppError) { 23 if result := <-a.Srv.Store.Role().GetByName(name); result.Err != nil { 24 return nil, result.Err 25 } else { 26 return result.Data.(*model.Role), nil 27 } 28 } 29 30 func (a *App) GetRolesByNames(names []string) ([]*model.Role, *model.AppError) { 31 if result := <-a.Srv.Store.Role().GetByNames(names); result.Err != nil { 32 return nil, result.Err 33 } else { 34 return result.Data.([]*model.Role), nil 35 } 36 } 37 38 func (a *App) PatchRole(role *model.Role, patch *model.RolePatch) (*model.Role, *model.AppError) { 39 // If patch is a no-op then short-circuit the store. 40 if patch.Permissions != nil && reflect.DeepEqual(*patch.Permissions, role.Permissions) { 41 return role, nil 42 } 43 44 role.Patch(patch) 45 role, err := a.UpdateRole(role) 46 if err != nil { 47 return nil, err 48 } 49 50 return role, err 51 } 52 53 func (a *App) CreateRole(role *model.Role) (*model.Role, *model.AppError) { 54 role.Id = "" 55 role.CreateAt = 0 56 role.UpdateAt = 0 57 role.DeleteAt = 0 58 role.BuiltIn = false 59 role.SchemeManaged = false 60 61 if result := <-a.Srv.Store.Role().Save(role); result.Err != nil { 62 return nil, result.Err 63 } else { 64 return result.Data.(*model.Role), nil 65 } 66 } 67 68 func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) { 69 if result := <-a.Srv.Store.Role().Save(role); result.Err != nil { 70 return nil, result.Err 71 } else { 72 a.sendUpdatedRoleEvent(role) 73 74 return role, nil 75 } 76 } 77 78 func (a *App) CheckRolesExist(roleNames []string) *model.AppError { 79 roles, err := a.GetRolesByNames(roleNames) 80 if err != nil { 81 return err 82 } 83 84 for _, name := range roleNames { 85 nameFound := false 86 for _, role := range roles { 87 if name == role.Name { 88 nameFound = true 89 break 90 } 91 } 92 if !nameFound { 93 return model.NewAppError("CheckRolesExist", "app.role.check_roles_exist.role_not_found", nil, "role="+name, http.StatusBadRequest) 94 } 95 } 96 97 return nil 98 } 99 100 func (a *App) sendUpdatedRoleEvent(role *model.Role) { 101 message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_ROLE_UPDATED, "", "", "", nil) 102 message.Add("role", role.ToJson()) 103 104 a.Go(func() { 105 a.Publish(message) 106 }) 107 } 108 109 func RemoveRoles(rolesToRemove []string, roles string) string { 110 roleList := strings.Fields(roles) 111 newRoles := make([]string, 0) 112 113 for _, role := range roleList { 114 shouldRemove := false 115 for _, roleToRemove := range rolesToRemove { 116 if role == roleToRemove { 117 shouldRemove = true 118 break 119 } 120 } 121 if !shouldRemove { 122 newRoles = append(newRoles, role) 123 } 124 } 125 126 return strings.Join(newRoles, " ") 127 }