github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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  	"reflect"
     8  
     9  	"net/http"
    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) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
    54  	if result := <-a.Srv.Store.Role().Save(role); result.Err != nil {
    55  		return nil, result.Err
    56  	} else {
    57  		a.sendUpdatedRoleEvent(role)
    58  
    59  		return role, nil
    60  	}
    61  }
    62  
    63  func (a *App) CheckRolesExist(roleNames []string) *model.AppError {
    64  	roles, err := a.GetRolesByNames(roleNames)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	for _, name := range roleNames {
    70  		nameFound := false
    71  		for _, role := range roles {
    72  			if name == role.Name {
    73  				nameFound = true
    74  				break
    75  			}
    76  		}
    77  		if !nameFound {
    78  			return model.NewAppError("CheckRolesExist", "app.role.check_roles_exist.role_not_found", nil, "role="+name, http.StatusBadRequest)
    79  		}
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func (a *App) sendUpdatedRoleEvent(role *model.Role) {
    86  	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_ROLE_UPDATED, "", "", "", nil)
    87  	message.Add("role", role.ToJson())
    88  
    89  	a.Go(func() {
    90  		a.Publish(message)
    91  	})
    92  }