using System; using System.Linq; using System.Web.Security; using Umbraco.Core.Models.Membership; namespace Umbraco.Core.Services { public static class UserServiceExtensions { public static EntityPermission GetPermissions(this IUserService userService, IUser user, string path) { var ids = path.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.TryConvertTo()) .Where(x => x.Success) .Select(x => x.Result) .ToArray(); if (ids.Length == 0) throw new InvalidOperationException("The path: " + path + " could not be parsed into an array of integers or the path was empty"); return userService.GetPermissions(user, ids[ids.Length - 1]).FirstOrDefault(); } /// /// Get explicitly assigned permissions for a group and optional node Ids /// /// /// /// /// Flag indicating if we want to include the default group permissions for each result if there are not explicit permissions set /// /// Specifying nothing will return all permissions for all nodes /// An enumerable list of public static EntityPermissionCollection GetPermissions(this IUserService service, IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds) { return service.GetPermissions(new[] {group}, fallbackToDefaultPermissions, nodeIds); } /// /// Gets the permissions for the provided group and path /// /// /// /// Path to check permissions for /// /// Flag indicating if we want to include the default group permissions for each result if there are not explicit permissions set /// public static EntityPermissionSet GetPermissionsForPath(this IUserService service, IUserGroup group, string path, bool fallbackToDefaultPermissions = false) { return service.GetPermissionsForPath(new[] { group }, path, fallbackToDefaultPermissions); } /// /// Remove all permissions for this user group for all nodes specified /// /// /// /// public static void RemoveUserGroupPermissions(this IUserService userService, int groupId, params int[] entityIds) { userService.ReplaceUserGroupPermissions(groupId, new char[] {}, entityIds); } /// /// Remove all permissions for this user group for all nodes /// /// /// public static void RemoveUserGroupPermissions(this IUserService userService, int groupId) { userService.ReplaceUserGroupPermissions(groupId, new char[] { }); } /// /// Maps a custom provider's information to an umbraco user account /// /// /// /// /// To maintain compatibility we have to check the login name if the provider key lookup fails but otherwise /// we'll store the provider user key in the login column. /// internal static IUser CreateUserMappingForCustomProvider(this IUserService userService, MembershipUser member) { if (member == null) throw new ArgumentNullException("member"); var valToLookup = member.ProviderUserKey == null ? member.UserName : member.ProviderUserKey.ToString(); var found = userService.GetByUsername(valToLookup); if (found == null && member.ProviderUserKey != null) { //try by username found = userService.GetByUsername(member.UserName); } if (found == null) { var user = new User( member.UserName, member.Email ?? Guid.NewGuid().ToString("N") + "@example.com", //email cannot be empty member.ProviderUserKey == null ? member.UserName : member.ProviderUserKey.ToString(), Guid.NewGuid().ToString("N")); //pass cannot be empty userService.Save(user); return user; } return found; } } }