akester/go-shell

Fork Project
[550d2ec7c] Breaking apart files functions where it makes sense
/*
 * go-shell - Storehouse jailed shell program
 *
 * For full license and copyright information, see the README and COPYING file.
 */

package main

import (
	"encoding/json"
	"errors"
	"github.com/jinzhu/gorm"
)

/**
 * User structure, maintained by Doctrine in Storehouse's main code.
 * This is just here so we can easilly access the entity with Gorm.
 */
type User struct {
	UserId				uint		`gorm:"column:id"`
	Username			string		`gorm:"column:username"`
	RolesString		string		`gorm:"column:roles"`
	Roles				[]string
}

/**
 * Check if a user is in good standing.
 */
func (user User) IsActive() bool {
	var active = false;
	for _, role := range user.Roles {
		if role == "ROLE_USER" {
			active = true
		}
	}

	return active
}

/**
 * Get a user given a username.
 *
 * @param *string username
 *         Username to load.
 * @param *gorm.DB
 *         Database connection.
 */
func getUser(username *string, db *gorm.DB) (User, error) {
	var user User
	db.First(&user, "username = ?", *username)

	if user.Username == "" {
		err := errors.New("User not found.");
		return user, err
	}

	/* Parse out the roles of the user */
	var arr []string
	_ = json.Unmarshal([]byte(user.RolesString), &arr)
	user.Roles = arr;

	return user, nil
}