diff --git a/.fswatch.json b/.fswatch.json index 4ef36ce47..7b12022c1 100644 --- a/.fswatch.json +++ b/.fswatch.json @@ -8,6 +8,5 @@ ], "env": { "POWERED_BY": "github.com/shxsun/fswatch" - }, - "enable-restart": false + } } diff --git a/gogs.go b/gogs.go index 4616141e3..45be7e871 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.2.0408 Alpha" +const APP_VER = "0.2.2.0409 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/oauth2.go b/models/oauth2.go index cde57b87b..45728b0d5 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -44,5 +44,6 @@ func GetOauth2(identity string) (oa *Oauth2, err error) { } else if oa.Uid == 0 { return oa, ErrOauth2NotAssociatedWithUser } - return GetUserById(oa.Uid) + oa.User, err = GetUserById(oa.Uid) + return oa, err } diff --git a/public/css/gogs.css b/public/css/gogs.css index da2a7fd1a..dcfb2758a 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -309,6 +309,18 @@ html, body { height: 8em; } +#repo-import-auth{ + width: 100%; + margin-top: 48px; + box-sizing: border-box; +} + +#repo-import-auth .form-group{ + box-sizing: border-box; + margin-left: 0; + margin-right: 0; +} + /* gogits user setting */ #user-setting-nav > h4, #user-setting-container > h4, #user-setting-container > div > h4, @@ -444,6 +456,32 @@ html, body { margin-right: 1em; } +#user-dashboard-repo-new .btn-sm.dropdown-toggle { + padding: 3px 8px; +} + +#user-dashboard-repo-new .dropdown-menu { + padding: 0; + margin: 0; +} + +#user-dashboard-repo-new ul { + margin: 0; + width: 200px; +} + +#user-dashboard-repo-new li a { + line-height: 36px; + display: block; + padding: 0 18px; + color: #444; +} + +#user-dashboard-repo-new li a:hover { + background: #0093c4; + color: #FFF; +} + /* gogits repo single page */ #body-nav.repo-nav { @@ -1372,6 +1410,6 @@ html, body { margin: 16px 0; } -#release-preview{ +#release-preview { margin: 6px 0; } \ No newline at end of file diff --git a/routers/repo/repo.go b/routers/repo/repo.go index d223600c5..0ab1c9e42 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -55,6 +55,38 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Handle(200, "repo.Create", err) } +func Import(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = "Import repository" + ctx.Data["PageIsNewRepo"] = true // For navbar arrow. + ctx.Data["LanguageIgns"] = models.LanguageIgns + ctx.Data["Licenses"] = models.Licenses + + if ctx.Req.Method == "GET" { + ctx.HTML(200, "repo/import") + return + } + + if ctx.HasError() { + ctx.HTML(200, "repo/import") + return + } + + _, err := models.CreateRepository(ctx.User, form.RepoName, form.Description, + form.Language, form.License, form.Visibility == "private", form.InitReadme == "on") + 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/import", &form) + return + } else if err == models.ErrRepoNameIllegal { + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/import", &form) + return + } + ctx.Handle(200, "repo.Import", err) +} + func Single(ctx *middleware.Context, params martini.Params) { branchName := ctx.Repo.BranchName commitId := ctx.Repo.CommitId diff --git a/routers/user/social.go b/routers/user/social.go index a35da5493..b87c313f5 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -6,11 +6,15 @@ package user import ( "encoding/json" + "net/http" + "net/url" "strconv" + "strings" "code.google.com/p/goauth2/oauth" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/oauth2" @@ -69,23 +73,59 @@ 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", } - if len(tokens.Access()) == 0 { - log.Error("empty access") + transport := &oauth.Transport{ + Config: config, + Transport: http.DefaultTransport, + } + code := ctx.Query("code") + if code == "" { + // redirect to social login page + ctx.Redirect(config.AuthCodeURL(next)) return } - var err error + + // 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 } @@ -102,18 +142,18 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { 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: - // pass + // ignore it. judge in /usr/login page default: log.Error(err.Error()) // FIXME: handle error page return } ctx.Session.Set("socialId", oa.Id) - log.Info("socialId: %v", oa.Id) - ctx.Redirect("/") + log.Debug("socialId: %v", oa.Id) + ctx.Redirect(next) } diff --git a/templates/repo/import.tmpl b/templates/repo/import.tmpl new file mode 100644 index 000000000..75d928d13 --- /dev/null +++ b/templates/repo/import.tmpl @@ -0,0 +1,112 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+
+ {{.CsrfTokenHtml}} +

Import Repository

+
{{.ErrorMsg}}
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+
+ +
+

{{.SignedUserName}}

+ +
+
+ +
+ +
+ + Great repository names are short and memorable. +
+
+ +
+ +
+

Public

+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + + +
+
+ + Cancel +
+
+
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index bc0853fb0..f0a0a0cca 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -29,7 +29,20 @@
Your Repositories - New Repo +
+ + + +
    {{range .MyRepos}} diff --git a/web.go b/web.go index b8fa9eb7a..5dae84b64 100644 --- a/web.go +++ b/web.go @@ -91,7 +91,7 @@ func runWeb(*cli.Context) { m.Group("/user", func(r martini.Router) { r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn) - r.Any("/login/github", oauth2.LoginRequired, user.SocialSignIn) + r.Any("/login/github", user.SocialSignIn) r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) r.Any("/forget_password", user.ForgotPasswd) r.Any("/reset_password", user.ResetPasswd) @@ -116,6 +116,7 @@ func runWeb(*cli.Context) { m.Get("/user/:username", ignSignIn, user.Profile) m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) + m.Any("/repo/import", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Import) adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})