admin.go 4.97 KB
Newer Older
1
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7 8 9
// See License.txt for license information.

package api

import (
	"bufio"
	"net/http"
	"os"
10
	"strings"
=Corey Hulen's avatar
=Corey Hulen committed
11

=Corey Hulen's avatar
=Corey Hulen committed
12
	l4g "github.com/alecthomas/log4go"
=Corey Hulen's avatar
=Corey Hulen committed
13 14 15 16 17 18 19 20 21 22 23
	"github.com/gorilla/mux"

	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"
)

func InitAdmin(r *mux.Router) {
	l4g.Debug("Initializing admin api routes")

	sr := r.PathPrefix("/admin").Subrouter()
	sr.Handle("/logs", ApiUserRequired(getLogs)).Methods("GET")
24 25
	sr.Handle("/config", ApiUserRequired(getConfig)).Methods("GET")
	sr.Handle("/save_config", ApiUserRequired(saveConfig)).Methods("POST")
=Corey Hulen's avatar
=Corey Hulen committed
26
	sr.Handle("/test_email", ApiUserRequired(testEmail)).Methods("POST")
27
	sr.Handle("/client_props", ApiAppHandler(getClientConfig)).Methods("GET")
28
	sr.Handle("/log_client", ApiAppHandler(logClient)).Methods("POST")
=Corey Hulen's avatar
=Corey Hulen committed
29
	sr.Handle("/analytics/{id:[A-Za-z0-9]+}/{name:[A-Za-z0-9_]+}", ApiAppHandler(getAnalytics)).Methods("GET")
=Corey Hulen's avatar
=Corey Hulen committed
30 31 32 33 34 35 36 37 38 39
}

func getLogs(c *Context, w http.ResponseWriter, r *http.Request) {

	if !c.HasSystemAdminPermissions("getLogs") {
		return
	}

	var lines []string

40
	if utils.Cfg.LogSettings.EnableFile {
=Corey Hulen's avatar
=Corey Hulen committed
41

42
		file, err := os.Open(utils.GetLogFileLocation(utils.Cfg.LogSettings.FileLocation))
=Corey Hulen's avatar
=Corey Hulen committed
43
		if err != nil {
44
			c.Err = model.NewAppError("getLogs", c.T("api.admin.file_read_error"), err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58
		}

		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			lines = append(lines, scanner.Text())
		}
	} else {
		lines = append(lines, "")
	}

	w.Write([]byte(model.ArrayToJson(lines)))
}
59

60 61
func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(model.MapToJson(utils.ClientCfg)))
62
}
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
func logClient(c *Context, w http.ResponseWriter, r *http.Request) {
	m := model.MapFromJson(r.Body)

	lvl := m["level"]
	msg := m["message"]

	if len(msg) > 400 {
		msg = msg[0:399]
	}

	if lvl == "ERROR" {
		err := model.NewAppError("client", msg, "")
		c.LogError(err)
	}

	rm := make(map[string]string)
	rm["SUCCESS"] = "true"
	w.Write([]byte(model.MapToJson(rm)))
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
func getConfig(c *Context, w http.ResponseWriter, r *http.Request) {
	if !c.HasSystemAdminPermissions("getConfig") {
		return
	}

	json := utils.Cfg.ToJson()
	cfg := model.ConfigFromJson(strings.NewReader(json))
	json = cfg.ToJson()

	w.Write([]byte(json))
}

func saveConfig(c *Context, w http.ResponseWriter, r *http.Request) {
	if !c.HasSystemAdminPermissions("getConfig") {
		return
	}

	cfg := model.ConfigFromJson(r.Body)
	if cfg == nil {
		c.SetInvalidParam("saveConfig", "config")
		return
	}

107 108
	cfg.SetDefaults()

109 110
	if err := cfg.IsValid(); err != nil {
		c.Err = err
111 112 113 114 115 116 117 118
		return
	}

	utils.SaveConfig(utils.CfgFileName, cfg)
	utils.LoadConfig(utils.CfgFileName)
	json := utils.Cfg.ToJson()
	w.Write([]byte(json))
}
=Corey Hulen's avatar
=Corey Hulen committed
119 120 121 122 123 124 125 126 127 128 129 130

func testEmail(c *Context, w http.ResponseWriter, r *http.Request) {
	if !c.HasSystemAdminPermissions("testEmail") {
		return
	}

	cfg := model.ConfigFromJson(r.Body)
	if cfg == nil {
		c.SetInvalidParam("testEmail", "config")
		return
	}

131
	if result := <-Srv.Store.User().Get(c.T, c.Session.UserId); result.Err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
132 133 134 135 136 137 138 139 140 141 142 143 144
		c.Err = result.Err
		return
	} else {
		if err := utils.SendMailUsingConfig(result.Data.(*model.User).Email, "Mattermost - Testing Email Settings", "<br/><br/><br/>It appears your Mattermost email is setup correctly!", cfg); err != nil {
			c.Err = err
			return
		}
	}

	m := make(map[string]string)
	m["SUCCESS"] = "true"
	w.Write([]byte(model.MapToJson(m)))
}
=Corey Hulen's avatar
=Corey Hulen committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) {
	if !c.HasSystemAdminPermissions("getAnalytics") {
		return
	}

	params := mux.Vars(r)
	teamId := params["id"]
	name := params["name"]

	if name == "standard" {
		var rows model.AnalyticsRows = make([]*model.AnalyticsRow, 3)
		rows[0] = &model.AnalyticsRow{"channel_open_count", 0}
		rows[1] = &model.AnalyticsRow{"channel_private_count", 0}
		rows[2] = &model.AnalyticsRow{"post_count", 0}
160 161
		openChan := Srv.Store.Channel().AnalyticsTypeCount(c.T, teamId, model.CHANNEL_OPEN)
		privateChan := Srv.Store.Channel().AnalyticsTypeCount(c.T, teamId, model.CHANNEL_PRIVATE)
162
		postChan := Srv.Store.Post().AnalyticsPostCount(c.T, teamId)
=Corey Hulen's avatar
=Corey Hulen committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

		if r := <-openChan; r.Err != nil {
			c.Err = r.Err
			return
		} else {
			rows[0].Value = float64(r.Data.(int64))
		}

		if r := <-privateChan; r.Err != nil {
			c.Err = r.Err
			return
		} else {
			rows[1].Value = float64(r.Data.(int64))
		}

		if r := <-postChan; r.Err != nil {
			c.Err = r.Err
			return
		} else {
			rows[2].Value = float64(r.Data.(int64))
		}

		w.Write([]byte(rows.ToJson()))
	} else if name == "post_counts_day" {
187
		if r := <-Srv.Store.Post().AnalyticsPostCountsByDay(c.T, teamId); r.Err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
188 189 190 191 192 193
			c.Err = r.Err
			return
		} else {
			w.Write([]byte(r.Data.(model.AnalyticsRows).ToJson()))
		}
	} else if name == "user_counts_with_posts_day" {
194
		if r := <-Srv.Store.Post().AnalyticsUserCountsWithPostsByDay(c.T, teamId); r.Err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
195 196 197 198 199 200 201 202 203 204
			c.Err = r.Err
			return
		} else {
			w.Write([]byte(r.Data.(model.AnalyticsRows).ToJson()))
		}
	} else {
		c.SetInvalidParam("getAnalytics", "name")
	}

}