diff options
author | =Corey Hulen <corey@hulen.com> | 2015-09-10 18:32:22 -0700 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-09-10 18:32:22 -0700 |
commit | e06e292be71ca699d90bafbd635118aa47c2d7a5 (patch) | |
tree | 3f8f7ce86a1618f625e71084041746745139ba0c /api | |
parent | 41439eb801c6c8c0a55bcada3eeba3b4a561c663 (diff) | |
download | chat-e06e292be71ca699d90bafbd635118aa47c2d7a5.tar.gz chat-e06e292be71ca699d90bafbd635118aa47c2d7a5.tar.bz2 chat-e06e292be71ca699d90bafbd635118aa47c2d7a5.zip |
PLT-12 adding log viewer
Diffstat (limited to 'api')
-rw-r--r-- | api/admin.go | 51 | ||||
-rw-r--r-- | api/admin_test.go | 35 | ||||
-rw-r--r-- | api/api.go | 1 | ||||
-rw-r--r-- | api/context.go | 10 |
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 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 |