github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/elasticsearch.go (about)

     1  // Copyright (c) 2017-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) InitElasticsearch() {
    13  	api.BaseRoutes.Elasticsearch.Handle("/test", api.ApiSessionRequired(testElasticsearch)).Methods("POST")
    14  	api.BaseRoutes.Elasticsearch.Handle("/purge_indexes", api.ApiSessionRequired(purgeElasticsearchIndexes)).Methods("POST")
    15  }
    16  
    17  func testElasticsearch(c *Context, w http.ResponseWriter, r *http.Request) {
    18  	cfg := model.ConfigFromJson(r.Body)
    19  	if cfg == nil {
    20  		cfg = c.App.Config()
    21  	}
    22  
    23  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    24  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    25  		return
    26  	}
    27  
    28  	if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
    29  		c.Err = model.NewAppError("testElasticsearch", "api.restricted_system_admin", nil, "", http.StatusForbidden)
    30  		return
    31  	}
    32  
    33  	if err := c.App.TestElasticsearch(cfg); err != nil {
    34  		c.Err = err
    35  		return
    36  	}
    37  
    38  	ReturnStatusOK(w)
    39  }
    40  
    41  func purgeElasticsearchIndexes(c *Context, w http.ResponseWriter, r *http.Request) {
    42  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    43  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    44  		return
    45  	}
    46  
    47  	if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
    48  		c.Err = model.NewAppError("purgeElasticsearchIndexes", "api.restricted_system_admin", nil, "", http.StatusForbidden)
    49  		return
    50  	}
    51  
    52  	if err := c.App.PurgeElasticsearchIndexes(); err != nil {
    53  		c.Err = err
    54  		return
    55  	}
    56  
    57  	ReturnStatusOK(w)
    58  }