github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/store/sqlstore/role_supplier.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"fmt"
    10  	"net/http"
    11  	"strings"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store"
    15  )
    16  
    17  type Role struct {
    18  	Id            string
    19  	Name          string
    20  	DisplayName   string
    21  	Description   string
    22  	CreateAt      int64
    23  	UpdateAt      int64
    24  	DeleteAt      int64
    25  	Permissions   string
    26  	SchemeManaged bool
    27  }
    28  
    29  func NewRoleFromModel(role *model.Role) *Role {
    30  	permissionsMap := make(map[string]bool)
    31  	permissions := ""
    32  
    33  	for _, permission := range role.Permissions {
    34  		if !permissionsMap[permission] {
    35  			permissions += fmt.Sprintf(" %v", permission)
    36  			permissionsMap[permission] = true
    37  		}
    38  	}
    39  
    40  	return &Role{
    41  		Id:            role.Id,
    42  		Name:          role.Name,
    43  		DisplayName:   role.DisplayName,
    44  		Description:   role.Description,
    45  		CreateAt:      role.CreateAt,
    46  		UpdateAt:      role.UpdateAt,
    47  		DeleteAt:      role.DeleteAt,
    48  		Permissions:   permissions,
    49  		SchemeManaged: role.SchemeManaged,
    50  	}
    51  }
    52  
    53  func (role Role) ToModel() *model.Role {
    54  	return &model.Role{
    55  		Id:            role.Id,
    56  		Name:          role.Name,
    57  		DisplayName:   role.DisplayName,
    58  		Description:   role.Description,
    59  		CreateAt:      role.CreateAt,
    60  		UpdateAt:      role.UpdateAt,
    61  		DeleteAt:      role.DeleteAt,
    62  		Permissions:   strings.Fields(role.Permissions),
    63  		SchemeManaged: role.SchemeManaged,
    64  	}
    65  }
    66  
    67  func initSqlSupplierRoles(sqlStore SqlStore) {
    68  	for _, db := range sqlStore.GetAllConns() {
    69  		table := db.AddTableWithName(Role{}, "Roles").SetKeys(false, "Id")
    70  		table.ColMap("Id").SetMaxSize(26)
    71  		table.ColMap("Name").SetMaxSize(64).SetUnique(true)
    72  		table.ColMap("DisplayName").SetMaxSize(128)
    73  		table.ColMap("Description").SetMaxSize(1024)
    74  		table.ColMap("Permissions").SetMaxSize(4096)
    75  	}
    76  }
    77  
    78  func (s *SqlSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
    79  	result := store.NewSupplierResult()
    80  
    81  	// Check the role is valid before proceeding.
    82  	if !role.IsValidWithoutId() {
    83  		result.Err = model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.invalid_role.app_error", nil, "", http.StatusBadRequest)
    84  		return result
    85  	}
    86  
    87  	dbRole := NewRoleFromModel(role)
    88  	if len(dbRole.Id) == 0 {
    89  		dbRole.Id = model.NewId()
    90  		dbRole.CreateAt = model.GetMillis()
    91  		dbRole.UpdateAt = dbRole.CreateAt
    92  		if err := s.GetMaster().Insert(dbRole); err != nil {
    93  			result.Err = model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.insert.app_error", nil, err.Error(), http.StatusInternalServerError)
    94  		}
    95  	} else {
    96  		dbRole.UpdateAt = model.GetMillis()
    97  		if rowsChanged, err := s.GetMaster().Update(dbRole); err != nil {
    98  			result.Err = model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.update.app_error", nil, err.Error(), http.StatusInternalServerError)
    99  		} else if rowsChanged != 1 {
   100  			result.Err = model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.update.app_error", nil, "no record to update", http.StatusInternalServerError)
   101  		}
   102  	}
   103  
   104  	result.Data = dbRole.ToModel()
   105  
   106  	return result
   107  }
   108  
   109  func (s *SqlSupplier) RoleGet(ctx context.Context, roleId string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
   110  	result := store.NewSupplierResult()
   111  
   112  	var dbRole Role
   113  
   114  	if err := s.GetReplica().SelectOne(&dbRole, "SELECT * from Roles WHERE Id = :Id", map[string]interface{}{"Id": roleId}); err != nil {
   115  		if err == sql.ErrNoRows {
   116  			result.Err = model.NewAppError("SqlRoleStore.Get", "store.sql_role.get.app_error", nil, "Id="+roleId+", "+err.Error(), http.StatusNotFound)
   117  		} else {
   118  			result.Err = model.NewAppError("SqlRoleStore.Get", "store.sql_role.get.app_error", nil, err.Error(), http.StatusInternalServerError)
   119  		}
   120  	}
   121  
   122  	result.Data = dbRole.ToModel()
   123  
   124  	return result
   125  }
   126  
   127  func (s *SqlSupplier) RoleGetByName(ctx context.Context, name string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
   128  	result := store.NewSupplierResult()
   129  
   130  	var dbRole Role
   131  
   132  	if err := s.GetReplica().SelectOne(&dbRole, "SELECT * from Roles WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
   133  		if err == sql.ErrNoRows {
   134  			result.Err = model.NewAppError("SqlRoleStore.GetByName", "store.sql_role.get_by_name.app_error", nil, "name="+name+",err="+err.Error(), http.StatusNotFound)
   135  		} else {
   136  			result.Err = model.NewAppError("SqlRoleStore.GetByName", "store.sql_role.get_by_name.app_error", nil, "name="+name+",err="+err.Error(), http.StatusInternalServerError)
   137  		}
   138  	}
   139  
   140  	result.Data = dbRole.ToModel()
   141  
   142  	return result
   143  }
   144  
   145  func (s *SqlSupplier) RoleGetByNames(ctx context.Context, names []string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
   146  	result := store.NewSupplierResult()
   147  
   148  	var dbRoles []*Role
   149  
   150  	if len(names) == 0 {
   151  		result.Data = []*model.Role{}
   152  		return result
   153  	}
   154  
   155  	var searchPlaceholders []string
   156  	var parameters = map[string]interface{}{}
   157  	for i, value := range names {
   158  		searchPlaceholders = append(searchPlaceholders, fmt.Sprintf(":Name%d", i))
   159  		parameters[fmt.Sprintf("Name%d", i)] = value
   160  	}
   161  
   162  	searchTerm := "Name IN (" + strings.Join(searchPlaceholders, ", ") + ")"
   163  
   164  	if _, err := s.GetReplica().Select(&dbRoles, "SELECT * from Roles WHERE "+searchTerm, parameters); err != nil {
   165  		result.Err = model.NewAppError("SqlRoleStore.GetByNames", "store.sql_role.get_by_names.app_error", nil, err.Error(), http.StatusInternalServerError)
   166  	}
   167  
   168  	var roles []*model.Role
   169  	for _, dbRole := range dbRoles {
   170  		roles = append(roles, dbRole.ToModel())
   171  	}
   172  
   173  	result.Data = roles
   174  
   175  	return result
   176  }
   177  
   178  func (s *SqlSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
   179  	result := store.NewSupplierResult()
   180  
   181  	if _, err := s.GetMaster().Exec("DELETE FROM Roles"); err != nil {
   182  		result.Err = model.NewAppError("SqlRoleStore.PermanentDeleteAll", "store.sql_role.permanent_delete_all.app_error", nil, err.Error(), http.StatusInternalServerError)
   183  	}
   184  
   185  	return result
   186  }