public void will_enforce_single_role() { var singleRolePolicy = new Policy("single", true, new Role("role1"), new Role("role2")); var multiRolePolicy = new Policy("multi", false, new Role("role1"), new Role("role2")); var map = new PolicyMap(singleRolePolicy); map.AddAdditionalPolicy(multiRolePolicy); var user = new UserDetails() { UserId = Guid.NewGuid(), RoleNames = new[] { "role1", "role2" } }; var userPolicy = map.GetUserPolicy(user); //default single role policy Assert.Collection(userPolicy.Roles, r => string.Equals("role1", r)); user.PolicyName = "single"; var userPolicy2 = map.GetUserPolicy(user); //explict single role policy Assert.Collection(userPolicy2.Roles, r => string.Equals("role1", r)); user.PolicyName = "multi"; var userPolicy3 = map.GetUserPolicy(user);//explict multi role policy Assert.Collection(userPolicy3.Roles, r => string.Equals("role1", r), r => string.Equals("role2", r)); }
public PolicyMapTest() { //test and usage example //building a base policy var policy = new Policy(_defaultPolicyName, false, new Role(_userRoleName, new Permission(typeof(AddItem)), new Permission(typeof(DisplayItem))), new Role(_adminRoleName, new Permission(typeof(AddItem)), new Permission(typeof(DisplayItem)), new Permission(typeof(DeleteItem)), new Permission(_customPermissionName)) ); //building an optional additional policy _backupRole = new Role(_backupRoleName, new Permission(typeof(ExportItem))); var backupAgentPolicy = new Policy(_backupPolicyName, false, _backupRole); //creating the base policy map _policyMap = new PolicyMap(policy, _applicationName); _policyMap.AddAdditionalPolicy(backupAgentPolicy); //create usesrs // In usage user details and roles will be recieved from the authenication and permissions system _systemUser = new UserDetails { UserId = Guid.NewGuid(), UserName = "******", PolicyName = _defaultPolicyName, RoleNames = new string[] { _userRoleName, _customRoleName } }; _adminUser = new UserDetails { UserId = Guid.NewGuid(), UserName = "******", PolicyName = _defaultPolicyName, RoleNames = new string[] { _adminRoleName } }; _backupAgent = new UserDetails { UserId = Guid.NewGuid(), UserName = "******", PolicyName = _backupPolicyName, RoleNames = new string[] { _backupRoleName } }; dispatcher = new PolicyDispatcher(new Dispatcher(nameof(PolicyMapTest)), () => CurrentPolicy); dispatcher.Subscribe <AddItem>(this); dispatcher.Subscribe <DisplayItem>(this); dispatcher.Subscribe <DeleteItem>(this); dispatcher.Subscribe <ExportItem>(this); dispatcher.Subscribe <OtherCmd>(this); dispatcher.Subscribe <OtherMsg>(this); }