0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-17 15:31:06 +01:00

Merge branch 'dev' of github.com:gogits/gogs into dev

Conflicts:
	models/oauth2.go
This commit is contained in:
Unknown 2014-04-09 14:15:09 -04:00
commit 5f6bd323f5
9 changed files with 260 additions and 24 deletions

View file

@ -8,6 +8,5 @@
], ],
"env": { "env": {
"POWERED_BY": "github.com/shxsun/fswatch" "POWERED_BY": "github.com/shxsun/fswatch"
}, }
"enable-restart": false
} }

View file

@ -19,7 +19,7 @@ import (
// Test that go1.2 tag above is included in builds. main.go refers to this definition. // Test that go1.2 tag above is included in builds. main.go refers to this definition.
const go12tag = true const go12tag = true
const APP_VER = "0.2.2.0408 Alpha" const APP_VER = "0.2.2.0409 Alpha"
func init() { func init() {
base.AppVer = APP_VER base.AppVer = APP_VER

View file

@ -44,5 +44,6 @@ func GetOauth2(identity string) (oa *Oauth2, err error) {
} else if oa.Uid == 0 { } else if oa.Uid == 0 {
return oa, ErrOauth2NotAssociatedWithUser return oa, ErrOauth2NotAssociatedWithUser
} }
return GetUserById(oa.Uid) oa.User, err = GetUserById(oa.Uid)
return oa, err
} }

View file

@ -309,6 +309,18 @@ html, body {
height: 8em; 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 */ /* gogits user setting */
#user-setting-nav > h4, #user-setting-container > h4, #user-setting-container > div > h4, #user-setting-nav > h4, #user-setting-container > h4, #user-setting-container > div > h4,
@ -444,6 +456,32 @@ html, body {
margin-right: 1em; 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 */ /* gogits repo single page */
#body-nav.repo-nav { #body-nav.repo-nav {
@ -1372,6 +1410,6 @@ html, body {
margin: 16px 0; margin: 16px 0;
} }
#release-preview{ #release-preview {
margin: 6px 0; margin: 6px 0;
} }

View file

@ -55,6 +55,38 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
ctx.Handle(200, "repo.Create", err) 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) { func Single(ctx *middleware.Context, params martini.Params) {
branchName := ctx.Repo.BranchName branchName := ctx.Repo.BranchName
commitId := ctx.Repo.CommitId commitId := ctx.Repo.CommitId

View file

@ -6,11 +6,15 @@ package user
import ( import (
"encoding/json" "encoding/json"
"net/http"
"net/url"
"strconv" "strconv"
"strings"
"code.google.com/p/goauth2/oauth" "code.google.com/p/goauth2/oauth"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/oauth2" "github.com/gogits/gogs/modules/oauth2"
@ -69,23 +73,59 @@ func (s *SocialGithub) Update() error {
return json.NewDecoder(r.Body).Decode(&s.data) 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 && ... // github && google && ...
func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
gh := &SocialGithub{ var socid int64
WebToken: &oauth.Token{ var ok bool
AccessToken: tokens.Access(), next := extractPath(ctx.Query("next"))
RefreshToken: tokens.Refresh(), log.Debug("social signed check %s", next)
Expiry: tokens.ExpiryTime(), if socid, ok = ctx.Session.Get("socialId").(int64); ok && socid != 0 {
Extra: tokens.ExtraData(), // already login
}, ctx.Redirect(next)
} log.Info("login soc id: %v", socid)
if len(tokens.Access()) == 0 {
log.Error("empty access")
return return
} }
var err error 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,
}
code := ctx.Query("code")
if code == "" {
// redirect to social login page
ctx.Redirect(config.AuthCodeURL(next))
return
}
// 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 { if err = gh.Update(); err != nil {
// FIXME: handle error page // FIXME: handle error page 501
log.Error("connect with github error: %s", err) log.Error("connect with github error: %s", err)
return return
} }
@ -102,18 +142,18 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
oa.Type = soc.Type() oa.Type = soc.Type()
oa.Token = soc.Token() oa.Token = soc.Token()
oa.Identity = soc.Identity() oa.Identity = soc.Identity()
log.Info("oa: %v", oa) log.Debug("oa: %v", oa)
if err = models.AddOauth2(oa); err != nil { if err = models.AddOauth2(oa); err != nil {
log.Error("add oauth2 %v", err) log.Error("add oauth2 %v", err) // 501
return return
} }
case models.ErrOauth2NotAssociatedWithUser: case models.ErrOauth2NotAssociatedWithUser:
// pass // ignore it. judge in /usr/login page
default: default:
log.Error(err.Error()) // FIXME: handle error page log.Error(err.Error()) // FIXME: handle error page
return return
} }
ctx.Session.Set("socialId", oa.Id) ctx.Session.Set("socialId", oa.Id)
log.Info("socialId: %v", oa.Id) log.Debug("socialId: %v", oa.Id)
ctx.Redirect("/") ctx.Redirect(next)
} }

112
templates/repo/import.tmpl Normal file
View file

@ -0,0 +1,112 @@
{{template "base/head" .}}
{{template "base/navbar" .}}
<div class="container" id="body">
<form action="/repo/create" method="post" class="form-horizontal card" id="repo-create">
{{.CsrfTokenHtml}}
<h3>Import Repository</h3>
<div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div>
<div class="form-group">
<label class="col-md-2 control-label">From<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<select class="form-control" name="from">
<option value="">GitHub</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">URL<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<input name="url" type="text" class="form-control" placeholder="Type your imported repository url link" required="required">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<a class="btn btn-default" data-toggle="collapse" data-target="#repo-import-auth">Need Authorization</a>
</div>
<div id="repo-import-auth" class="collapse">
<div class="form-group">
<label class="col-md-2 control-label">Username</label>
<div class="col-md-8">
<input name="auth-username" type="text" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Password</label>
<div class="col-md-8">
<input name="auth-password" type="text" class="form-control">
</div>
</div>
</div>
</div>
<hr/>
<div class="form-group">
<label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<p class="form-control-static">{{.SignedUserName}}</p>
<input type="hidden" value="{{.SignedUserId}}" name="userId"/>
</div>
</div>
<div class="form-group {{if .Err_RepoName}}has-error has-feedback{{end}}">
<label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<input name="repo" type="text" class="form-control" placeholder="Type your repository name" value="{{.repo}}" required="required">
<span class="help-block">Great repository names are short and memorable. </span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Visibility<strong class="text-danger">*</strong></label>
<div class="col-md-8">
<p class="form-control-static">Public</p>
<input type="hidden" value="public" name="visibility"/>
</div>
</div>
<div class="form-group {{if .Err_Description}}has-error has-feedback{{end}}">
<label class="col-md-2 control-label">Description</label>
<div class="col-md-8">
<textarea name="desc" class="form-control" placeholder="Type your repository description">{{.desc}}</textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Language</label>
<div class="col-md-8">
<select class="form-control" name="language">
<option value="">Select a language</option>
{{range .LanguageIgns}}<option value="{{.}}">{{.}}</option>{{end}}
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">License</label>
<div class="col-md-8">
<select class="form-control" name="license">
<option value="">Select a license</option>
{{range .Licenses}}<option value="{{.}}">{{.}}</option>{{end}}
</select>
</div>
</div>
<!--<div class="form-group">
<div class="col-md-8 col-md-offset-2">
<div class="checkbox">
<label>
<input type="checkbox" name="initReadme" {{if .initReadme}}checked{{end}}>
<strong>Initialize this repository with a README</strong>
</label>
</div>
</div>
</div>-->
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<button type="submit" class="btn btn-lg btn-primary">Import repository</button>
<a href="/" class="text-danger">Cancel</a>
</div>
</div>
</form>
</div>
{{template "base/footer" .}}

View file

@ -29,7 +29,20 @@
<div id="feed-right" class="col-md-4"> <div id="feed-right" class="col-md-4">
<div class="panel panel-default repo-panel"> <div class="panel panel-default repo-panel">
<div class="panel-heading">Your Repositories <div class="panel-heading">Your Repositories
<a class="btn btn-success pull-right btn-sm" href="/repo/create"><i class="fa fa-plus-square"></i>New Repo</a> <div class="btn-group pull-right" id="user-dashboard-repo-new">
<button type="button" class="btn btn-success btn-sm"><i class="fa fa-plus-square"></i>New</button>
<button type="button" class="btn btn-success btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<div class="dropdown-menu dropdown-menu-right">
<ul class="list-unstyled">
<li><a href="/repo/create"><i class="fa fa-book"></i>Repository</a></li>
<li><a href="/repo/import"><i class="fa fa-clipboard"></i>Mirror</a></li>
<li><a href="#"><i class="fa fa-users"></i>Organization</a></li>
</ul>
</div>
</div>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<ul class="list-group">{{range .MyRepos}} <ul class="list-group">{{range .MyRepos}}

3
web.go
View file

@ -91,7 +91,7 @@ func runWeb(*cli.Context) {
m.Group("/user", func(r martini.Router) { m.Group("/user", func(r martini.Router) {
r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn) 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("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
r.Any("/forget_password", user.ForgotPasswd) r.Any("/forget_password", user.ForgotPasswd)
r.Any("/reset_password", user.ResetPasswd) r.Any("/reset_password", user.ResetPasswd)
@ -116,6 +116,7 @@ func runWeb(*cli.Context) {
m.Get("/user/:username", ignSignIn, user.Profile) m.Get("/user/:username", ignSignIn, user.Profile)
m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) 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}) adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})