Skip to content

Commit

Permalink
db: remove db.User.LoginType field (#6122)
Browse files Browse the repository at this point in the history
  • Loading branch information
kousikmitra committed Apr 18, 2020
1 parent c0fd604 commit 5a52ee7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
4 changes: 0 additions & 4 deletions internal/db/login_source.go
Expand Up @@ -105,7 +105,6 @@ func LoginViaLDAP(login, password string, source *LoginSource, autoRegister bool
Name: username,
FullName: composeFullName(fn, sn, username),
Email: mail,
LoginType: source.Type,
LoginSource: source.ID,
LoginName: login,
IsActive: true,
Expand Down Expand Up @@ -245,7 +244,6 @@ func LoginViaSMTP(login, password string, sourceID int64, cfg *SMTPConfig, autoR
Name: strings.ToLower(username),
Email: login,
Passwd: password,
LoginType: LoginSMTP,
LoginSource: sourceID,
LoginName: login,
IsActive: true,
Expand Down Expand Up @@ -281,7 +279,6 @@ func LoginViaPAM(login, password string, sourceID int64, cfg *PAMConfig, autoReg
Name: login,
Email: login,
Passwd: password,
LoginType: LoginPAM,
LoginSource: sourceID,
LoginName: login,
IsActive: true,
Expand Down Expand Up @@ -317,7 +314,6 @@ func LoginViaGitHub(login, password string, sourceID int64, cfg *GitHubConfig, a
Email: email,
Website: url,
Passwd: password,
LoginType: LoginGitHub,
LoginSource: sourceID,
LoginName: login,
IsActive: true,
Expand Down
9 changes: 4 additions & 5 deletions internal/db/user.go
Expand Up @@ -53,10 +53,9 @@ type User struct {
Name string `xorm:"UNIQUE NOT NULL" gorm:"NOT NULL"`
FullName string
// Email is the primary email address (to be used for communication)
Email string `xorm:"NOT NULL" gorm:"NOT NULL"`
Passwd string `xorm:"NOT NULL" gorm:"NOT NULL"`
LoginType LoginType // TODO: Remove me https://github.com/gogs/gogs/issues/6117.
LoginSource int64 `xorm:"NOT NULL DEFAULT 0" gorm:"NOT NULL;DEFAULT:0"`
Email string `xorm:"NOT NULL" gorm:"NOT NULL"`
Passwd string `xorm:"NOT NULL" gorm:"NOT NULL"`
LoginSource int64 `xorm:"NOT NULL DEFAULT 0" gorm:"NOT NULL;DEFAULT:0"`
LoginName string
Type UserType
OwnedOrgs []*User `xorm:"-" gorm:"-" json:"-"`
Expand Down Expand Up @@ -142,7 +141,7 @@ func (u *User) APIFormat() *api.User {

// returns true if user login type is LoginPlain.
func (u *User) IsLocal() bool {
return u.LoginType <= LoginPlain
return u.LoginSource <= 0
}

// HasForkedRepo checks if user has already forked a repository with given ID.
Expand Down
2 changes: 1 addition & 1 deletion internal/db/users.go
Expand Up @@ -98,7 +98,7 @@ func (db *users) Authenticate(login, password string, loginSourceID int64) (*Use
}

// Validate password hash fetched from database for local accounts.
if user.LoginType == LoginNotype || user.LoginType == LoginPlain {
if user.IsLocal() {
if user.ValidatePassword(password) {
return user, nil
}
Expand Down
12 changes: 4 additions & 8 deletions internal/route/admin/users.go
Expand Up @@ -77,17 +77,15 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) {
}

u := &db.User{
Name: f.UserName,
Email: f.Email,
Passwd: f.Password,
IsActive: true,
LoginType: db.LoginPlain,
Name: f.UserName,
Email: f.Email,
Passwd: f.Password,
IsActive: true,
}

if len(f.LoginType) > 0 {
fields := strings.Split(f.LoginType, "-")
if len(fields) == 2 {
u.LoginType = db.LoginType(com.StrTo(fields[0]).MustInt())
u.LoginSource = com.StrTo(fields[1]).MustInt64()
u.LoginName = f.LoginName
}
Expand Down Expand Up @@ -180,12 +178,10 @@ func EditUserPost(c *context.Context, f form.AdminEditUser) {

fields := strings.Split(f.LoginType, "-")
if len(fields) == 2 {
loginType := db.LoginType(com.StrTo(fields[0]).MustInt())
loginSource := com.StrTo(fields[1]).MustInt64()

if u.LoginSource != loginSource {
u.LoginSource = loginSource
u.LoginType = loginType
}
}

Expand Down
12 changes: 5 additions & 7 deletions internal/route/api/v1/admin/user.go
Expand Up @@ -32,19 +32,17 @@ func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginNa
return
}

u.LoginType = source.Type
u.LoginSource = source.ID
u.LoginName = loginName
}

func CreateUser(c *context.APIContext, form api.CreateUserOption) {
u := &db.User{
Name: form.Username,
FullName: form.FullName,
Email: form.Email,
Passwd: form.Password,
IsActive: true,
LoginType: db.LoginPlain,
Name: form.Username,
FullName: form.FullName,
Email: form.Email,
Passwd: form.Password,
IsActive: true,
}

parseLoginSource(c, u, form.SourceID, form.LoginName)
Expand Down

0 comments on commit 5a52ee7

Please sign in to comment.