* Rename Umbraco.Core namespace to Umbraco.Cms.Core * Move extension methods in core project to Umbraco.Extensions * Move extension methods in core project to Umbraco.Extensions * Rename Umbraco.Examine namespace to Umbraco.Cms.Examine * Move examine extensions to Umbraco.Extensions namespace * Reflect changed namespaces in Builder and fix unit tests * Adjust namespace in Umbraco.ModelsBuilder.Embedded * Adjust namespace in Umbraco.Persistence.SqlCe * Adjust namespace in Umbraco.PublishedCache.NuCache * Align namespaces in Umbraco.Web.BackOffice * Align namespaces in Umbraco.Web.Common * Ensure that SqlCeSupport is still enabled after changing the namespace * Align namespaces in Umbraco.Web.Website * Align namespaces in Umbraco.Web.UI.NetCore * Align namespaces in Umbraco.Tests.Common * Align namespaces in Umbraco.Tests.UnitTests * Align namespaces in Umbraco.Tests.Integration * Fix errors caused by changed namespaces * Fix integration tests * Undo the Umbraco.Examine.Lucene namespace change This breaks integration tests on linux, since the namespace wont exists there because it's only used on windows. * Fix merge * Fix Merge
80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
// Copyright (c) Umbraco.
|
|
// See LICENSE for more details.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
namespace Umbraco.Extensions
|
|
{
|
|
public static class UmbracoUserExtensions
|
|
{
|
|
public static IEnumerable<string> GetPermissions(this IUser user, string path, IUserService userService)
|
|
{
|
|
return userService.GetPermissionsForPath(user, path).GetAllPermissions();
|
|
}
|
|
|
|
public static bool HasSectionAccess(this IUser user, string app)
|
|
{
|
|
var apps = user.AllowedSections;
|
|
return apps.Any(uApp => uApp.InvariantEquals(app));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether this user is the 'super' user.
|
|
/// </summary>
|
|
public static bool IsSuper(this IUser user)
|
|
{
|
|
if (user == null) throw new ArgumentNullException(nameof(user));
|
|
return user.Id == Constants.Security.SuperUserId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether this user belongs to the administrators group.
|
|
/// </summary>
|
|
/// <remarks>The 'super' user does not automatically belongs to the administrators group.</remarks>
|
|
public static bool IsAdmin(this IUser user)
|
|
{
|
|
if (user == null) throw new ArgumentNullException(nameof(user));
|
|
return user.Groups != null && user.Groups.Any(x => x.Alias == Constants.Security.AdminGroupAlias);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the culture info associated with this user, based on the language they're assigned to in the back office
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="textService"></param>
|
|
/// <param name="globalSettings"></param>
|
|
/// <returns></returns>
|
|
public static CultureInfo GetUserCulture(this IUser user, ILocalizedTextService textService, GlobalSettings globalSettings)
|
|
{
|
|
if (user == null) throw new ArgumentNullException(nameof(user));
|
|
if (textService == null) throw new ArgumentNullException(nameof(textService));
|
|
return GetUserCulture(user.Language, textService, globalSettings);
|
|
}
|
|
|
|
public static CultureInfo GetUserCulture(string userLanguage, ILocalizedTextService textService, GlobalSettings globalSettings)
|
|
{
|
|
try
|
|
{
|
|
var culture = CultureInfo.GetCultureInfo(userLanguage.Replace("_", "-"));
|
|
// TODO: This is a hack because we store the user language as 2 chars instead of the full culture
|
|
// which is actually stored in the language files (which are also named with 2 chars!) so we need to attempt
|
|
// to convert to a supported full culture
|
|
var result = textService.ConvertToSupportedCultureWithRegionCode(culture);
|
|
return result;
|
|
}
|
|
catch (CultureNotFoundException)
|
|
{
|
|
//return the default one
|
|
return CultureInfo.GetCultureInfo(globalSettings.DefaultUILanguage);
|
|
}
|
|
}
|
|
}
|
|
}
|