Files
cms_thermo_active/Step.Database/Controllers/UsersController.cs
T
CMS4390\marantalu 2b9f74a1b2 * Added Logger and Exception manager
* Added autocreate database if not exists
2017-12-05 17:21:10 +01:00

72 lines
2.0 KiB
C#

using System;
using System.Linq;
using System.Web.Helpers;
using Step.Model;
namespace Step.Database.Controllers
{
public class UsersController : IDisposable
{
private DatabaseContext dbCtx;
public UsersController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
public void Create(string userName, string password, string firstName, string lastName, int roleId)
{
// Create a new user model with params
UserModel user = new UserModel()
{
Username = userName,
Password = Crypto.HashPassword(password),
FirstName = firstName,
LastName = lastName,
RoleId = roleId,
SecurityStamp = Guid.NewGuid().ToString()
};
// Add to database
dbCtx.Users.Add(user);
// Commit changes
dbCtx.SaveChanges();
}
public UserModel Find(int id)
{
// Find user by Id with Role object included
return dbCtx.Users.Include("Role").Where(u => u.UserId == id).FirstOrDefault();
}
public UserModel Find(string username)
{
// Find user by Id with Role object included
return dbCtx.Users.Include("Role").Where(u => u.Username == username).FirstOrDefault();
}
public UserModel Find(string username, string password)
{
// Find if username exists
UserModel user = Find(username);
if (user != null)
{
// Check if the passwords match
if (Crypto.VerifyHashedPassword(user.GetPassword(), password) != true)
{
return null;
}
}
return user;
}
}
}