github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/api4/scheme.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func (api *API) InitScheme() {
    13  	api.BaseRoutes.Schemes.Handle("", api.ApiSessionRequired(getSchemes)).Methods("GET")
    14  	api.BaseRoutes.Schemes.Handle("", api.ApiSessionRequired(createScheme)).Methods("POST")
    15  	api.BaseRoutes.Schemes.Handle("/{scheme_id:[A-Za-z0-9]+}", api.ApiSessionRequired(deleteScheme)).Methods("DELETE")
    16  	api.BaseRoutes.Schemes.Handle("/{scheme_id:[A-Za-z0-9]+}", api.ApiSessionRequiredTrustRequester(getScheme)).Methods("GET")
    17  	api.BaseRoutes.Schemes.Handle("/{scheme_id:[A-Za-z0-9]+}/patch", api.ApiSessionRequired(patchScheme)).Methods("PUT")
    18  	api.BaseRoutes.Schemes.Handle("/{scheme_id:[A-Za-z0-9]+}/teams", api.ApiSessionRequiredTrustRequester(getTeamsForScheme)).Methods("GET")
    19  	api.BaseRoutes.Schemes.Handle("/{scheme_id:[A-Za-z0-9]+}/channels", api.ApiSessionRequiredTrustRequester(getChannelsForScheme)).Methods("GET")
    20  }
    21  
    22  func createScheme(c *Context, w http.ResponseWriter, r *http.Request) {
    23  	scheme := model.SchemeFromJson(r.Body)
    24  	if scheme == nil {
    25  		c.SetInvalidParam("scheme")
    26  		return
    27  	}
    28  
    29  	if c.App.License() == nil || !*c.App.License().Features.CustomPermissionsSchemes {
    30  		c.Err = model.NewAppError("Api4.CreateScheme", "api.scheme.create_scheme.license.error", nil, "", http.StatusNotImplemented)
    31  		return
    32  	}
    33  
    34  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    35  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    36  		return
    37  	}
    38  
    39  	scheme, err := c.App.CreateScheme(scheme)
    40  	if err != nil {
    41  		c.Err = err
    42  		return
    43  	}
    44  
    45  	w.WriteHeader(http.StatusCreated)
    46  	w.Write([]byte(scheme.ToJson()))
    47  }
    48  
    49  func getScheme(c *Context, w http.ResponseWriter, r *http.Request) {
    50  	c.RequireSchemeId()
    51  	if c.Err != nil {
    52  		return
    53  	}
    54  
    55  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    56  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    57  		return
    58  	}
    59  
    60  	scheme, err := c.App.GetScheme(c.Params.SchemeId)
    61  	if err != nil {
    62  		c.Err = err
    63  		return
    64  	}
    65  
    66  	w.Write([]byte(scheme.ToJson()))
    67  }
    68  
    69  func getSchemes(c *Context, w http.ResponseWriter, r *http.Request) {
    70  	if c.Err != nil {
    71  		return
    72  	}
    73  
    74  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    75  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    76  		return
    77  	}
    78  
    79  	scope := c.Params.Scope
    80  	if scope != "" && scope != model.SCHEME_SCOPE_TEAM && scope != model.SCHEME_SCOPE_CHANNEL {
    81  		c.SetInvalidParam("scope")
    82  		return
    83  	}
    84  
    85  	schemes, err := c.App.GetSchemesPage(c.Params.Scope, c.Params.Page, c.Params.PerPage)
    86  	if err != nil {
    87  		c.Err = err
    88  		return
    89  	}
    90  
    91  	w.Write([]byte(model.SchemesToJson(schemes)))
    92  }
    93  
    94  func getTeamsForScheme(c *Context, w http.ResponseWriter, r *http.Request) {
    95  	c.RequireSchemeId()
    96  	if c.Err != nil {
    97  		return
    98  	}
    99  
   100  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   101  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   102  		return
   103  	}
   104  
   105  	scheme, err := c.App.GetScheme(c.Params.SchemeId)
   106  	if err != nil {
   107  		c.Err = err
   108  		return
   109  	}
   110  
   111  	if scheme.Scope != model.SCHEME_SCOPE_TEAM {
   112  		c.Err = model.NewAppError("Api4.GetTeamsForScheme", "api.scheme.get_teams_for_scheme.scope.error", nil, "", http.StatusBadRequest)
   113  		return
   114  	}
   115  
   116  	teams, err := c.App.GetTeamsForSchemePage(scheme, c.Params.Page, c.Params.PerPage)
   117  	if err != nil {
   118  		c.Err = err
   119  		return
   120  	}
   121  
   122  	w.Write([]byte(model.TeamListToJson(teams)))
   123  }
   124  
   125  func getChannelsForScheme(c *Context, w http.ResponseWriter, r *http.Request) {
   126  	c.RequireSchemeId()
   127  	if c.Err != nil {
   128  		return
   129  	}
   130  
   131  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   132  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   133  		return
   134  	}
   135  
   136  	scheme, err := c.App.GetScheme(c.Params.SchemeId)
   137  	if err != nil {
   138  		c.Err = err
   139  		return
   140  	}
   141  
   142  	if scheme.Scope != model.SCHEME_SCOPE_CHANNEL {
   143  		c.Err = model.NewAppError("Api4.GetChannelsForScheme", "api.scheme.get_channels_for_scheme.scope.error", nil, "", http.StatusBadRequest)
   144  		return
   145  	}
   146  
   147  	channels, err := c.App.GetChannelsForSchemePage(scheme, c.Params.Page, c.Params.PerPage)
   148  	if err != nil {
   149  		c.Err = err
   150  		return
   151  	}
   152  
   153  	w.Write([]byte(channels.ToJson()))
   154  }
   155  
   156  func patchScheme(c *Context, w http.ResponseWriter, r *http.Request) {
   157  	c.RequireSchemeId()
   158  	if c.Err != nil {
   159  		return
   160  	}
   161  
   162  	patch := model.SchemePatchFromJson(r.Body)
   163  	if patch == nil {
   164  		c.SetInvalidParam("scheme")
   165  		return
   166  	}
   167  
   168  	if c.App.License() == nil || !*c.App.License().Features.CustomPermissionsSchemes {
   169  		c.Err = model.NewAppError("Api4.PatchScheme", "api.scheme.patch_scheme.license.error", nil, "", http.StatusNotImplemented)
   170  		return
   171  	}
   172  
   173  	scheme, err := c.App.GetScheme(c.Params.SchemeId)
   174  	if err != nil {
   175  		c.Err = err
   176  		return
   177  	}
   178  
   179  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   180  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   181  		return
   182  	}
   183  
   184  	scheme, err = c.App.PatchScheme(scheme, patch)
   185  	if err != nil {
   186  		c.Err = err
   187  		return
   188  	}
   189  
   190  	c.LogAudit("")
   191  	w.Write([]byte(scheme.ToJson()))
   192  }
   193  
   194  func deleteScheme(c *Context, w http.ResponseWriter, r *http.Request) {
   195  	c.RequireSchemeId()
   196  	if c.Err != nil {
   197  		return
   198  	}
   199  
   200  	if c.App.License() == nil || !*c.App.License().Features.CustomPermissionsSchemes {
   201  		c.Err = model.NewAppError("Api4.DeleteScheme", "api.scheme.delete_scheme.license.error", nil, "", http.StatusNotImplemented)
   202  		return
   203  	}
   204  
   205  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   206  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   207  		return
   208  	}
   209  
   210  	if _, err := c.App.DeleteScheme(c.Params.SchemeId); err != nil {
   211  		c.Err = err
   212  		return
   213  	}
   214  
   215  	ReturnStatusOK(w)
   216  }