/// <summary> /// Adds a <paramref name="role"/> to the <paramref name="owner"/>. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <param name="owner">The <see cref="RoleOwner"/>.</param> /// <param name="role">The <see cref="Role"/>.</param> public void AssignRole(IMansionContext context, RoleOwner owner, Role role) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (owner == null) throw new ArgumentNullException("owner"); if (role == null) throw new ArgumentNullException("role"); // get the repository var repository = context.Repository; // retrieve the required nodes var ownerNode = RetrieveRoleOwnerNode(context, owner, repository); var roleNode = RetrieveRoleNode(context, role, repository); // update the role owner repository.UpdateNode(context, ownerNode, new PropertyBag { {"assignedRoleGuids", string.Join(",", new[] {ownerNode.Get(context, "assignedRoleGuids", string.Empty), roleNode.Get<string>(context, "guid")})} }); }
/// <summary> /// Retrieves the role owner node. /// </summary> /// <param name="context"></param> /// <param name="owner"></param> /// <param name="repository"></param> /// <returns></returns> private static Node RetrieveRoleOwnerNode(IMansionContext context, RoleOwner owner, IRepository repository) { // if the user is not authenticated, retrieve the visiter from the database Node node; if (owner.Id == Guid.Empty) { node = repository.RetrieveSingleNode(context, new PropertyBag { {"baseType", "RoleOwner"}, {"key", "AnonymousUser"}, {"bypassAuthorization", true}, {StorageOnlyQueryComponent.PropertyKey, true} }); if (node == null) throw new InvalidOperationException("Could not find the anonymous user node"); } else { node = repository.RetrieveSingleNode(context, new PropertyBag { {"baseType", "RoleOwner"}, {"guid", owner.Id}, {"bypassAuthorization", true}, {StorageOnlyQueryComponent.PropertyKey, true} }); if (node == null) throw new InvalidOperationException(string.Format("Could not find role owner with foreign ID {0} in repository, please sync tables", owner.Id)); } return node; }
/// <summary> /// Removes a <paramref name="role"/> from the <paramref name="owner"/>. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <param name="owner">The <see cref="RoleOwner"/>.</param> /// <param name="role">The <see cref="Role"/>.</param> public void RemoveRole(IMansionContext context, RoleOwner owner, Role role) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (owner == null) throw new ArgumentNullException("owner"); if (role == null) throw new ArgumentNullException("role"); // get the repository var repository = context.Repository; // retrieve the required nodes var ownerNode = RetrieveRoleOwnerNode(context, owner, repository); var roleNode = RetrieveRoleNode(context, role, repository); // build the userGuids array var assignedRoleList = (ownerNode.Get(context, "assignedRoleGuids", string.Empty).Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)).ToList(); assignedRoleList.Remove(roleNode.Get<string>(context, "guid")); // update the user group repository.UpdateNode(context, ownerNode, new PropertyBag { {"assignedRoleGuids", string.Join(",", assignedRoleList)} }); }