summaryrefslogtreecommitdiffstats
path: root/models/repo_permission.go
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/repo_permission.go
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/repo_permission.go')
-rw-r--r--models/repo_permission.go61
1 files changed, 61 insertions, 0 deletions
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 {