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; } } }