github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/api4/elasticsearch.go (about)

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