From e06e292be71ca699d90bafbd635118aa47c2d7a5 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Thu, 10 Sep 2015 18:32:22 -0700 Subject: PLT-12 adding log viewer --- api/admin.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ api/admin_test.go | 35 +++++++++++++++++++++++++++++++++++ api/api.go | 1 + api/context.go | 10 ++++++++++ 4 files changed, 97 insertions(+) create mode 100644 api/admin.go create mode 100644 api/admin_test.go (limited to 'api') 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 1852ed4d6..ea5677f95 100644 --- a/api/context.go +++ b/api/context.go @@ -292,6 +292,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 -- cgit v1.2.3-1-g7c22