From d922b7e5049a88fbba436ad08c817ac79ee64c6b Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 7 Aug 2015 00:00:11 +0800 Subject: [PATCH] #1443 set first user to be admin ...not just ID=1 --- models/user.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/models/user.go b/models/user.go index 1e514cdf5108..37e7640ee641 100644 --- a/models/user.go +++ b/models/user.go @@ -314,19 +314,23 @@ func CreateUser(u *User) (err error) { return err } - // Auto-set admin for user whose ID is 1. - if u.Id == 1 { + // Auto-set admin for the first user. + if countUsers(sess) == 1 { u.IsAdmin = true u.IsActive = true - _, err = x.Id(u.Id).UseBool().Update(u) + _, err = x.Id(u.Id).AllCols().Update(u) } return err } +func countUsers(e Engine) int64 { + count, _ := e.Where("type=0").Count(new(User)) + return count +} + // CountUsers returns number of users. func CountUsers() int64 { - count, _ := x.Where("type=0").Count(new(User)) - return count + return countUsers(x) } // GetUsers returns given number of user objects with offset.