Controllers Update
This commit is contained in:
@@ -10,11 +10,12 @@ namespace Business.Business.Sales
|
|||||||
{
|
{
|
||||||
class ProductController : IController<Product>
|
class ProductController : IController<Product>
|
||||||
{
|
{
|
||||||
private LuminousContext context = new LuminousContext();
|
private LuminousContext context;
|
||||||
private User currentUser;
|
private User currentUser;
|
||||||
public ProductController(User currenUser)
|
public ProductController(User currenUser)
|
||||||
{
|
{
|
||||||
this.currentUser = currenUser;
|
this.currentUser = currenUser;
|
||||||
|
context = new LuminousContext();
|
||||||
}
|
}
|
||||||
public ICollection<Product> GetAll()
|
public ICollection<Product> GetAll()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,23 +10,56 @@ namespace Business.Business.UserManagment.Controllers
|
|||||||
{
|
{
|
||||||
class RoleController : IReadOnlyController<Role>
|
class RoleController : IReadOnlyController<Role>
|
||||||
{
|
{
|
||||||
private LuminousContext context = new LuminousContext();
|
private LuminousContext context;
|
||||||
|
private User currentUser;
|
||||||
|
public RoleController(User currentUser)
|
||||||
|
{
|
||||||
|
this.context = new LuminousContext();
|
||||||
|
this.currentUser = currentUser;
|
||||||
|
}
|
||||||
public ICollection<Role> GetAll()
|
public ICollection<Role> GetAll()
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.Role.ToList();
|
return context.Role.ToList();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public Role Get(int id)
|
public Role Get(int id)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.Role.Find(id);
|
return context.Role.Find(id);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public Role Get(string name)
|
public Role Get(string name)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.Role.FirstOrDefault(u => u.Name == name);
|
return context.Role.FirstOrDefault(u => u.Name == name);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public ICollection<Role> GetByApproximateName(string name)
|
public ICollection<Role> GetByApproximateName(string name)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.Role.Where(u => u.Name.Contains(name)).ToList();
|
return context.Role.Where(u => u.Name.Contains(name)).ToList();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,27 +9,59 @@ namespace Business.Business.UserManagment
|
|||||||
{
|
{
|
||||||
public class UserController : IController<User>
|
public class UserController : IController<User>
|
||||||
{
|
{
|
||||||
private LuminousContext context = new LuminousContext();
|
private LuminousContext context;
|
||||||
private RoleController rolectrl = new RoleController();
|
private RoleController rolectrl;
|
||||||
|
private User currentUser;
|
||||||
|
public UserController()
|
||||||
|
{
|
||||||
|
this.context = new LuminousContext();
|
||||||
|
}
|
||||||
|
public UserController(User currentUser)
|
||||||
|
{
|
||||||
|
this.currentUser = currentUser;
|
||||||
|
this.context = new LuminousContext();
|
||||||
|
this.rolectrl = new RoleController(currentUser);
|
||||||
|
}
|
||||||
public ICollection<User> GetAll()
|
public ICollection<User> GetAll()
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.User.ToList();
|
return context.User.ToList();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void CheckIfUserEverCreated()
|
public void CheckIfUserEverCreated()
|
||||||
{
|
{
|
||||||
if (!GetAll().Any())
|
if (!context.User.ToList().Any())
|
||||||
{
|
{
|
||||||
throw new ArgumentException("No users in the database!");
|
throw new ArgumentException("No users in the database!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public User Get(int id)
|
public User Get(int id)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.User.Find(id);
|
return context.User.Find(id);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public User Get(string name)
|
public User Get(string name)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.User.FirstOrDefault(u => u.Name == name);
|
return context.User.FirstOrDefault(u => u.Name == name);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public User ValidatePassword(string password)
|
public User ValidatePassword(string password)
|
||||||
{
|
{
|
||||||
var user = context.User.FirstOrDefault();
|
var user = context.User.FirstOrDefault();
|
||||||
@@ -40,10 +72,25 @@ namespace Business.Business.UserManagment
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
public ICollection<User> GetByApproximateName(string name)
|
public ICollection<User> GetByApproximateName(string name)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
return context.User.Where(u => u.Name.Contains(name)).ToList();
|
return context.User.Where(u => u.Name.Contains(name)).ToList();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void RegisterItem(string name, string password)
|
||||||
|
{
|
||||||
|
var user = new User(name, password, 1);
|
||||||
|
context.User.Add(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
public void RegisterItem(string name, string password, int roleId)
|
public void RegisterItem(string name, string password, int roleId)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
if (GetAll().Where(u => u.Name == name).Any())
|
if (GetAll().Where(u => u.Name == name).Any())
|
||||||
{
|
{
|
||||||
@@ -68,7 +115,14 @@ namespace Business.Business.UserManagment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void RegisterItem(string name, string password, string roleName)
|
public void RegisterItem(string name, string password, string roleName)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
if (GetAll().Where(u => u.Name == name).Any())
|
if (GetAll().Where(u => u.Name == name).Any())
|
||||||
{
|
{
|
||||||
@@ -93,7 +147,14 @@ namespace Business.Business.UserManagment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateName(int id, string newName)
|
public void UpdateName(int id, string newName)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.Id == 3)
|
||||||
{
|
{
|
||||||
var user = Get(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -113,7 +174,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("No user with such id");
|
throw new ArgumentException("No user with such id");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateName(string oldName, string newName)
|
public void UpdateName(string oldName, string newName)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
if (oldName != newName)
|
if (oldName != newName)
|
||||||
{
|
{
|
||||||
@@ -132,9 +200,15 @@ namespace Business.Business.UserManagment
|
|||||||
{
|
{
|
||||||
throw new ArgumentException("Usernames match. Please use another username!");
|
throw new ArgumentException("Usernames match. Please use another username!");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void UpdatePassword(int id, string newPassword)
|
public void UpdatePassword(int id, string newPassword)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -154,7 +228,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdatePassword(string name, string newPassword)
|
public void UpdatePassword(string name, string newPassword)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(name);
|
var user = Get(name);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -174,7 +255,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateRole(int id, int RoleId)
|
public void UpdateRole(int id, int RoleId)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -195,7 +283,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateRole(int id, string roleName)
|
public void UpdateRole(int id, string roleName)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -216,7 +311,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateRole(string name, int roleId)
|
public void UpdateRole(string name, int roleId)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(name);
|
var user = Get(name);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -237,7 +339,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void UpdateRole(string name, string roleName)
|
public void UpdateRole(string name, string roleName)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(name);
|
var user = Get(name);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -258,7 +367,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -271,7 +387,14 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void Delete(string name)
|
public void Delete(string name)
|
||||||
|
{
|
||||||
|
if (currentUser != null || currentUser.RoleId == 3)
|
||||||
{
|
{
|
||||||
var user = Get(name);
|
var user = Get(name);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
@@ -284,5 +407,10 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Insufficient Role!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user