Ejemplo n.º 1
0
// UpdateUserPassword updates an existing user's password and token in the database.
func UpdateUserPassword(context interface{}, db *db.DB, u *User, password string) error {
	log.Dev(context, "UpdateUserPassword", "Started : PublicID[%s]", u.PublicID)

	if err := u.Validate(); err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	if len(password) < 8 {
		err := errors.New("Invalid password length")
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	newPassHash, err := crypto.BcryptPassword(u.PrivateID + password)
	if err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": u.PublicID}
		upd := bson.M{"$set": bson.M{"password": newPassHash, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUserPassword", "MGO : db.%s.Update(%s, CAN'T SHOW)", c.Name, mongo.Query(q))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUserPassword", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUserPassword", "Completed")
	return nil
}
Ejemplo n.º 2
0
// removeSessions is used to clear out all the test sessions that are
// created from tests.
func removeSessions(db *db.DB) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		_, err := c.RemoveAll(q)
		return err
	}

	if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 3
0
// removeUser is used to clear out all the test user from the collection.
func removeUser(db *db.DB, publicID string) error {
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		return c.Remove(q)
	}

	if err := db.ExecuteMGO(tests.Context, auth.Collection, f); err != nil {
		return err
	}

	f = func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		_, err := c.RemoveAll(q)
		return err
	}

	if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 4
0
Archivo: media.go Proyecto: Cheetio/api
// GetAllMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func GetAllMedia(context interface{}, db *db.DB) ([]*Media, error) {
	log.Dev(context, "GetAllMedias", "Started")

	var medias []*Media
	f := func(c *mgo.Collection) error {
		log.Dev(context, "GetAllMedias", "MGO : db.%s.findAll()", c.Name)
		return c.Find(nil).All(&medias)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetAllMedias", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetAllMedias", "Completed")
	return medias, nil
}
Ejemplo n.º 5
0
// GetAllCourse deletes a Course from the Course database.
// Returns a non-nil error if it fails.
func GetAllCourse(context interface{}, db *db.DB) (Courses, error) {
	log.Dev(context, "GetAllCourses", "Started")

	var Courses Courses
	f := func(c *mgo.Collection) error {
		log.Dev(context, "GetAllCourses", "MGO : db.%s.findAll()", c.Name)
		return c.Find(nil).All(&Courses)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetAllCourses", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetAllCourses", "Completed")
	return Courses, nil
}
Ejemplo n.º 6
0
Archivo: media.go Proyecto: Cheetio/api
// GetMediaByTitle retrieves a Media from the Media database using the title.
// Returns a non-nil error if it fails.
func GetMediaByTitle(context interface{}, db *db.DB, title string) (*Media, error) {
	log.Dev(context, "GetMediaByTitle", "Started : Media[%s]", title)

	var media Media
	f := func(c *mgo.Collection) error {
		q := bson.M{"title": title}
		log.Dev(context, "GetMediaByTitle", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
		return c.Find(q).One(&media)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetMediaByTitle", err, "Completed : Media[%s]", title)
		return nil, err
	}

	log.Dev(context, "GetMediaByTitle", "Completed : Media[%s]", title)
	return &media, nil
}
Ejemplo n.º 7
0
// GetBySessionID retrieves a session from the session store.
func GetBySessionID(context interface{}, db *db.DB, sessionID string) (*Session, error) {
	log.Dev(context, "GetBySessionID", "Started : SessionID[%s]", sessionID)

	var s Session
	f := func(c *mgo.Collection) error {
		q := bson.M{"session_id": sessionID}
		log.Dev(context, "GetBySessionID", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetBySessionID", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetBySessionID", "Completed")
	return &s, nil
}
Ejemplo n.º 8
0
Archivo: media.go Proyecto: Cheetio/api
// DeleteMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func DeleteMedia(context interface{}, db *db.DB, code string) error {
	log.Dev(context, "DeleteMedia", "Started : Media[%s]", code)

	f := func(c *mgo.Collection) error {
		q := bson.M{"name": code}
		log.Dev(context, "DeleteMedia", "MGO : db.%s.remove(%s)", c.Name, mongo.Query(q))
		return c.Remove(q)
	}

	err := db.ExecuteMGO(context, Collection, f)
	if err != nil {
		log.Error(context, "DeleteMedia", err, "Completed : Media[%s]", code)
		return err
	}

	log.Dev(context, "DeleteMedia", "Completed : Media[%s]", code)
	return nil
}
Ejemplo n.º 9
0
// GetCoursesByAuthor retrieves a Course from the Course database using the author.
// Returns a non-nil error if it fails.
func GetCoursesByAuthor(context interface{}, db *db.DB, author string) (Courses, error) {
	log.Dev(context, "GetCoursesByAuthor", "Started : Author[%s]", author)

	var Courses Courses
	f := func(c *mgo.Collection) error {
		q := bson.M{"author": author}
		log.Dev(context, "GetCoursesByAuthor", "MGO : db.%s.find(%q)", c.Name, mongo.Query(q))
		return c.Find(q).All(&Courses)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetCoursesByAuthor", err, "Completed : Author[%s]", author)
		return nil, err
	}

	log.Dev(context, "GetCoursesByAuthor", "Completed : Author[%s]", author)
	return Courses, nil
}
Ejemplo n.º 10
0
// GetLibraryByCode retrieves a Library from the Library database using the Code.
// Returns a non-nil error if it fails.
func GetLibraryByCode(context interface{}, db *db.DB, code string) (*Library, error) {
	log.Dev(context, "GetLibraryByCode", "Started : Library[%s]", code)

	var Library Library
	f := func(c *mgo.Collection) error {
		q := bson.M{"code": code}
		log.Dev(context, "GetLibraryByCode", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
		return c.Find(q).One(&Library)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetLibraryByCode", err, "Completed : Library[%s]", code)
		return nil, err
	}

	log.Dev(context, "GetLibraryByCode", "Completed : Library[%s]", code)
	return &Library, nil
}
Ejemplo n.º 11
0
// GetByLatest retrieves the latest session for the specified user.
func GetByLatest(context interface{}, db *db.DB, publicID string) (*Session, error) {
	log.Dev(context, "GetByLatest", "Started : PublicID[%s]", publicID)

	var s Session
	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		log.Dev(context, "GetByLatest", "MGO : db.%s.find(%s).sort({\"date_created\": -1}).limit(1)", c.Name, mongo.Query(q))
		return c.Find(q).Sort("-date_created").One(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetByLatest", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetByLatest", "Completed")
	return &s, nil
}
Ejemplo n.º 12
0
Archivo: media.go Proyecto: Cheetio/api
// UpsertMedia add/updates a Media into the Media database.
// Returns a non-nil error if it fails.
func UpsertMedia(context interface{}, db *db.DB, b *Media) error {
	log.Dev(context, "UpsertMedia", "Started : Add Media : %s", mongo.Query(b))

	f := func(c *mgo.Collection) error {
		q := bson.M{"code": b.Code}
		log.Dev(context, "UpsertMedia", "MGO : db.%s.upsert(%s,%s)", c.Name, mongo.Query(q), mongo.Query(b))
		_, err := c.Upsert(q, b)
		return err
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpsertMedia", err, "Completed : Add Media")
		return err
	}

	log.Dev(context, "UpsertMedia", "Completed : Add Media")
	return nil
}
Ejemplo n.º 13
0
// CreateUser adds a new user to the database.
func CreateUser(context interface{}, db *db.DB, u *User) error {
	log.Dev(context, "CreateUser", "Started : PublicID[%s]", u.PublicID)

	if err := u.Validate(); err != nil {
		log.Error(context, "CreateUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		log.Dev(context, "CreateUser", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(&u))
		return c.Insert(u)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "CreateUser", err, "Completed")
		return err
	}

	log.Dev(context, "CreateUser", "Completed")
	return nil
}
Ejemplo n.º 14
0
// UpdateUser updates an existing user to the database.
func UpdateUser(context interface{}, db *db.DB, uu UpdUser) error {
	log.Dev(context, "UpdateUser", "Started : PublicID[%s]", uu.PublicID)

	if err := uu.Validate(); err != nil {
		log.Error(context, "UpdateUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": uu.PublicID}
		upd := bson.M{"$set": bson.M{"full_name": uu.FullName, "email": uu.Email, "status": uu.Status, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUser", "MGO : db.%s.Update(%s, %s)", c.Name, mongo.Query(q), mongo.Query(upd))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUser", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUser", "Completed")
	return nil
}
Ejemplo n.º 15
0
// GetUserByEmail retrieves a user record by using the provided email.
func GetUserByEmail(context interface{}, db *db.DB, email string, activeOnly bool) (*User, error) {
	log.Dev(context, "GetUserByEmail", "Started : Email[%s]", email)

	var user User
	f := func(c *mgo.Collection) error {
		var q bson.M
		if activeOnly {
			q = bson.M{"email": strings.ToLower(email), "status": StatusActive}
		} else {
			q = bson.M{"email": strings.ToLower(email)}
		}
		log.Dev(context, "GetUserByEmail", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetUserByEmail", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserByEmail", "Completed")
	return &user, nil
}
Ejemplo n.º 16
0
// GetUserByPublicID retrieves a user record by using the provided PublicID.
func GetUserByPublicID(context interface{}, db *db.DB, publicID string, activeOnly bool) (*User, error) {
	log.Dev(context, "GetUserByPublicID", "Started : PID[%s]", publicID)

	var user User
	f := func(c *mgo.Collection) error {
		var q bson.M
		if activeOnly {
			q = bson.M{"public_id": publicID, "status": StatusActive}
		} else {
			q = bson.M{"public_id": publicID}
		}
		log.Dev(context, "GetUserByPublicID", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
		return c.Find(q).One(&user)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "GetUserByPublicID", err, "Completed")
		return nil, err
	}

	log.Dev(context, "GetUserByPublicID", "Completed")
	return &user, nil
}
Ejemplo n.º 17
0
// Create adds a new session for the specified user to the database.
func Create(context interface{}, db *db.DB, publicID string, expires time.Duration) (*Session, error) {
	log.Dev(context, "Create", "Started : PublicID[%s]", publicID)

	s := Session{
		SessionID:   uuid.New(),
		PublicID:    publicID,
		DateExpires: time.Now().Add(expires),
		DateCreated: time.Now(),
	}

	f := func(c *mgo.Collection) error {
		log.Dev(context, "Create", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(&s))
		return c.Insert(&s)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "Create", err, "Completed")
		return nil, err
	}

	log.Dev(context, "Create", "Completed : SessionID[%s]", s.SessionID)
	return &s, nil
}
Ejemplo n.º 18
0
// UpdateUserStatus changes the status of a user to make them active or disabled.
func UpdateUserStatus(context interface{}, db *db.DB, publicID string, status int) error {
	log.Dev(context, "UpdateUserStatus", "Started : PublicID[%s] Status[%d]", publicID, status)

	if status != StatusActive && status != StatusDisabled {
		err := errors.New("Invalid status code")
		log.Error(context, "LoginUser", err, "Completed")
		return err
	}

	f := func(c *mgo.Collection) error {
		q := bson.M{"public_id": publicID}
		upd := bson.M{"$set": bson.M{"status": status, "modified_at": time.Now().UTC()}}
		log.Dev(context, "UpdateUserStatus", "MGO : db.%s.Update(%s, %s)", c.Name, mongo.Query(q), mongo.Query(upd))
		return c.Update(q, upd)
	}

	if err := db.ExecuteMGO(context, Collection, f); err != nil {
		log.Error(context, "UpdateUserStatus", err, "Completed")
		return err
	}

	log.Dev(context, "UpdateUserStatus", "Completed")
	return nil
}