Пример #1
0
func DeleteUser(userData database.UserData, userId string, password string) int {
	if len(userId) > 0 && len(password) > 0 {
		passwordHash, _ := userData.GetUser(userId)
		if passwordHash == nil {
			log.Warning("User not found, cannot delete: %s", userId)
			return http.StatusNotFound
		} else {
			if CompareHashAndPassword(passwordHash, password) == nil {
				userData.DeleteUser(userId)
				return http.StatusOK
			} else {
				return http.StatusBadRequest
			}

		}
	} else {
		log.Debug("Unable to delete user, username or password missing")
		return http.StatusBadRequest
	}
}
Пример #2
0
func RegisterUser(userData database.UserData, userId string, password string) int {
	if len(userId) > 0 && len(password) > 0 {
		userObj, _ := NewUser(userId, password)
		// Check if user already exists
		user, err := userData.GetUser(userObj.GetUserId())
		if user != nil {
			log.Warning("Duplicate username tried: %s", userId)
			return http.StatusConflict
		}
		// Save User to database
		err = userData.SaveUser(userObj.GetUserId(), userObj.GetPasswordHash())
		if err != nil {
			log.Warning("Unable to create user: %s", err.Error())
			return http.StatusInternalServerError
		} else {
			return http.StatusOK
		}
	} else {
		log.Debug("Unable to create user, username or password missing")
		return http.StatusBadRequest
	}
}