summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/admin.go51
-rw-r--r--api/admin_test.go35
-rw-r--r--api/api.go1
-rw-r--r--api/context.go10
4 files changed, 97 insertions, 0 deletions
diff --git a/api/admin.go b/api/admin.go
new file mode 100644
index 000000000..d4af1d247
--- /dev/null
+++ b/api/admin.go
@@ -0,0 +1,51 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api
+
+import (
+ "bufio"
+ "net/http"
+ "os"
+
+ l4g "code.google.com/p/log4go"
+ "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")
+}
+
+func getLogs(c *Context, w http.ResponseWriter, r *http.Request) {
+
+ if !c.HasSystemAdminPermissions("getLogs") {
+ return
+ }
+
+ var lines []string
+
+ if utils.Cfg.LogSettings.FileEnable {
+
+ file, err := os.Open(utils.Cfg.LogSettings.FileLocation)
+ if err != nil {
+ c.Err = model.NewAppError("getLogs", "Error reading log file", err.Error())
+ }
+
+ 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)))
+}
diff --git a/api/admin_test.go b/api/admin_test.go
new file mode 100644
index 000000000..460ac1208
--- /dev/null
+++ b/api/admin_test.go
@@ -0,0 +1,35 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api
+
+import (
+ "testing"
+
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/store"
+)
+
+func TestGetLogs(t *testing.T) {
+ Setup()
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user.Id))
+
+ c := &Context{}
+ c.RequestId = model.NewId()
+ c.IpAddress = "cmd_line"
+ UpdateRoles(c, user, model.ROLE_SYSTEM_ADMIN)
+
+ Client.LoginByEmail(team.Name, user.Email, "pwd")
+
+ if logs, err := Client.GetLogs(); err != nil {
+ t.Fatal(err)
+ } else if len(logs.Data.([]string)) <= 0 {
+ t.Fatal()
+ }
+}
diff --git a/api/api.go b/api/api.go
index 9770930f7..35ac0bdc0 100644
--- a/api/api.go
+++ b/api/api.go
@@ -41,6 +41,7 @@ func InitApi() {
InitFile(r)
InitCommand(r)
InitConfig(r)
+ InitAdmin(r)
templatesDir := utils.FindDir("api/templates")
l4g.Debug("Parsing server templates at %v", templatesDir)
diff --git a/api/context.go b/api/context.go
index d97295e5e..fc7d8f23d 100644
--- a/api/context.go
+++ b/api/context.go
@@ -295,6 +295,16 @@ func (c *Context) IsSystemAdmin() bool {
return false
}
+func (c *Context) HasSystemAdminPermissions(where string) bool {
+ if c.IsSystemAdmin() {
+ return true
+ }
+
+ c.Err = model.NewAppError(where, "You do not have the appropriate permissions", "userId="+c.Session.UserId)
+ c.Err.StatusCode = http.StatusForbidden
+ return false
+}
+
func (c *Context) IsTeamAdmin(userId string) bool {
if uresult := <-Srv.Store.User().Get(userId); uresult.Err != nil {
c.Err = uresult.Err