Files
Umbraco-CMS/src/Umbraco.Core/Services/UserServiceExtensions.cs
Bjarke Berg 452097d975 Fixed issue with Paths-integers being converted using local culture. (#11180)
* Fixed issue with Paths-integers being converted using local culture.

* Align with the old implementation

* Use int.TryParse insteaad of TryConvertTo when we do not want culture specific parsing

* More fixes for cultures and fixed wrong test. Users should be part of all groups to have access

* Fix casing for requested file

* Force tests to not use NLS

* try force tests to not use NLS

* try force tests to not use NLS

* Force tests on windows to run ICU

* More fixes for invariant int parsing

* Change key on actions/emptyRecycleBin, so the casing aligns with the view file, that is named emptyrecyclebin.html

* Fixed casing issue

* use Attempt to align with other code
2021-09-24 16:42:31 +01:00

89 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Extensions
{
public static class UserServiceExtensions
{
public static EntityPermission GetPermissions(this IUserService userService, IUser user, string path)
{
var ids = path.Split(Constants.CharArrays.Comma, StringSplitOptions.RemoveEmptyEntries)
.Select(x => int.TryParse(x, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? Attempt<int>.Succeed(value) : Attempt<int>.Fail())
.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();
}
/// <summary>
/// Get explicitly assigned permissions for a group and optional node Ids
/// </summary>
/// <param name="service"></param>
/// <param name="group"></param>
/// <param name="fallbackToDefaultPermissions">
/// Flag indicating if we want to include the default group permissions for each result if there are not explicit permissions set
/// </param>
/// <param name="nodeIds">Specifying nothing will return all permissions for all nodes</param>
/// <returns>An enumerable list of <see cref="EntityPermission"/></returns>
public static EntityPermissionCollection GetPermissions(this IUserService service, IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds)
{
return service.GetPermissions(new[] {group}, fallbackToDefaultPermissions, nodeIds);
}
/// <summary>
/// Gets the permissions for the provided group and path
/// </summary>
/// <param name="service"></param>
/// <param name="group"></param>
/// <param name="path">Path to check permissions for</param>
/// <param name="fallbackToDefaultPermissions">
/// Flag indicating if we want to include the default group permissions for each result if there are not explicit permissions set
/// </param>
public static EntityPermissionSet GetPermissionsForPath(this IUserService service, IUserGroup group, string path, bool fallbackToDefaultPermissions = false)
{
return service.GetPermissionsForPath(new[] { group }, path, fallbackToDefaultPermissions);
}
/// <summary>
/// Remove all permissions for this user group for all nodes specified
/// </summary>
/// <param name="userService"></param>
/// <param name="groupId"></param>
/// <param name="entityIds"></param>
public static void RemoveUserGroupPermissions(this IUserService userService, int groupId, params int[] entityIds)
{
userService.ReplaceUserGroupPermissions(groupId, new char[] {}, entityIds);
}
/// <summary>
/// Remove all permissions for this user group for all nodes
/// </summary>
/// <param name="userService"></param>
/// <param name="groupId"></param>
public static void RemoveUserGroupPermissions(this IUserService userService, int groupId)
{
userService.ReplaceUserGroupPermissions(groupId, new char[] { });
}
public static IEnumerable<IProfile> GetProfilesById(this IUserService userService, params int[] ids)
{
var fullUsers = userService.GetUsersById(ids);
return fullUsers.Select(user =>
{
var asProfile = user as IProfile;
return asProfile ?? new UserProfile(user.Id, user.Name);
});
}
}
}