summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-04-22 21:40:51 +0100
committerLauris BH <lauris@nix.lv>2019-04-22 23:40:51 +0300
commitbe666b03eef1e085adc0749837480e0db7f811ad (patch)
treeed8f4bc7c4a5cf2c7a7a37e38aef23b10a83595e /models
parentb83114f1407247415b184f77f8f2f6ecea8cb994 (diff)
downloadgitea-be666b03eef1e085adc0749837480e0db7f811ad.tar.gz
gitea-be666b03eef1e085adc0749837480e0db7f811ad.zip
Trace Logging on Permission Denied & ColorFormat (#6618)
* Add log.ColorFormat and log.ColorFormatted Structs can now implement log.ColorFormatted to provide their own colored format when logged with `%-v` or additional flags. Signed-off-by: Andrew Thornton <art27@cantab.net> * Add basic ColorFormat to repository and user Signed-off-by: Andrew Thornton <art27@cantab.net> * Add basic ColorFormat to access and unit Signed-off-by: Andrew Thornton <art27@cantab.net> * Add ColorFormat to permission and on trace log it Signed-off-by: Andrew Thornton <art27@cantab.net> * Add log.NewColoredIDValue to make ID value coloring consistent Signed-off-by: Andrew Thornton <art27@cantab.net> * formatting changes * Add some better tracing to permission denied for read issues/pulls Signed-off-by: Andrew Thornton <art27@cantab.net> * Add Trace logging on permission denied Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove isTrace() check from deferred func * Adjust repo and allow logging of team * use FormatInt instead of Itoa * Add blank line Signed-off-by: Andrew Thornton <art27@cantab.net> * Update access.go
Diffstat (limited to 'models')
-rw-r--r--models/access.go14
-rw-r--r--models/org_team.go10
-rw-r--r--models/repo.go19
-rw-r--r--models/repo_permission.go61
-rw-r--r--models/unit.go30
-rw-r--r--models/user.go7
6 files changed, 140 insertions, 1 deletions
diff --git a/models/access.go b/models/access.go
index 34d76953f5..3cdfc62f21 100644
--- a/models/access.go
+++ b/models/access.go
@@ -1,10 +1,15 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
+// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
-import "fmt"
+import (
+ "fmt"
+
+ "code.gitea.io/gitea/modules/log"
+)
// AccessMode specifies the users access mode
type AccessMode int
@@ -37,6 +42,13 @@ func (mode AccessMode) String() string {
}
}
+// ColorFormat provides a ColorFormatted version of this AccessMode
+func (mode AccessMode) ColorFormat(s fmt.State) {
+ log.ColorFprintf(s, "%d:%s",
+ log.NewColoredIDValue(mode),
+ mode)
+}
+
// ParseAccessMode returns corresponding access mode to given permission string.
func ParseAccessMode(permission string) AccessMode {
switch permission {
diff --git a/models/org_team.go b/models/org_team.go
index 4e496e2f72..49d06896e5 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -34,6 +34,16 @@ type Team struct {
Units []*TeamUnit `xorm:"-"`
}
+// ColorFormat provides a basic color format for a Team
+func (t *Team) ColorFormat(s fmt.State) {
+ log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
+ log.NewColoredIDValue(t.ID),
+ t.Name,
+ log.NewColoredIDValue(t.OrgID),
+ t.Authorize)
+
+}
+
// GetUnits return a list of available units for a team
func (t *Team) GetUnits() error {
return t.getUnits(x)
diff --git a/models/repo.go b/models/repo.go
index 6069be1243..936ad2ae37 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -18,6 +18,7 @@ import (
"path/filepath"
"regexp"
"sort"
+ "strconv"
"strings"
"time"
@@ -210,6 +211,24 @@ type Repository struct {
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
}
+// ColorFormat returns a colored string to represent this repo
+func (repo *Repository) ColorFormat(s fmt.State) {
+ var ownerName interface{}
+
+ if repo.OwnerName != "" {
+ ownerName = repo.OwnerName
+ } else if repo.Owner != nil {
+ ownerName = repo.Owner.Name
+ } else {
+ ownerName = log.NewColoredIDValue(strconv.FormatInt(repo.OwnerID, 10))
+ }
+
+ log.ColorFprintf(s, "%d:%s/%s",
+ log.NewColoredIDValue(repo.ID),
+ ownerName,
+ repo.Name)
+}
+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (repo *Repository) AfterLoad() {
// FIXME: use models migration to solve all at once.
diff --git a/models/repo_permission.go b/models/repo_permission.go
index eb0628cbd1..b4bd1c30f5 100644
--- a/models/repo_permission.go
+++ b/models/repo_permission.go
@@ -4,6 +4,12 @@
package models
+import (
+ "fmt"
+
+ "code.gitea.io/gitea/modules/log"
+)
+
// Permission contains all the permissions related variables to a repository for a user
type Permission struct {
AccessMode AccessMode
@@ -90,12 +96,67 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
return p.CanWrite(UnitTypeIssues)
}
+// ColorFormat writes a colored string for these Permissions
+func (p *Permission) ColorFormat(s fmt.State) {
+ noColor := log.ColorBytes(log.Reset)
+
+ format := "AccessMode: %-v, %d Units, %d UnitsMode(s): [ "
+ args := []interface{}{
+ p.AccessMode,
+ log.NewColoredValueBytes(len(p.Units), &noColor),
+ log.NewColoredValueBytes(len(p.UnitsMode), &noColor),
+ }
+ if s.Flag('+') {
+ for i, unit := range p.Units {
+ config := ""
+ if unit.Config != nil {
+ configBytes, err := unit.Config.ToDB()
+ config = string(configBytes)
+ if err != nil {
+ config = string(err.Error())
+ }
+ }
+ format += "\nUnits[%d]: ID: %d RepoID: %d Type: %-v Config: %s"
+ args = append(args,
+ log.NewColoredValueBytes(i, &noColor),
+ log.NewColoredIDValue(unit.ID),
+ log.NewColoredIDValue(unit.RepoID),
+ unit.Type,
+ config)
+ }
+ for key, value := range p.UnitsMode {
+ format += "\nUnitMode[%-v]: %-v"
+ args = append(args,
+ key,
+ value)
+ }
+ } else {
+ format += "..."
+ }
+ format += " ]"
+ log.ColorFprintf(s, format, args...)
+}
+
// GetUserRepoPermission returns the user permissions to the repository
func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
return getUserRepoPermission(x, repo, user)
}
func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permission, err error) {
+ if log.IsTrace() {
+ defer func() {
+ if user == nil {
+ log.Trace("Permission Loaded for anonymous user in %-v:\nPermissions: %-+v",
+ repo,
+ perm)
+ return
+ }
+ log.Trace("Permission Loaded for %-v in %-v:\nPermissions: %-+v",
+ user,
+ repo,
+ perm)
+ }()
+ }
// anonymous user visit private repo.
// TODO: anonymous user visit public unit of private repo???
if user == nil && repo.IsPrivate {
diff --git a/models/unit.go b/models/unit.go
index 9619232cf4..a8ddf285da 100644
--- a/models/unit.go
+++ b/models/unit.go
@@ -5,7 +5,10 @@
package models
import (
+ "fmt"
"strings"
+
+ "code.gitea.io/gitea/modules/log"
)
// UnitType is Unit's Type
@@ -22,6 +25,33 @@ const (
UnitTypeExternalTracker // 7 ExternalTracker
)
+func (u UnitType) String() string {
+ switch u {
+ case UnitTypeCode:
+ return "UnitTypeCode"
+ case UnitTypeIssues:
+ return "UnitTypeIssues"
+ case UnitTypePullRequests:
+ return "UnitTypePullRequests"
+ case UnitTypeReleases:
+ return "UnitTypeReleases"
+ case UnitTypeWiki:
+ return "UnitTypeWiki"
+ case UnitTypeExternalWiki:
+ return "UnitTypeExternalWiki"
+ case UnitTypeExternalTracker:
+ return "UnitTypeExternalTracker"
+ }
+ return fmt.Sprintf("Unknown UnitType %d", u)
+}
+
+// ColorFormat provides a ColorFormatted version of this UnitType
+func (u UnitType) ColorFormat(s fmt.State) {
+ log.ColorFprintf(s, "%d:%s",
+ log.NewColoredIDValue(u),
+ u)
+}
+
var (
// allRepUnitTypes contains all the unit types
allRepUnitTypes = []UnitType{
diff --git a/models/user.go b/models/user.go
index 93fdc6f4af..eafc53d6f1 100644
--- a/models/user.go
+++ b/models/user.go
@@ -146,6 +146,13 @@ type User struct {
Theme string `xorm:"NOT NULL DEFAULT ''"`
}
+// ColorFormat writes a colored string to identify this struct
+func (u *User) ColorFormat(s fmt.State) {
+ log.ColorFprintf(s, "%d:%s",
+ log.NewColoredIDValue(u.ID),
+ log.NewColoredValue(u.Name))
+}
+
// BeforeUpdate is invoked from XORM before updating this object.
func (u *User) BeforeUpdate() {
if u.MaxRepoCreation < -1 {