Beispiel #1
0
 public UserGroup GetUserGroupByName(string name)
 {
     using (var uow = UserGroupHive.Create())
     {
         return(GetUserGroupByName(name, uow));
     }
 }
Beispiel #2
0
 public IEnumerable <UserGroup> GetUserGroupsByName(string[] names)
 {
     using (var uow = UserGroupHive.Create())
     {
         return(GetUserGroupsByName(names, uow));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Removes the specified user names from the specified roles for the configured applicationName.
        /// </summary>
        /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
        /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (roleNames.Any(rolename => !RoleExists(rolename)))
            {
                throw new ProviderException("Role name not found.");
            }

            if (usernames.Any(username => roleNames.Any(rolename => !IsUserInRole(username, rolename))))
            {
                throw new ProviderException("User is not in role.");
            }

            using (var uow2 = UserGroupHive.Create())
            {
                var userGroups = GetUserGroupsByName(roleNames, uow2);

                foreach (var userGroup in userGroups)
                {
                    var relations = uow2.Repositories.GetLazyChildRelations(userGroup.Id, FixedRelationTypes.UserGroupRelationType)
                                    .Where(x => usernames.Contains(((TypedEntity)x.Destination).Attribute <string>(UserSchema.UsernameAlias), StringComparer.InvariantCultureIgnoreCase));

                    foreach (var relation in relations)
                    {
                        uow2.Repositories.RemoveRelation(relation);
                    }
                }

                uow2.Complete();

                //remove the role from the current identity
                RemoveRolesFromCurrentIdentity(usernames, roleNames);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Gets a list of the roles that a specified user is in for the configured applicationName.
        /// </summary>
        /// <param name="username">The user to return a list of roles for.</param>
        /// <returns>
        /// A string array containing the names of all the roles that the specified user is in for the configured applicationName.
        /// </returns>
        public override string[] GetRolesForUser(string username)
        {
            //before we go looking in the db, we need to check the current identity
            if (Thread.CurrentPrincipal.Identity is UmbracoBackOfficeIdentity)
            {
                var identity = (UmbracoBackOfficeIdentity)Thread.CurrentPrincipal.Identity;
                if (identity.Name.Trim() == username.Trim())
                {
                    return(identity.Roles);
                }
            }

            using (var uow1 = UserHive.Create())
                using (var uow2 = UserGroupHive.Create())
                {
                    var user = uow1.Repositories.GetEntityByRelationType <User>(FixedRelationTypes.DefaultRelationType, FixedHiveIds.UserVirtualRoot)
                               .SingleOrDefault(x => x.Username == username);

                    var userGroups = user != null?uow2.Repositories.GetLazyParentRelations(user.Id, FixedRelationTypes.UserGroupRelationType)
                                     .Select(x => ((TypedEntity)x.Source).Attribute <string>(NodeNameAttributeDefinition.AliasValue))
                                     .ToArray() : new string[0];

                    return(userGroups);
                }
        }
Beispiel #5
0
 /// <summary>
 /// Gets a value indicating whether the specified role name already exists in the role data source for the configured applicationName.
 /// </summary>
 /// <param name="roleName">The name of the role to search for in the data source.</param>
 /// <returns>
 /// true if the role name already exists in the data source for the configured applicationName; otherwise, false.
 /// </returns>
 public override bool RoleExists(string roleName)
 {
     using (var uow1 = UserGroupHive.Create())
     {
         return(uow1.Repositories.GetEntityByRelationType <UserGroup>(FixedRelationTypes.DefaultRelationType, FixedHiveIds.UserGroupVirtualRoot)
                .Any(x => x.Name.ToLower() == roleName.ToLower()));
     }
 }
Beispiel #6
0
 /// <summary>
 /// Gets a list of all the roles for the configured applicationName.
 /// </summary>
 /// <returns>
 /// A string array containing the names of all the roles stored in the data source for the configured applicationName.
 /// </returns>
 public override string[] GetAllRoles()
 {
     using (var uow1 = UserGroupHive.Create())
     {
         return(uow1.Repositories
                .GetEntityByRelationType <UserGroup>(FixedRelationTypes.DefaultRelationType, FixedHiveIds.UserVirtualRoot)
                .Select(x => x.Name).ToArray());
     }
 }
Beispiel #7
0
        /// <summary>
        /// Gets a list of users in the specified role for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to get the list of users for.</param>
        /// <returns>
        /// A string array containing the names of all the users who are members of the specified role for the configured applicationName.
        /// </returns>
        public override string[] GetUsersInRole(string roleName)
        {
            using (var uow1 = UserGroupHive.Create())
            {
                var userGroup = GetUserGroupByName(roleName, uow1);

                return(uow1.Repositories.GetLazyChildRelations(userGroup.Id, FixedRelationTypes.UserGroupRelationType)
                       .Select(x => ((TypedEntity)x.Destination).Attribute <string>(UserSchema.UsernameAlias))
                       .ToArray());
            }
        }
Beispiel #8
0
        /// <summary>
        /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
        /// </summary>
        /// <param name="username">The user name to search for.</param>
        /// <param name="roleName">The role to search in.</param>
        /// <returns>
        /// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
        /// </returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            //before we go looking in the db, we need to check the current identity
            if (Thread.CurrentPrincipal.Identity is UmbracoBackOfficeIdentity)
            {
                return(((UmbracoBackOfficeIdentity)Thread.CurrentPrincipal.Identity).Roles.Contains(roleName));
            }

            using (var uow1 = UserGroupHive.Create())
            {
                var userGroup = GetUserGroupByName(roleName, uow1);

                return(uow1.Repositories.GetLazyChildRelations(userGroup.Id, FixedRelationTypes.UserGroupRelationType)
                       .Any(x => ((TypedEntity)x.Destination).Attribute <string>(UserSchema.UsernameAlias) == username));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Adds a new role to the data source for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to create.</param>
        public override void CreateRole(string roleName)
        {
            if (RoleExists(roleName))
            {
                throw new ProviderException("Role name already exists.");
            }

            using (var uow1 = UserGroupHive.Create())
            {
                var role = new UserGroup()
                {
                    Name = roleName
                };

                uow1.Repositories.AddOrUpdate(role);
                uow1.Complete();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Adds the specified user names to the specified roles for the configured applicationName.
        /// </summary>
        /// <param name="usernames">A string array of user names to be added to the specified roles.</param>
        /// <param name="roleNames">A string array of the role names to add the specified user names to.</param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            if (usernames.Length == 0)
            {
                return;
            }

            if (roleNames.Length == 0)
            {
                return;
            }

            using (var uow1 = UserHive.Create())
                using (var uow2 = UserGroupHive.Create())
                {
                    foreach (var username in usernames)
                    {
                        var user = uow1.Repositories
                                   .GetEntityByRelationType <User>(FixedRelationTypes.DefaultRelationType, FixedHiveIds.UserVirtualRoot)
                                   .SingleOrDefault(x => x.Username == username);

                        var userGroups = GetUserGroupsByName(roleNames, uow2);

                        if (user == null || userGroups.Count() == 0)
                        {
                            continue;
                        }

                        // Add any new user group
                        foreach (var userGroup in userGroups)
                        {
                            uow2.Repositories.ChangeOrCreateRelationMetadata(userGroup.Id, user.Id, FixedRelationTypes.UserGroupRelationType);
                        }

                        uow2.Repositories.AddOrUpdate(user);
                    }

                    uow1.Complete();

                    AddRolesToCurrentIdentity(roleNames, usernames);
                }
        }
Beispiel #11
0
        /// <summary>
        /// Removes a role from the data source for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to delete.</param>
        /// <param name="throwOnPopulatedRole">If true, throw an exception if <paramref name="roleName"/> has one or more members and do not delete <paramref name="roleName"/>.</param>
        /// <returns>
        /// true if the role was successfully deleted; otherwise, false.
        /// </returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            if (!RoleExists(roleName))
            {
                throw new ProviderException("Role name already exists.");
            }

            if (throwOnPopulatedRole && GetUsersInRole(roleName).Length > 0)
            {
                throw new ProviderException("Cannot delete a populated role.");
            }

            try
            {
                // Remove users from role
                var users = GetUsersInRole(roleName);
                RemoveUsersFromRoles(users, new[] { roleName });

                // Delete the role
                using (var uow1 = UserGroupHive.Create())
                {
                    var userGroup = GetUserGroupByName(roleName, uow1);
                    if (userGroup != null)
                    {
                        uow1.Repositories.Delete <UserGroup>(userGroup.Id);
                        uow1.Complete();
                    }
                }

                //remove the role from the current identity
                RemoveRolesFromCurrentIdentity(new[] { roleName });

                return(true);
            }
            catch
            {
                return(false);
            }
        }