| @@ -1,4 +1,4 @@ | |||
| // Copyright github.com/juju2013. All rights reserved. | |||
| // Copyright 2014 The Gogs Authors. All rights reserved. | |||
| // Use of this source code is governed by a MIT-style | |||
| // license that can be found in the LICENSE file. | |||
| @@ -20,12 +20,13 @@ import ( | |||
| "github.com/gogits/gogs/modules/log" | |||
| ) | |||
| // Login types. | |||
| type LoginType int | |||
| const ( | |||
| LT_NOTYPE = iota | |||
| LT_PLAIN | |||
| LT_LDAP | |||
| LT_SMTP | |||
| NOTYPE LoginType = iota | |||
| PLAIN | |||
| LDAP | |||
| SMTP | |||
| ) | |||
| var ( | |||
| @@ -34,9 +35,9 @@ var ( | |||
| ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") | |||
| ) | |||
| var LoginTypes = map[int]string{ | |||
| LT_LDAP: "LDAP", | |||
| LT_SMTP: "SMTP", | |||
| var LoginTypes = map[LoginType]string{ | |||
| LDAP: "LDAP", | |||
| SMTP: "SMTP", | |||
| } | |||
| // Ensure structs implmented interface. | |||
| @@ -49,7 +50,6 @@ type LDAPConfig struct { | |||
| ldap.Ldapsource | |||
| } | |||
| // implement | |||
| func (cfg *LDAPConfig) FromDB(bs []byte) error { | |||
| return json.Unmarshal(bs, &cfg.Ldapsource) | |||
| } | |||
| @@ -65,7 +65,6 @@ type SMTPConfig struct { | |||
| TLS bool | |||
| } | |||
| // implement | |||
| func (cfg *SMTPConfig) FromDB(bs []byte) error { | |||
| return json.Unmarshal(bs, cfg) | |||
| } | |||
| @@ -76,13 +75,13 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) { | |||
| type LoginSource struct { | |||
| Id int64 | |||
| Type int | |||
| Name string `xorm:"unique"` | |||
| IsActived bool `xorm:"not null default false"` | |||
| Type LoginType | |||
| Name string `xorm:"UNIQUE"` | |||
| IsActived bool `xorm:"NOT NULL DEFAULT false"` | |||
| Cfg core.Conversion `xorm:"TEXT"` | |||
| Created time.Time `xorm:"created"` | |||
| Updated time.Time `xorm:"updated"` | |||
| AllowAutoRegister bool `xorm:"not null default false"` | |||
| AllowAutoRegister bool `xorm:"NOT NULL DEFAULT false"` | |||
| Created time.Time `xorm:"CREATED"` | |||
| Updated time.Time `xorm:"UPDATED"` | |||
| } | |||
| func (source *LoginSource) TypeString() string { | |||
| @@ -97,21 +96,25 @@ func (source *LoginSource) SMTP() *SMTPConfig { | |||
| return source.Cfg.(*SMTPConfig) | |||
| } | |||
| // for xorm callback | |||
| func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { | |||
| if colName == "type" { | |||
| ty := (*val).(int64) | |||
| switch ty { | |||
| case LT_LDAP: | |||
| switch LoginType(ty) { | |||
| case LDAP: | |||
| source.Cfg = new(LDAPConfig) | |||
| case LT_SMTP: | |||
| case SMTP: | |||
| source.Cfg = new(SMTPConfig) | |||
| } | |||
| } | |||
| } | |||
| func CreateSource(source *LoginSource) error { | |||
| _, err := orm.Insert(source) | |||
| return err | |||
| } | |||
| func GetAuths() ([]*LoginSource, error) { | |||
| var auths = make([]*LoginSource, 0) | |||
| var auths = make([]*LoginSource, 0, 5) | |||
| err := orm.Find(&auths) | |||
| return auths, err | |||
| } | |||
| @@ -121,18 +124,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) { | |||
| has, err := orm.Id(id).Get(source) | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| if !has { | |||
| } else if !has { | |||
| return nil, ErrAuthenticationNotExist | |||
| } | |||
| return source, nil | |||
| } | |||
| func AddSource(source *LoginSource) error { | |||
| _, err := orm.Insert(source) | |||
| return err | |||
| } | |||
| func UpdateSource(source *LoginSource) error { | |||
| _, err := orm.Id(source.Id).AllCols().Update(source) | |||
| return err | |||
| @@ -164,14 +161,14 @@ func UserSignIn(uname, passwd string) (*User, error) { | |||
| return nil, err | |||
| } | |||
| if u.LoginType == LT_NOTYPE { | |||
| if u.LoginType == NOTYPE { | |||
| if has { | |||
| u.LoginType = LT_PLAIN | |||
| u.LoginType = PLAIN | |||
| } | |||
| } | |||
| // for plain login, user must have existed. | |||
| if u.LoginType == LT_PLAIN { | |||
| if u.LoginType == PLAIN { | |||
| if !has { | |||
| return nil, ErrUserNotExist | |||
| } | |||
| @@ -191,22 +188,20 @@ func UserSignIn(uname, passwd string) (*User, error) { | |||
| } | |||
| for _, source := range sources { | |||
| if source.Type == LT_LDAP { | |||
| if source.Type == LDAP { | |||
| u, err := LoginUserLdapSource(nil, uname, passwd, | |||
| source.Id, source.Cfg.(*LDAPConfig), true) | |||
| if err == nil { | |||
| return u, nil | |||
| } else { | |||
| log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err) | |||
| } | |||
| } else if source.Type == LT_SMTP { | |||
| log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err) | |||
| } else if source.Type == SMTP { | |||
| u, err := LoginUserSMTPSource(nil, uname, passwd, | |||
| source.Id, source.Cfg.(*SMTPConfig), true) | |||
| if err == nil { | |||
| return u, nil | |||
| } else { | |||
| log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err) | |||
| } | |||
| log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err) | |||
| } | |||
| } | |||
| @@ -224,10 +219,10 @@ func UserSignIn(uname, passwd string) (*User, error) { | |||
| } | |||
| switch u.LoginType { | |||
| case LT_LDAP: | |||
| case LDAP: | |||
| return LoginUserLdapSource(u, u.LoginName, passwd, | |||
| source.Id, source.Cfg.(*LDAPConfig), false) | |||
| case LT_SMTP: | |||
| case SMTP: | |||
| return LoginUserSMTPSource(u, u.LoginName, passwd, | |||
| source.Id, source.Cfg.(*SMTPConfig), false) | |||
| } | |||
| @@ -252,7 +247,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L | |||
| user = &User{ | |||
| LowerName: strings.ToLower(name), | |||
| Name: strings.ToLower(name), | |||
| LoginType: LT_LDAP, | |||
| LoginType: LDAP, | |||
| LoginSource: sourceId, | |||
| LoginName: name, | |||
| IsActive: true, | |||
| @@ -320,9 +315,8 @@ func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error { | |||
| return err | |||
| } | |||
| return nil | |||
| } else { | |||
| return ErrUnsupportedLoginType | |||
| } | |||
| return ErrUnsupportedLoginType | |||
| } | |||
| // Query if name/passwd can login against the LDAP direcotry pool | |||
| @@ -358,13 +352,12 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S | |||
| user = &User{ | |||
| LowerName: strings.ToLower(loginName), | |||
| Name: strings.ToLower(loginName), | |||
| LoginType: LT_SMTP, | |||
| LoginType: SMTP, | |||
| LoginSource: sourceId, | |||
| LoginName: name, | |||
| IsActive: true, | |||
| Passwd: passwd, | |||
| Email: name, | |||
| } | |||
| return RegisterUser(user) | |||
| } | |||
| @@ -109,11 +109,11 @@ func NewRepoContext() { | |||
| // Repository represents a git repository. | |||
| type Repository struct { | |||
| Id int64 | |||
| OwnerId int64 `xorm:"unique(s)"` | |||
| OwnerId int64 `xorm:"UNIQUE(s)"` | |||
| Owner *User `xorm:"-"` | |||
| ForkId int64 | |||
| LowerName string `xorm:"unique(s) index not null"` | |||
| Name string `xorm:"index not null"` | |||
| LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` | |||
| Name string `xorm:"INDEX NOT NULL"` | |||
| Description string | |||
| Website string | |||
| NumWatches int | |||
| @@ -131,8 +131,8 @@ type Repository struct { | |||
| IsBare bool | |||
| IsGoget bool | |||
| DefaultBranch string | |||
| Created time.Time `xorm:"created"` | |||
| Updated time.Time `xorm:"updated"` | |||
| Created time.Time `xorm:"CREATED"` | |||
| Updated time.Time `xorm:"UPDATED"` | |||
| } | |||
| func (repo *Repository) GetOwner() (err error) { | |||
| @@ -184,6 +184,25 @@ type Mirror struct { | |||
| NextUpdate time.Time | |||
| } | |||
| // MirrorRepository creates a mirror repository from source. | |||
| func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error { | |||
| _, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath) | |||
| if err != nil { | |||
| return errors.New("git clone --mirror: " + stderr) | |||
| } | |||
| if _, err = orm.InsertOne(&Mirror{ | |||
| RepoId: repoId, | |||
| RepoName: strings.ToLower(userName + "/" + repoName), | |||
| Interval: 24, | |||
| NextUpdate: time.Now().Add(24 * time.Hour), | |||
| }); err != nil { | |||
| return err | |||
| } | |||
| return git.UnpackRefs(repoPath) | |||
| } | |||
| func GetMirror(repoId int64) (*Mirror, error) { | |||
| m := &Mirror{RepoId: repoId} | |||
| has, err := orm.Get(m) | |||
| @@ -223,25 +242,6 @@ func MirrorUpdate() { | |||
| } | |||
| } | |||
| // MirrorRepository creates a mirror repository from source. | |||
| func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error { | |||
| _, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath) | |||
| if err != nil { | |||
| return errors.New("git clone --mirror: " + stderr) | |||
| } | |||
| if _, err = orm.InsertOne(&Mirror{ | |||
| RepoId: repoId, | |||
| RepoName: strings.ToLower(userName + "/" + repoName), | |||
| Interval: 24, | |||
| NextUpdate: time.Now().Add(24 * time.Hour), | |||
| }); err != nil { | |||
| return err | |||
| } | |||
| return git.UnpackRefs(repoPath) | |||
| } | |||
| // MigrateRepository migrates a existing repository from other project hosting. | |||
| func MigrateRepository(user *User, name, desc string, private, mirror bool, url string) (*Repository, error) { | |||
| repo, err := CreateRepository(user, name, desc, "", "", private, mirror, false) | |||
| @@ -746,16 +746,11 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) { | |||
| sess.Rollback() | |||
| return err | |||
| } | |||
| if err = sess.Commit(); err != nil { | |||
| sess.Rollback() | |||
| return err | |||
| } | |||
| if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil { | |||
| // TODO: log and delete manully | |||
| log.Error("delete repo %s/%s failed: %v", userName, repo.Name, err) | |||
| sess.Rollback() | |||
| return err | |||
| } | |||
| return nil | |||
| return sess.Commit() | |||
| } | |||
| // GetRepositoryByName returns the repository by given name under user if exists. | |||
| @@ -47,7 +47,7 @@ type User struct { | |||
| FullName string | |||
| Email string `xorm:"unique not null"` | |||
| Passwd string `xorm:"not null"` | |||
| LoginType int | |||
| LoginType LoginType | |||
| LoginSource int64 `xorm:"not null default 0"` | |||
| LoginName string | |||
| Type int | |||
| @@ -10,7 +10,7 @@ body { | |||
| html, body { | |||
| height: 100%; | |||
| font-family: Helvetica, Arial, sans-serif; | |||
| font-family: Arial, Helvetica, sans-serif; | |||
| } | |||
| /* override bs3 */ | |||
| @@ -38,8 +38,8 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| } | |||
| var u core.Conversion | |||
| switch form.Type { | |||
| case models.LT_LDAP: | |||
| switch models.LoginType(form.Type) { | |||
| case models.LDAP: | |||
| u = &models.LDAPConfig{ | |||
| Ldapsource: ldap.Ldapsource{ | |||
| Host: form.Host, | |||
| @@ -53,7 +53,7 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| Name: form.AuthName, | |||
| }, | |||
| } | |||
| case models.LT_SMTP: | |||
| case models.SMTP: | |||
| u = &models.SMTPConfig{ | |||
| Auth: form.SmtpAuth, | |||
| Host: form.SmtpHost, | |||
| @@ -66,14 +66,14 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| } | |||
| var source = &models.LoginSource{ | |||
| Type: form.Type, | |||
| Type: models.LoginType(form.Type), | |||
| Name: form.AuthName, | |||
| IsActived: true, | |||
| AllowAutoRegister: form.AllowAutoRegister, | |||
| Cfg: u, | |||
| } | |||
| if err := models.AddSource(source); err != nil { | |||
| if err := models.CreateSource(source); err != nil { | |||
| ctx.Handle(500, "admin.auths.NewAuth", err) | |||
| return | |||
| } | |||
| @@ -116,8 +116,8 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| } | |||
| var config core.Conversion | |||
| switch form.Type { | |||
| case models.LT_LDAP: | |||
| switch models.LoginType(form.Type) { | |||
| case models.LDAP: | |||
| config = &models.LDAPConfig{ | |||
| Ldapsource: ldap.Ldapsource{ | |||
| Host: form.Host, | |||
| @@ -131,7 +131,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| Name: form.AuthName, | |||
| }, | |||
| } | |||
| case models.LT_SMTP: | |||
| case models.SMTP: | |||
| config = &models.SMTPConfig{ | |||
| Auth: form.SmtpAuth, | |||
| Host: form.SmtpHost, | |||
| @@ -147,7 +147,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { | |||
| Id: form.Id, | |||
| Name: form.AuthName, | |||
| IsActived: form.IsActived, | |||
| Type: form.Type, | |||
| Type: models.LoginType(form.Type), | |||
| AllowAutoRegister: form.AllowAutoRegister, | |||
| Cfg: config, | |||
| } | |||
| @@ -51,12 +51,13 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { | |||
| Email: form.Email, | |||
| Passwd: form.Password, | |||
| IsActive: true, | |||
| LoginType: models.LT_PLAIN, | |||
| LoginType: models.PLAIN, | |||
| } | |||
| if len(form.LoginType) > 0 { | |||
| fields := strings.Split(form.LoginType, "-") | |||
| u.LoginType, _ = strconv.Atoi(fields[0]) | |||
| tp, _ := strconv.Atoi(fields[0]) | |||
| u.LoginType = models.LoginType(tp) | |||
| u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64) | |||
| u.LoginName = form.LoginName | |||
| fmt.Println(u.LoginType, u.LoginSource, u.LoginName) | |||
| @@ -27,6 +27,7 @@ | |||
| <hr/> | |||
| <div class="clone-zip text-center"> | |||
| <a class="btn btn-success btn-lg" href="{{.RepoLink}}/archive/{{.BranchName}}/{{.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-suitcase"></i>Download ZIP</a> | |||
| <a class="btn btn-success btn-lg" href="{{.RepoLink}}/archive/{{.BranchName}}/{{.Repository.Name}}.tar.gz" rel="nofollow"><i class="fa fa-suitcase"></i>Download TAR.GZ</a> | |||
| </div> | |||
| </div> | |||
| </div> | |||