<commit_before>package identity

import (
	"gost/service"
	"time"

	"gopkg.in/mgo.v2/bson"
)

// Constants describing the type of account the the users have
const (
	AccountTypeNormalUser    = iota
	AccountTypeAdministrator = iota
)

// Constants describing the status of the current user account
const (
	AccountStatusDeactivated = iota
	AccountStatusActivated   = iota
)

const collectionName = "appusers"

// ApplicationUser contains information necessary for managing accounts
type ApplicationUser struct {
	ID bson.ObjectId `bson:"_id" json:"id"`

	Email                          string    `bson:"email,omitempty" json:"email"`
	Password                       string    `bson:"password,omitempty" json:"password"`
	AccountType                    int       `bson:"accountType,omitempty" json:"accountType"`
	ResetPasswordToken             string    `bson:"resetPasswordToken,omitempty" json:"resetPasswordToken"`
	ResetPasswordTokenExpireDate   time.Time `bson:"resetPasswordTokenExpireDate,omitempty" json:"resetPasswordTokenExpireDate"`
	ActivateAccountToken           string    `bson:"activateAccountToken" json:"activateAccountToken"`
	ActivateAccountTokenExpireDate time.Time `bson:"activateAccountTokenExpireDate,omitempty" json:"activateAccountTokenExpireDate"`
	AccountStatus                  int       `bson:"status,omitempty" json:"status"`
}

// CreateUser adds a new ApplicationUser to the database
func CreateUser(user *ApplicationUser) error {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	if user.ID == "" {
		user.ID = bson.NewObjectId()
	}

	err := collection.Insert(user)

	return err
}

// UpdateUser updates an existing ApplicationUser in the database
func UpdateUser(user *ApplicationUser) error {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	if user.ID == "" {
		return service.ErrNoIDSpecified
	}

	err := collection.UpdateId(user.ID, user)

	return err
}

// GetUser retrieves an ApplicationUser from the database, based on its ID
func GetUser(userID bson.ObjectId) (*ApplicationUser, error) {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	user := ApplicationUser{}
	err := collection.FindId(userID).One(&user)

	return &user, err
}

// IsUserExistent verifies if an user with the given id exists
func IsUserExistent(userID bson.ObjectId) bool {
	user, err := GetUser(userID)

	return err == nil && user != nil
}

// IsUserActivated verifies if an user account is activated
func IsUserActivated(userID bson.ObjectId) bool {
	user, err := GetUser(userID)

	return err == nil && user.AccountStatus == AccountStatusActivated
}<commit_msg>Added new methods for fetching an ApplicationUser and refactored the file<commit_after>package identity

import (
	"gost/service"
	"time"

	"gopkg.in/mgo.v2/bson"
)

// Constants describing the type of account the the users have
const (
	AccountTypeNormalUser    = iota
	AccountTypeAdministrator = iota
)

// Constants describing the status of the current user account
const (
	AccountStatusDeactivated = iota
	AccountStatusActivated   = iota
)

const collectionName = "appusers"

// ApplicationUser contains information necessary for managing accounts
type ApplicationUser struct {
	ID bson.ObjectId `bson:"_id" json:"id"`

	Email                          string    `bson:"email,omitempty" json:"email"`
	Password                       string    `bson:"password,omitempty" json:"password"`
	AccountType                    int       `bson:"accountType,omitempty" json:"accountType"`
	ResetPasswordToken             string    `bson:"resetPasswordToken,omitempty" json:"resetPasswordToken"`
	ResetPasswordTokenExpireDate   time.Time `bson:"resetPasswordTokenExpireDate,omitempty" json:"resetPasswordTokenExpireDate"`
	ActivateAccountToken           string    `bson:"activateAccountToken" json:"activateAccountToken"`
	ActivateAccountTokenExpireDate time.Time `bson:"activateAccountTokenExpireDate,omitempty" json:"activateAccountTokenExpireDate"`
	AccountStatus                  int       `bson:"accountStatus,omitempty" json:"accountStatus"`
}

// CreateUser adds a new ApplicationUser to the database
func CreateUser(user *ApplicationUser) error {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	if user.ID == "" {
		user.ID = bson.NewObjectId()
	}

	var err = collection.Insert(user)

	return err
}

// UpdateUser updates an existing ApplicationUser in the database
func UpdateUser(user *ApplicationUser) error {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	if user.ID == "" {
		return service.ErrNoIDSpecified
	}

	var err = collection.UpdateId(user.ID, user)

	return err
}

// GetUser retrieves an ApplicationUser from the database, based on its ID
func GetUser(userID bson.ObjectId) (*ApplicationUser, error) {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	var user = ApplicationUser{}
	var err = collection.FindId(userID).One(&user)

	return &user, err
}

// GetUserByActivationToken retrieves an ApplicationUser from the database, based on its account activation token
func GetUserByActivationToken(token string) (*ApplicationUser, error) {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	var user = ApplicationUser{}
	var err = collection.Find(bson.M{"activateAccountToken": token}).One(&user)

	return &user, err
}

// GetUserByResetPasswordToken retrieves an ApplicationUser from the database, based on its reset password token
func GetUserByResetPasswordToken(token string) (*ApplicationUser, error) {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	var user = ApplicationUser{}
	var err = collection.Find(bson.M{"resetPasswordToken": token}).One(&user)

	return &user, err
}

// DeleteUser deletes an ApplicationUser from the database, based on its ID
func DeleteUser(userID bson.ObjectId) error {
	session, collection := service.Connect(collectionName)
	defer session.Close()

	return collection.RemoveId(userID)
}

// IsUserExistent verifies if an user with the given id exists
func IsUserExistent(userID bson.ObjectId) bool {
	var user, err = GetUser(userID)

	return err == nil && user != nil
}

// IsUserActivated verifies if an user account is activated
func IsUserActivated(userID bson.ObjectId) bool {
	var user, err = GetUser(userID)

	return err == nil && user.AccountStatus == AccountStatusActivated
}
{'fraction_non_alphanumeric': 0.09283614420547734, 'fraction_numerical': 0.00030945381401825775, 'mean_word_length': 4.543739279588336, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5409041', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1787, 'n_words': 514}