// register a user func (this *UserService) RegisterUser(u *user.User, username, email, password string, userType *user.UserType, role *user.Role) error { u.UserType = userType // use random salt encode password salt := GetUserSalt() pwd := utils.EncodePassword(password, salt) u.Username = strings.ToLower(username) u.Email = strings.ToLower(email) // save salt and encode password, use $ as split char u.Password = fmt.Sprintf("%s$%s", salt, pwd) u.PasswordSalt = salt var err error tr := orm.NewOrm() tr.Begin() if err = this.InsertWithScope(tr, u); err == nil { roleService := RoleService{} err = roleService.InsertUsersWithScope(tr, role, u) } if err == nil { tr.Commit() } else { tr.Rollback() } return err }
// check if exist user by username or email, ignore "deleted" users func (this *UserService) HasUser(user *user.User, username string) bool { var err error qs := orm.NewOrm() if strings.IndexRune(username, '@') == -1 { user.Username = username err = qs.Read(user, "Username", "Deleted") } else { user.Email = username err = qs.Read(user, "Email", "Deleted") } if err == nil { return true } return false }