So here are the business rules:
- A basic user can edit their own tasks, but not the tasks of another user.
- An admin user can edit their own tasks and the tasks of other users.
public class User : Entity { protected string _username; protected Role _role; public virtual string Username { get { return _username; } set { _username = value; } } public virtual Role Role { get { return _role; } set { _role = value; } } public virtual bool CanEditTask(Task task) { if (task.AssignedTo == this && _role.Permissions.Select(permission => permission.Key).Contains(PermissionKey.EditOwnTask)) return true; if (_role.Permissions.Select(permission => permission.Key).Contains(PermissionKey.EditAnotherUsersTask)) return true; return false; } } public class Role : Entity { protected string _rolename; private IListSo the section to focus on is the CanEditTask(Task) method of the User entity. Here is the code for the test, which creates the permissions for editing tasks, creates the basic and admin roles with the required permissions, creates the users with their roles and creates tasks assigned to those user. It then asserts that the correct users can edit the required users tasks._permissions; public virtual string RoleName { get { return _rolename; } set { _rolename = value; } } public virtual IList Permissions { get { return _permissions; } set { _permissions = value; } } } public class Permission : Entity { protected string _key; protected string _description; public virtual string Key { get { return _key; } set { _key = value; } } public virtual string Description { get { return _description; } set { _description = value; } } } public class Task : Entity { private string _description; private User _assignedTo; public virtual string Description { get { return _description; } set { _description = value; } } public virtual User AssignedTo { get { return _assignedTo; } set { _assignedTo = value; } } } public static class PermissionKey { public const string EditAnotherUsersTask = "EditAnotherUsersTask"; public const string EditOwnTask = "EditOwnTask"; public const string EditTaskForRole = "EditTaskForRole"; }
[TestClass] public class UserTests { [TestMethod] public void CheckUserCannotEditAnotherUsersTask() { Permission editAnotherUsersTaskPermission = new Permission() { Key = PermissionKey.EditAnotherUsersTask, Description = "Edit another user's task" }; Permission editOwnTaskPermission = new Permission() { Key = PermissionKey.EditOwnTask, Description = "Edit own task" }; Role basicRole = new Role() { RoleName = "Basic User", Permissions = new ListAnd finally, here is the NHibernate mapping files to map all of this:() { editOwnTaskPermission } }; Role adminRole = new Role() { RoleName = "Admin User", Permissions = new List () { editAnotherUsersTaskPermission } }; User barryBasic = new User() { Username = "Barry Basic", Role = basicRole }; User arnoldAdmin = new User() { Username = "Arnold Admin", Role = adminRole }; Task arnoldsTask = new Task() { Description = "Arnold's Task", AssignedTo = arnoldAdmin }; Task barrysTask = new Task() { Description = "Barry's Task", AssignedTo = barryBasic }; Assert.IsTrue(barryBasic.CanEditTask(barrysTask)); Assert.IsFalse(barryBasic.CanEditTask(arnoldsTask)); Assert.IsTrue(arnoldAdmin.CanEditTask(barrysTask)); } }
No comments:
Post a Comment