Example #1
0
// addUser adds new user to bot's database.
func (bot *Bot) addUser(nick, password string, owner, admin bool) error {
	if password == "" {
		return errors.New("Password can't be empty.")
	}
	// Insert user into the db.
	if _, err := bot.Db.Exec(`INSERT INTO users(nick, password, owner, admin) VALUES(?, ?, ?, ?)`,
		nick, utils.HashPassword(password), owner, admin); err != nil {
		// Get exact error.
		driverErr := err.(sqlite3.Error)
		if driverErr.Code == sqlite3.ErrConstraint {
			return errors.New("User already exists.")
		}
		return errors.New("Error while adding new user!")
	}
	return nil
}
Example #2
0
// authenticateUser authenticates the user as an owner or admin
func (bot *Bot) authenticateUser(nick, fullName, password string) error {
	_, dbPassword, _, owner, admin, err := bot.getUserData(nick)
	if err != nil {
		return errors.New(fmt.Sprintf("Error when getting user data for %s: %s", nick, err))
	}
	// Check the password
	if utils.HashPassword(password) != dbPassword {
		return errors.New("Invalid password for user")
	}
	// Check if user has any privileges
	if owner {
		bot.Log.Infof("Authenticating %s as an owner.", nick)
		bot.authenticatedOwners[fullName] = nick
	}
	if admin {
		bot.Log.Infof("Authenticating %s as an admin.", nick)
		bot.authenticatedAdmins[fullName] = nick
	}
	if !admin && !owner {
		bot.Log.Infof("Authenticating %s with no special privileges.", nick)
		bot.authenticatedUsers[fullName] = nick
	}
	return nil
}