<commit_before>package user

import (
	"github.com/MG-RAST/AWE/lib/conf"
	"github.com/MG-RAST/AWE/lib/db"
	"github.com/MG-RAST/AWE/vendor/code.google.com/p/go-uuid/uuid"
	mgo "github.com/MG-RAST/AWE/vendor/gopkg.in/mgo.v2"
	"github.com/MG-RAST/AWE/vendor/gopkg.in/mgo.v2/bson"
)

// Array of User
type Users []User

// User struct
type User struct {
	Uuid         string      `bson:"uuid" json:"uuid"`
	Username     string      `bson:"username" json:"username"`
	Fullname     string      `bson:"fullname" json:"fullname"`
	Email        string      `bson:"email" json:"email"`
	Password     string      `bson:"password" json:"-"`
	Admin        bool        `bson:"admin" json:"admin"`
	CustomFields interface{} `bson:"custom_fields" json:"custom_fields"`
}

func Initialize() (err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	if err = c.EnsureIndex(mgo.Index{Key: []string{"uuid"}, Unique: true}); err != nil {
		return err
	}
	if err = c.EnsureIndex(mgo.Index{Key: []string{"username"}, Unique: true}); err != nil {
		return err
	}

	// Setting admin users based on config file.  First, set all users to Admin = false
	if _, err = c.UpdateAll(bson.M{}, bson.M{"$set": bson.M{"admin": false}}); err != nil {
		return err
	}

	// This config parameter contains a string that should be a comma-separated list of users that are Admins.
	for k, _ := range conf.Admin_Users {
		if k != "" {
			if info, err := c.UpdateAll(bson.M{"username": k}, bson.M{"$set": bson.M{"admin": true}}); err != nil {
				return err
			} else if info.Updated == 0 {
				u, err := New(k, "", true)
				if err != nil {
					return err
				}
				if err := u.Save(); err != nil {
					return err
				}
			}
		}
	}
	return
}

func New(username string, password string, isAdmin bool) (u *User, err error) {
	u = &User{Uuid: uuid.New(), Username: username, Password: password, Admin: isAdmin}
	if err = u.Save(); err != nil {
		u = nil
	}
	return
}

func FindByUuid(uuid string) (u *User, err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	u = &User{Uuid: uuid}
	if err = c.Find(bson.M{"uuid": u.Uuid}).One(&u); err != nil {
		return nil, err
	}
	return
}

func FindByUsernamePassword(username string, password string) (u *User, err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	u = &User{}
	if err = c.Find(bson.M{"username": username, "password": password}).One(&u); err != nil {
		return nil, err
	}
	return
}

func AdminGet(u *Users) (err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()<commit_msg>remove double user save<commit_after>package user

import (
	"github.com/MG-RAST/AWE/lib/conf"
	"github.com/MG-RAST/AWE/lib/db"
	"github.com/MG-RAST/AWE/vendor/code.google.com/p/go-uuid/uuid"
	mgo "github.com/MG-RAST/AWE/vendor/gopkg.in/mgo.v2"
	"github.com/MG-RAST/AWE/vendor/gopkg.in/mgo.v2/bson"
)

// Array of User
type Users []User

// User struct
type User struct {
	Uuid         string      `bson:"uuid" json:"uuid"`
	Username     string      `bson:"username" json:"username"`
	Fullname     string      `bson:"fullname" json:"fullname"`
	Email        string      `bson:"email" json:"email"`
	Password     string      `bson:"password" json:"-"`
	Admin        bool        `bson:"admin" json:"admin"`
	CustomFields interface{} `bson:"custom_fields" json:"custom_fields"`
}

func Initialize() (err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	if err = c.EnsureIndex(mgo.Index{Key: []string{"uuid"}, Unique: true}); err != nil {
		return err
	}
	if err = c.EnsureIndex(mgo.Index{Key: []string{"username"}, Unique: true}); err != nil {
		return err
	}

	// Setting admin users based on config file.  First, set all users to Admin = false
	if _, err = c.UpdateAll(bson.M{}, bson.M{"$set": bson.M{"admin": false}}); err != nil {
		return err
	}

	// This config parameter contains a string that should be a comma-separated list of users that are Admins.
	for k, _ := range conf.Admin_Users {
		if k != "" {
			if info, err := c.UpdateAll(bson.M{"username": k}, bson.M{"$set": bson.M{"admin": true}}); err != nil {
				return err
			} else if info.Updated == 0 {
				if _, err := New(k, "", true); err != nil {
					return err
				}
			}
		}
	}
	return
}

func New(username string, password string, isAdmin bool) (u *User, err error) {
	u = &User{Uuid: uuid.New(), Username: username, Password: password, Admin: isAdmin}
	err = u.Save()
	if err != nil {
		u = nil
	}
	return
}

func FindByUuid(uuid string) (u *User, err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	u = &User{Uuid: uuid}
	if err = c.Find(bson.M{"uuid": u.Uuid}).One(&u); err != nil {
		return nil, err
	}
	return
}

func FindByUsernamePassword(username string, password string) (u *User, err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
	c := session.DB(conf.MONGODB_DATABASE).C("Users")
	u = &User{}
	if err = c.Find(bson.M{"username": username, "password": password}).One(&u); err != nil {
		return nil, err
	}
	return
}

func AdminGet(u *Users) (err error) {
	session := db.Connection.Session.Copy()
	defer session.Close()
{'fraction_non_alphanumeric': 0.16974852071005916, 'fraction_numerical': 0.0011094674556213018, 'mean_word_length': 3.5, 'pattern_counts': {'":': 16, '<': 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': '7413743', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1980, 'n_words': 494}