github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/scheme.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 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 ) 12 13 func (a *App) GetScheme(id string) (*model.Scheme, *model.AppError) { 14 if err := a.IsPhase2MigrationCompleted(); err != nil { 15 return nil, err 16 } 17 18 if result := <-a.Srv.Store.Scheme().Get(id); result.Err != nil { 19 return nil, result.Err 20 } else { 21 return result.Data.(*model.Scheme), nil 22 } 23 } 24 25 func (a *App) GetSchemeByName(name string) (*model.Scheme, *model.AppError) { 26 if err := a.IsPhase2MigrationCompleted(); err != nil { 27 return nil, err 28 } 29 30 if result := <-a.Srv.Store.Scheme().GetByName(name); result.Err != nil { 31 return nil, result.Err 32 } else { 33 return result.Data.(*model.Scheme), nil 34 } 35 } 36 37 func (a *App) GetSchemesPage(scope string, page int, perPage int) ([]*model.Scheme, *model.AppError) { 38 if err := a.IsPhase2MigrationCompleted(); err != nil { 39 return nil, err 40 } 41 42 return a.GetSchemes(scope, page*perPage, perPage) 43 } 44 45 func (a *App) GetSchemes(scope string, offset int, limit int) ([]*model.Scheme, *model.AppError) { 46 if err := a.IsPhase2MigrationCompleted(); err != nil { 47 return nil, err 48 } 49 50 if result := <-a.Srv.Store.Scheme().GetAllPage(scope, offset, limit); result.Err != nil { 51 return nil, result.Err 52 } else { 53 return result.Data.([]*model.Scheme), nil 54 } 55 } 56 57 func (a *App) CreateScheme(scheme *model.Scheme) (*model.Scheme, *model.AppError) { 58 if err := a.IsPhase2MigrationCompleted(); err != nil { 59 return nil, err 60 } 61 62 // Clear any user-provided values for trusted properties. 63 scheme.DefaultTeamAdminRole = "" 64 scheme.DefaultTeamUserRole = "" 65 scheme.DefaultChannelAdminRole = "" 66 scheme.DefaultChannelUserRole = "" 67 scheme.CreateAt = 0 68 scheme.UpdateAt = 0 69 scheme.DeleteAt = 0 70 71 if result := <-a.Srv.Store.Scheme().Save(scheme); result.Err != nil { 72 return nil, result.Err 73 } else { 74 return scheme, nil 75 } 76 } 77 78 func (a *App) PatchScheme(scheme *model.Scheme, patch *model.SchemePatch) (*model.Scheme, *model.AppError) { 79 if err := a.IsPhase2MigrationCompleted(); err != nil { 80 return nil, err 81 } 82 83 scheme.Patch(patch) 84 scheme, err := a.UpdateScheme(scheme) 85 if err != nil { 86 return nil, err 87 } 88 89 return scheme, err 90 } 91 92 func (a *App) UpdateScheme(scheme *model.Scheme) (*model.Scheme, *model.AppError) { 93 if err := a.IsPhase2MigrationCompleted(); err != nil { 94 return nil, err 95 } 96 97 if result := <-a.Srv.Store.Scheme().Save(scheme); result.Err != nil { 98 return nil, result.Err 99 } else { 100 return scheme, nil 101 } 102 } 103 104 func (a *App) DeleteScheme(schemeId string) (*model.Scheme, *model.AppError) { 105 if err := a.IsPhase2MigrationCompleted(); err != nil { 106 return nil, err 107 } 108 109 if result := <-a.Srv.Store.Scheme().Delete(schemeId); result.Err != nil { 110 return nil, result.Err 111 } else { 112 return result.Data.(*model.Scheme), nil 113 } 114 } 115 116 func (a *App) GetTeamsForSchemePage(scheme *model.Scheme, page int, perPage int) ([]*model.Team, *model.AppError) { 117 if err := a.IsPhase2MigrationCompleted(); err != nil { 118 return nil, err 119 } 120 121 return a.GetTeamsForScheme(scheme, page*perPage, perPage) 122 } 123 124 func (a *App) GetTeamsForScheme(scheme *model.Scheme, offset int, limit int) ([]*model.Team, *model.AppError) { 125 if err := a.IsPhase2MigrationCompleted(); err != nil { 126 return nil, err 127 } 128 129 if result := <-a.Srv.Store.Team().GetTeamsByScheme(scheme.Id, offset, limit); result.Err != nil { 130 return nil, result.Err 131 } else { 132 return result.Data.([]*model.Team), nil 133 } 134 } 135 136 func (a *App) GetChannelsForSchemePage(scheme *model.Scheme, page int, perPage int) (model.ChannelList, *model.AppError) { 137 if err := a.IsPhase2MigrationCompleted(); err != nil { 138 return nil, err 139 } 140 141 return a.GetChannelsForScheme(scheme, page*perPage, perPage) 142 } 143 144 func (a *App) GetChannelsForScheme(scheme *model.Scheme, offset int, limit int) (model.ChannelList, *model.AppError) { 145 if err := a.IsPhase2MigrationCompleted(); err != nil { 146 return nil, err 147 } 148 149 if result := <-a.Srv.Store.Channel().GetChannelsByScheme(scheme.Id, offset, limit); result.Err != nil { 150 return nil, result.Err 151 } else { 152 return result.Data.(model.ChannelList), nil 153 } 154 } 155 156 func (a *App) IsPhase2MigrationCompleted() *model.AppError { 157 if a.phase2PermissionsMigrationComplete { 158 return nil 159 } 160 161 if result := <-a.Srv.Store.System().GetByName(model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2); result.Err != nil { 162 return model.NewAppError("App.IsPhase2MigrationCompleted", "app.schemes.is_phase_2_migration_completed.not_completed.app_error", nil, result.Err.Error(), http.StatusNotImplemented) 163 } 164 165 a.phase2PermissionsMigrationComplete = true 166 167 return nil 168 } 169 170 func (a *App) SchemesIterator(batchSize int) func() []*model.Scheme { 171 offset := 0 172 return func() []*model.Scheme { 173 var result store.StoreResult 174 if result = <-a.Srv.Store.Scheme().GetAllPage("", offset, batchSize); result.Err != nil { 175 return []*model.Scheme{} 176 } 177 offset += batchSize 178 return result.Data.([]*model.Scheme) 179 } 180 }