Ejemplo n.º 1
0
func (b *BuiltinStrategy) setupAdminAccount(db orm.Database) (user *builtinUser, profile *perfect.Profile, err error) {

	//setup the admin account
	user = &builtinUser{Id: orm.String(b.Config.Username)}
	err = db.Find(user)
	if err != nil && err != orm.ErrNotFound {
		return
	}

	profile = &perfect.Profile{}
	//search for this profile
	if user.ProfileId != nil {
		profile.Id = user.ProfileId
		err = db.Find(profile)
		if err != nil && err != orm.ErrNotFound {
			return
		}
	}

	//update the profile
	profile.Id = orm.String(b.Config.Email)
	profile.Name = orm.String(b.Config.Name)
	profile.AuthType = orm.String(b.Config.Type)
	err = db.Save(profile)
	if err != nil {
		return
	}

	//hash the password
	password_salt := generateRandomSalt(SALT_ENTROPY)
	password_hash := hash(b.Config.Password, password_salt)
	//update username
	user.Password = &password_hash
	user.Salt = &password_salt
	user.ProfileId = profile.Id
	err = db.Save(user)
	if err != nil {
		return
	}

	return user, profile, nil
}
Ejemplo n.º 2
0
func createBuiltinProfile(username, password, name, email string, db orm.Database) (user *builtinUser, profile *perfect.Profile, err error) {
	user = &builtinUser{Id: &username}

	//query the database to check if the username exists
	err = db.Find(user)
	if err != orm.ErrNotFound {
		//make sure users can't register duplicate usernames
		if err == nil {
			err = ErrUsernameExists
		}
		return
	}

	//create a perfect user (profile)
	profile = perfect.NewProfile(email, name)
	profile.AuthType = orm.String(BUILTIN)
	err = db.Save(profile)
	if err != nil {
		log.Println(err)
		return
	}

	//hash the password
	password_salt := generateRandomSalt(SALT_ENTROPY)
	password_hash := hash(password, password_salt)

	//create an entry to store auth details
	user.Password = &password_hash
	user.Salt = &password_salt
	user.ProfileId = profile.Id
	err = db.Save(user)
	if err != nil {
		log.Println(err)
		return
	}

	return user, profile, nil
}