From 9f9cd6bfc61d82ee0a3d31cee112be7975b8ca86 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 07:50:26 -0400 Subject: Work on admin --- modules/middleware/auth.go | 14 +++++++++++++- modules/middleware/context.go | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index d45a21e9..b67f766b 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -20,7 +20,7 @@ func SignInRequire(redirect bool) martini.Handler { return } else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm { ctx.Data["Title"] = "Activate Your Account" - ctx.Render.HTML(200, "user/active", ctx.Data) + ctx.HTML(200, "user/active") return } } @@ -31,6 +31,18 @@ func SignOutRequire() martini.Handler { return func(ctx *Context) { if ctx.IsSigned { ctx.Redirect("/") + return + } + } +} + +// AdminRequire requires user signed in as administor. +func AdminRequire() martini.Handler { + return func(ctx *Context) { + if ctx.User.LowerName != base.AdminName && !ctx.User.IsAdmin { + ctx.Error(403) + return } + ctx.Data["PageIsAdmin"] = true } } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 6ac87de3..744cdfc1 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -14,6 +14,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" ) @@ -61,24 +62,29 @@ func (ctx *Context) HasError() bool { return hasErr.(bool) } +// HTML calls render.HTML underlying but reduce one argument. +func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) { + ctx.Render.HTML(status, name, ctx.Data, htmlOpt...) +} + // RenderWithErr used for page has form validation but need to prompt error to users. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { ctx.Data["HasError"] = true ctx.Data["ErrorMsg"] = msg auth.AssignForm(form, ctx.Data) - ctx.HTML(200, tpl, ctx.Data) + ctx.HTML(200, tpl) } // Handle handles and logs error by given status. func (ctx *Context) Handle(status int, title string, err error) { log.Error("%s: %v", title, err) if martini.Dev == martini.Prod { - ctx.HTML(500, "status/500", ctx.Data) + ctx.HTML(500, "status/500") return } ctx.Data["ErrorMsg"] = err - ctx.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data) + ctx.HTML(status, fmt.Sprintf("status/%d", status)) } // InitContext initializes a classic context for a request. @@ -106,6 +112,10 @@ func InitContext() martini.Handler { ctx.Data["SignedUser"] = user ctx.Data["SignedUserId"] = user.Id ctx.Data["SignedUserName"] = user.LowerName + + if ctx.User.IsAdmin || ctx.User.LowerName == base.AdminName { + ctx.Data["IsAdmin"] = true + } } ctx.Data["PageStartTime"] = time.Now() -- cgit v1.2.3 From 4cf6cc63b0679aaf5fe8b74a2aaf0bd92b1f12d3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 08:02:14 -0400 Subject: Work on admin --- conf/app.ini | 4 ---- models/user.go | 8 +++++++- modules/base/conf.go | 2 -- modules/middleware/auth.go | 2 +- modules/middleware/context.go | 6 +----- routers/user/user.go | 2 +- 6 files changed, 10 insertions(+), 14 deletions(-) (limited to 'modules/middleware') diff --git a/conf/app.ini b/conf/app.ini index 21090ceb..658f7c01 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -27,10 +27,6 @@ PASSWD = ; For "postgres" only, either "disable", "require" or "verify-full" SSL_MODE = disable -[admin] -; Administor's name, which should be same as the user name you want to authorize -NAME = admin - [security] ; !!CHANGE THIS TO KEEP YOUR USER DATA SAFE!! SECRET_KEY = !#@FDEWREWR&*( diff --git a/models/user.go b/models/user.go index 8f74fd53..fd89af6b 100644 --- a/models/user.go +++ b/models/user.go @@ -137,7 +137,13 @@ func RegisterUser(user *User) (*User, error) { } return nil, err } - return user, nil + + if user.Id == 1 { + user.IsAdmin = true + user.IsActive = true + _, err = orm.Id(user.Id).UseBool().Update(user) + } + return user, err } // get user by erify code diff --git a/modules/base/conf.go b/modules/base/conf.go index c904c5b3..fdbf3ad3 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -32,7 +32,6 @@ var ( AppUrl string Domain string SecretKey string - AdminName string Cfg *goconfig.ConfigFile MailService *Mailer ) @@ -174,7 +173,6 @@ func init() { AppUrl = Cfg.MustValue("server", "ROOT_URL") Domain = Cfg.MustValue("server", "DOMAIN") SecretKey = Cfg.MustValue("security", "SECRET_KEY") - AdminName = strings.ToLower(Cfg.MustValue("admin", "NAME")) } func NewServices() { diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index b67f766b..44033abb 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -39,7 +39,7 @@ func SignOutRequire() martini.Handler { // AdminRequire requires user signed in as administor. func AdminRequire() martini.Handler { return func(ctx *Context) { - if ctx.User.LowerName != base.AdminName && !ctx.User.IsAdmin { + if !ctx.User.IsAdmin { ctx.Error(403) return } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 744cdfc1..cb3cbabc 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -14,7 +14,6 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" ) @@ -112,10 +111,7 @@ func InitContext() martini.Handler { ctx.Data["SignedUser"] = user ctx.Data["SignedUserId"] = user.Id ctx.Data["SignedUserName"] = user.LowerName - - if ctx.User.IsAdmin || ctx.User.LowerName == base.AdminName { - ctx.Data["IsAdmin"] = true - } + ctx.Data["IsAdmin"] = ctx.User.IsAdmin } ctx.Data["PageStartTime"] = time.Now() diff --git a/routers/user/user.go b/routers/user/user.go index 2b759e41..be2c4d38 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -153,7 +153,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName)) // Send confirmation e-mail. - if base.Service.RegisterEmailConfirm { + if base.Service.RegisterEmailConfirm && u.Id > 1 { mailer.SendRegisterMail(ctx.Render, u) ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email -- cgit v1.2.3 From e8a49c7746f3d93688d83401c69b0f4936c25783 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 08:25:41 -0400 Subject: Add data to ui --- modules/middleware/repo.go | 1 + templates/repo/single_bare.tmpl | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index b25c9423..62c67bce 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -70,6 +70,7 @@ func RepoAssignment(redirect bool) martini.Handler { } ctx.Repo.Repository = repo ctx.Repo.CloneLink.SSH = fmt.Sprintf("git@%s:%s/%s.git", base.Domain, user.LowerName, repo.LowerName) + ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("https://%s/%s/%s.git", base.Domain, user.LowerName, repo.LowerName) ctx.Data["IsRepositoryValid"] = true ctx.Data["Repository"] = repo diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index caf2ef74..999ff557 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -6,8 +6,8 @@

Clone this repository

- - + + @@ -20,12 +20,12 @@
touch README.md
 git init
 git add README.md
-git commit -m "first commit"
-git remote add origin https://github.com/fuxiaohei/air.git
+git commit -m "first commit" {{.CloneLink.SSH}}
+git remote add origin {{.CloneLink.HTTPS}}
 git push -u origin master

Push an existing repository from the command line

-
git remote add origin https://github.com/fuxiaohei/air.git
+        
git remote add origin {{.CloneLink.HTTPS}}
 git push -u origin master
\ No newline at end of file -- cgit v1.2.3 From 42b08ff26158b9c815d13a427c596bd76468beb8 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Thu, 20 Mar 2014 21:28:12 +0800 Subject: fix single bare page link --- modules/middleware/repo.go | 1 + public/js/app.js | 46 +++++++++++++++++++++-------------------- templates/repo/nav.tmpl | 2 +- templates/repo/single_bare.tmpl | 4 ++-- 4 files changed, 28 insertions(+), 25 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 62c67bce..a9a90e3f 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -79,5 +79,6 @@ func RepoAssignment(redirect bool) martini.Handler { ctx.Data["CloneLink"] = ctx.Repo.CloneLink ctx.Data["RepositoryLink"] = ctx.Data["Title"] ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner + ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching } } diff --git a/public/js/app.js b/public/js/app.js index 93cfbc1f..64cc980f 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -72,18 +72,18 @@ var Gogits = { prettyPrint(); var $lineNums = $pre.parent().siblings('.lines-num'); - if($lineNums.length > 0){ + if ($lineNums.length > 0) { var nums = $pre.find('ol.linenums > li').length; - for(var i=0;i < nums;i++){ - $lineNums.append(''+(i+1)+''); + for (var i = 1; i <= nums; i++) { + $lineNums.append('' + i + ''); } var last; - $(document).on('click', '.lines-num span', function(){ + $(document).on('click', '.lines-num span', function () { var $e = $(this); console.log($e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel'))); console.log('ol.linenums > ' + $e.attr('rel')); - if(last){ + if (last) { last.removeClass('active'); } last = $e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel')); @@ -98,12 +98,12 @@ var Gogits = { var node = $(this); var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-')); var name = val; - if(headers[val] > 0){ + if (headers[val] > 0) { name = val + '-' + headers[val]; } - if(headers[val] == undefined){ + if (headers[val] == undefined) { headers[val] = 1; - }else{ + } else { headers[val] += 1; } node = node.wrap('
'); @@ -183,20 +183,22 @@ function initUserSetting() { } function initRepository() { - var $guide = $('.guide-box'); - if ($guide.length) { - var $url = $('#guide-clone-url'); - $guide.find('button[data-url]').on("click",function () { - var $this = $(this); - if (!$this.hasClass('btn-primary')) { - $guide.find('.btn-primary').removeClass('btn-primary').addClass("btn-default"); - $(this).addClass('btn-primary').removeClass('btn-default'); - $url.val($this.data("url")); - $guide.find('span.clone-url').text($this.data('url')); - } - }).eq(0).trigger("click"); - // todo copy to clipboard - } + (function () { + var $guide = $('.guide-box'); + if ($guide.length) { + var $url = $('#guide-clone-url'); + $guide.find('button[data-url]').on("click",function () { + var $this = $(this); + if (!$this.hasClass('btn-primary')) { + $guide.find('.btn-primary').removeClass('btn-primary').addClass("btn-default"); + $(this).addClass('btn-primary').removeClass('btn-default'); + $url.val($this.data("url")); + $guide.find('span.clone-url').text($this.data('url')); + } + }).eq(0).trigger("click"); + // todo copy to clipboard + } + })(); } (function ($) { diff --git a/templates/repo/nav.tmpl b/templates/repo/nav.tmpl index 92e529db..e8685b08 100644 --- a/templates/repo/nav.tmpl +++ b/templates/repo/nav.tmpl @@ -13,7 +13,7 @@ -
+
- + + -- cgit v1.2.3 From 5373a3093eaf9bc9ced7a6b3335ccf1b17fd343e Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Mar 2014 01:59:15 -0400 Subject: config option: Require sign in to view repository --- conf/app.ini | 2 ++ modules/base/conf.go | 2 ++ modules/middleware/auth.go | 2 +- web.go | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) (limited to 'modules/middleware') diff --git a/conf/app.ini b/conf/app.ini index d38cd1f0..d4fdc0dc 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -42,6 +42,8 @@ RESET_PASSWD_CODE_LIVE_MINUTES = 180 REGISTER_EMAIL_CONFIRM = false ; Does not allow register and admin create account only DISENABLE_REGISTERATION = false +; User must sign in to view anything. +REQUIRE_SIGNIN_VIEW = false [mailer] ENABLED = false diff --git a/modules/base/conf.go b/modules/base/conf.go index 42d50da4..3050b915 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -41,6 +41,7 @@ var ( var Service struct { RegisterEmailConfirm bool DisenableRegisteration bool + RequireSignInView bool ActiveCodeLives int ResetPwdCodeLives int } @@ -70,6 +71,7 @@ func newService() { Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180) Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180) Service.DisenableRegisteration = Cfg.MustBool("service", "DISENABLE_REGISTERATION", false) + Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW", false) } func newLogService() { diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index 44033abb..f211de32 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -15,7 +15,7 @@ func SignInRequire(redirect bool) martini.Handler { return func(ctx *Context) { if !ctx.IsSigned { if redirect { - ctx.Redirect("/") + ctx.Redirect("/user/login") } return } else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm { diff --git a/web.go b/web.go index 648cb9d7..6fe838aa 100644 --- a/web.go +++ b/web.go @@ -87,7 +87,8 @@ func runWeb(*cli.Context) { m.Use(middleware.InitContext()) - reqSignIn, ignSignIn := middleware.SignInRequire(true), middleware.SignInRequire(false) + reqSignIn := middleware.SignInRequire(true) + ignSignIn := middleware.SignInRequire(base.Service.RequireSignInView) reqSignOut := middleware.SignOutRequire() // Routers. m.Get("/", ignSignIn, routers.Home) -- cgit v1.2.3 From c7e0d3a499941a4cbd7814e30308c8f3c6e45543 Mon Sep 17 00:00:00 2001 From: slene Date: Fri, 21 Mar 2014 21:06:47 +0800 Subject: add cache --- conf/app.ini | 4 ++++ modules/base/conf.go | 16 ++++++++++++++++ modules/middleware/context.go | 3 +++ 3 files changed, 23 insertions(+) (limited to 'modules/middleware') diff --git a/conf/app.ini b/conf/app.ini index 985903a8..71fe81e8 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -60,6 +60,10 @@ FROM = USER = PASSWD = +[cache] +ADAPTER = memory +CONFIG = + [log] ; Either "console", "file", "conn" or "smtp", default is "console" MODE = console diff --git a/modules/base/conf.go b/modules/base/conf.go index bf054ec3..3962972c 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -15,6 +15,8 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/goconfig" + "github.com/gogits/cache" + "github.com/gogits/gogs/modules/log" ) @@ -37,6 +39,10 @@ var ( Cfg *goconfig.ConfigFile MailService *Mailer + + Cache cache.Cache + CacheAdapter string + CacheConfig string ) var Service struct { @@ -182,6 +188,16 @@ func NewConfigContext() { SecretKey = Cfg.MustValue("security", "SECRET_KEY") RunUser = Cfg.MustValue("", "RUN_USER") + CacheAdapter = Cfg.MustValue("cache", "ADAPTER") + CacheConfig = Cfg.MustValue("cache", "CONFIG") + + Cache, err = cache.NewCache(CacheAdapter, CacheConfig) + if err != nil { + fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n", + CacheAdapter, CacheConfig, err) + os.Exit(2) + } + // Determine and create root git reposiroty path. RepoRootPath = Cfg.MustValue("repository", "ROOT") if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { diff --git a/modules/middleware/context.go b/modules/middleware/context.go index cb3cbabc..da051918 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -12,6 +12,8 @@ import ( "github.com/codegangsta/martini" "github.com/martini-contrib/sessions" + "github.com/gogits/cache" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/log" @@ -25,6 +27,7 @@ type Context struct { Req *http.Request Res http.ResponseWriter Session sessions.Session + Cache cache.Cache User *models.User IsSigned bool -- cgit v1.2.3 From 770c78a177c64ba2014f4152da95f0899495772a Mon Sep 17 00:00:00 2001 From: slene Date: Fri, 21 Mar 2014 21:31:47 +0800 Subject: fix --- modules/middleware/context.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'modules/middleware') diff --git a/modules/middleware/context.go b/modules/middleware/context.go index da051918..a25a3dbb 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" ) @@ -100,6 +101,7 @@ func InitContext() martini.Handler { Req: r, Res: res, Session: session, + Cache: base.Cache, Render: rd, } -- cgit v1.2.3