Esempio n. 1
0
// Save inserts or updates a Account.
func (m *Manager) Save(account *Account) error {
	log.Debug("query: accounts.Save")
	var (
		hash string
		err  error
	)

	// hash password
	if account.Password != "" {
		h, err := auth.Hash(account.Password)
		if err != nil {
			return err
		}

		hash = h
	}

	if account.Id != "" {
		updates := map[string]interface{}{
			"firstName": account.FirstName,
			"lastName":  account.LastName,
			"email":     account.Email,
			"isAdmin":   account.IsAdmin,
		}

		if account.Password != "" {
			updates["password"] = hash
		}

		_, err = m.t().Get(account.Id).Update(updates).RunWrite(m.session)
		log.Debugf("updated account: id=%s username=%s", account.Id, account.Username)
	} else {
		// set creation date
		account.Created = time.Now().Unix()

		// check for existing account when creating a new account
		existing, _ := m.GetByUsername(account.Username)
		if existing != nil {
			return ErrAccountExists
		}

		account.Password = hash
		_, err = m.t().Insert(account).RunWrite(m.session)
		log.Debugf("saved account: username=%s", account.Username)
	}

	if err != nil {
		return err
	}

	return nil
}
Esempio n. 2
0
// ChangePassword changes the password for the specified user.  Password
// must not be hashed as it will be hashed before save.
func (m *Manager) ChangePassword(username, password string) error {
	if !m.authenticator.IsUpdateSupported() {
		return fmt.Errorf("not supported for authenticator: %s", m.authenticator.Name())
	}

	hash, err := auth.Hash(password)
	if err != nil {
		return err
	}

	res, err := m.t().Filter(map[string]string{"username": username}).Update(map[string]string{"password": hash}).Run(m.session)
	if err != nil {
		return err
	}
	defer res.Close()

	log.Debugf("password updated: username=%s", username)

	return nil
}