diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2014-04-10 22:22:18 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2014-04-10 22:22:18 +0800 |
commit | 88d873c67f2940325a2a7a8aa851372c9afb983d (patch) | |
tree | 53908c92ebc0dbce9ae935b72eb4b959d55a587c /routers | |
parent | 94c7278194694fec728b518d4390b03ba6c237a4 (diff) | |
parent | 6b30d9b0f27a8cb0961d52980a8e655d9a0bbda2 (diff) | |
download | gitea-88d873c67f2940325a2a7a8aa851372c9afb983d.tar.gz gitea-88d873c67f2940325a2a7a8aa851372c9afb983d.zip |
Merge branch 'dev' of github.com:gogits/gogs into dev
Diffstat (limited to 'routers')
-rw-r--r-- | routers/install.go | 7 | ||||
-rw-r--r-- | routers/repo/repo.go | 31 | ||||
-rw-r--r-- | routers/user/social.go | 99 | ||||
-rw-r--r-- | routers/user/user.go | 14 |
4 files changed, 120 insertions, 31 deletions
diff --git a/routers/install.go b/routers/install.go index 1c4e6181d5..5d6c65ef9b 100644 --- a/routers/install.go +++ b/routers/install.go @@ -7,6 +7,7 @@ package routers import ( "errors" "os" + "os/exec" "strings" "github.com/Unknwon/goconfig" @@ -27,6 +28,7 @@ func checkRunMode() { switch base.Cfg.MustValue("", "RUN_MODE") { case "prod": martini.Env = martini.Prod + base.IsProdMode = true case "test": martini.Env = martini.Test } @@ -102,6 +104,11 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { return } + if _, err := exec.LookPath("git"); err != nil { + ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), "install", &form) + return + } + // Pass basic check, now test configuration. // Test database setting. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6b002f6c81..d4d52ba0d7 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -53,6 +53,36 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Handle(200, "repo.Create", err) } +func Mirror(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = "Mirror repository" + ctx.Data["PageIsNewRepo"] = true // For navbar arrow. + + if ctx.Req.Method == "GET" { + ctx.HTML(200, "repo/mirror") + return + } + + if ctx.HasError() { + ctx.HTML(200, "repo/mirror") + return + } + + _, err := models.CreateRepository(ctx.User, form.RepoName, form.Description, + "", form.License, form.Visibility == "private", false) + if err == nil { + log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, form.RepoName) + ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) + return + } else if err == models.ErrRepoAlreadyExist { + ctx.RenderWithErr("Repository name has already been used", "repo/mirror", &form) + return + } else if err == models.ErrRepoNameIllegal { + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/mirror", &form) + return + } + ctx.Handle(200, "repo.Mirror", err) +} + func Single(ctx *middleware.Context, params martini.Params) { branchName := ctx.Repo.BranchName commitId := ctx.Repo.CommitId @@ -312,6 +342,7 @@ func SettingPost(ctx *middleware.Context) { ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") + ctx.Repo.Repository.IsGoget = ctx.Query("goget") == "on" if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { ctx.Handle(404, "repo.SettingPost(update)", err) return diff --git a/routers/user/social.go b/routers/user/social.go index 08cfcd83f2..b87c313f5d 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -6,7 +6,10 @@ package user import ( "encoding/json" + "net/http" + "net/url" "strconv" + "strings" "code.google.com/p/goauth2/oauth" @@ -70,53 +73,87 @@ func (s *SocialGithub) Update() error { return json.NewDecoder(r.Body).Decode(&s.data) } +func extractPath(next string) string { + n, err := url.Parse(next) + if err != nil { + return "/" + } + return n.Path +} + // github && google && ... func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { - gh := &SocialGithub{ - WebToken: &oauth.Token{ - AccessToken: tokens.Access(), - RefreshToken: tokens.Refresh(), - Expiry: tokens.ExpiryTime(), - Extra: tokens.ExtraData(), - }, + var socid int64 + var ok bool + next := extractPath(ctx.Query("next")) + log.Debug("social signed check %s", next) + if socid, ok = ctx.Session.Get("socialId").(int64); ok && socid != 0 { + // already login + ctx.Redirect(next) + log.Info("login soc id: %v", socid) + return + } + config := &oauth.Config{ + //ClientId: base.OauthService.Github.ClientId, + //ClientSecret: base.OauthService.Github.ClientSecret, // FIXME: I don't know why compile error here + ClientId: "09383403ff2dc16daaa1", + ClientSecret: "0e4aa0c3630df396cdcea01a9d45cacf79925fea", + RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI(), + Scope: base.OauthService.GitHub.Scopes, + AuthURL: "https://github.com/login/oauth/authorize", + TokenURL: "https://github.com/login/oauth/access_token", + } + transport := &oauth.Transport{ + Config: config, + Transport: http.DefaultTransport, } - if len(tokens.Access()) == 0 { - log.Error("empty access") + code := ctx.Query("code") + if code == "" { + // redirect to social login page + ctx.Redirect(config.AuthCodeURL(next)) return } - var err error - var u *models.User + + // handle call back + tk, err := transport.Exchange(code) + if err != nil { + log.Error("oauth2 handle callback error: %v", err) + return // FIXME, need error page 501 + } + next = extractPath(ctx.Query("state")) + log.Debug("success token: %v", tk) + + gh := &SocialGithub{WebToken: tk} if err = gh.Update(); err != nil { - // FIXME: handle error page + // FIXME: handle error page 501 log.Error("connect with github error: %s", err) return } var soc SocialConnector = gh log.Info("login: %s", soc.Name()) - // FIXME: login here, user email to check auth, if not registe, then generate a uniq username - if u, err = models.GetOauth2User(soc.Identity()); err != nil { - u = &models.User{ - Name: soc.Name(), - Email: soc.Email(), - Passwd: "123456", - IsActive: !base.Service.RegisterEmailConfirm, - } - if u, err = models.RegisterUser(u); err != nil { - log.Error("register user: %v", err) - return - } - oa := &models.Oauth2{} - oa.Uid = u.Id + oa, err := models.GetOauth2(soc.Identity()) + switch err { + case nil: + ctx.Session.Set("userId", oa.User.Id) + ctx.Session.Set("userName", oa.User.Name) + case models.ErrOauth2RecordNotExists: + oa = &models.Oauth2{} + oa.Uid = 0 oa.Type = soc.Type() oa.Token = soc.Token() oa.Identity = soc.Identity() - log.Info("oa: %v", oa) + log.Debug("oa: %v", oa) if err = models.AddOauth2(oa); err != nil { - log.Error("add oauth2 %v", err) + log.Error("add oauth2 %v", err) // 501 return } + case models.ErrOauth2NotAssociatedWithUser: + // ignore it. judge in /usr/login page + default: + log.Error(err.Error()) // FIXME: handle error page + return } - ctx.Session.Set("userId", u.Id) - ctx.Session.Set("userName", u.Name) - ctx.Redirect("/") + ctx.Session.Set("socialId", oa.Id) + log.Debug("socialId: %v", oa.Id) + ctx.Redirect(next) } diff --git a/routers/user/user.go b/routers/user/user.go index f6a39b86c7..084d0bbde2 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -396,6 +396,10 @@ func Activate(ctx *middleware.Context) { } else { ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 mailer.SendActiveMail(ctx.Render, ctx.User) + + if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { + log.Error("Set cache(MailResendLimit) fail: %v", err) + } } } else { ctx.Data["ServiceNotEnabled"] = true @@ -451,7 +455,17 @@ func ForgotPasswd(ctx *middleware.Context) { return } + if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { + ctx.Data["ResendLimited"] = true + ctx.HTML(200, "user/forgot_passwd") + return + } + mailer.SendResetPasswdMail(ctx.Render, u) + if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + log.Error("Set cache(MailResendLimit) fail: %v", err) + } + ctx.Data["Email"] = email ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 ctx.Data["IsResetSent"] = true |