using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net; using Umbraco.Core.Cache; using Umbraco.Core.IO; using Umbraco.Core.Models.Identity; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; namespace Umbraco.Core.Models { public static class UserExtensions { /// /// Tries to lookup the user's gravatar to see if the endpoint can be reached, if so it returns the valid URL /// /// /// /// /// /// A list of 5 different sized avatar URLs /// internal static string[] GetCurrentUserAvatarUrls(this IUser user, IUserService userService, ICacheProvider staticCache) { if (user.Avatar.IsNullOrWhiteSpace()) { var gravatarHash = user.Email.ToMd5(); var gravatarUrl = "https://www.gravatar.com/avatar/" + gravatarHash + "?d=404"; //try gravatar var gravatarAccess = staticCache.GetCacheItem("UserAvatar" + user.Id, () => { // Test if we can reach this URL, will fail when there's network or firewall errors var request = (HttpWebRequest)WebRequest.Create(gravatarUrl); // Require response within 10 seconds request.Timeout = 10000; try { using ((HttpWebResponse)request.GetResponse()) { } } catch (Exception) { // There was an HTTP or other error, return an null instead return false; } return true; }); if (gravatarAccess) { return new[] { gravatarUrl + "&s=30", gravatarUrl + "&s=60", gravatarUrl + "&s=90", gravatarUrl + "&s=150", gravatarUrl + "&s=300" }; } return null; } //use the custom avatar var avatarUrl = FileSystemProviderManager.Current.MediaFileSystem.GetUrl(user.Avatar); return new[] { avatarUrl + "?width=30&height=30&mode=crop", avatarUrl + "?width=60&height=60&mode=crop", avatarUrl + "?width=90&height=90&mode=crop", avatarUrl + "?width=150&height=150&mode=crop", avatarUrl + "?width=300&height=300&mode=crop" }; } /// /// Returns the culture info associated with this user, based on the language they're assigned to in the back office /// /// /// /// public static CultureInfo GetUserCulture(this IUser user, ILocalizedTextService textService) { if (user == null) throw new ArgumentNullException("user"); if (textService == null) throw new ArgumentNullException("textService"); return GetUserCulture(user.Language, textService); } internal static CultureInfo GetUserCulture(string userLanguage, ILocalizedTextService textService) { 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("en"); } } internal static bool HasContentRootAccess(this IUser user, IEntityService entityService) { return HasPathAccess(Constants.System.Root.ToInvariantString(), user.CalculateContentStartNodeIds(entityService), Constants.System.RecycleBinContent); } internal static bool HasContentBinAccess(this IUser user, IEntityService entityService) { return HasPathAccess(Constants.System.RecycleBinContent.ToInvariantString(), user.CalculateContentStartNodeIds(entityService), Constants.System.RecycleBinContent); } internal static bool HasMediaRootAccess(this IUser user, IEntityService entityService) { return HasPathAccess(Constants.System.Root.ToInvariantString(), user.CalculateMediaStartNodeIds(entityService), Constants.System.RecycleBinMedia); } internal static bool HasMediaBinAccess(this IUser user, IEntityService entityService) { return HasPathAccess(Constants.System.RecycleBinMedia.ToInvariantString(), user.CalculateMediaStartNodeIds(entityService), Constants.System.RecycleBinMedia); } internal static bool HasPathAccess(this IUser user, IContent content, IEntityService entityService) { return HasPathAccess(content.Path, user.CalculateContentStartNodeIds(entityService), Constants.System.RecycleBinContent); } internal static bool HasPathAccess(this IUser user, IMedia media, IEntityService entityService) { return HasPathAccess(media.Path, user.CalculateMediaStartNodeIds(entityService), Constants.System.RecycleBinMedia); } internal static bool HasPathAccess(string path, int[] startNodeIds, int recycleBinId) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value cannot be null or whitespace.", "path"); var formattedPath = "," + path + ","; var formattedRecycleBinId = "," + recycleBinId.ToInvariantString() + ","; //check for root path access //TODO: This logic may change if (startNodeIds.Length == 0 || startNodeIds.Contains(Constants.System.Root)) return true; //only users with root access have access to the recycle bin so if the above check didn't pass than access is denied if (formattedPath.Contains(formattedRecycleBinId)) { return false; } //check for normal paths foreach (var startNodeId in startNodeIds) { var formattedStartNodeId = "," + startNodeId.ToInvariantString() + ","; var hasAccess = formattedPath.Contains(formattedStartNodeId); if (hasAccess) return true; } return false; } /// /// Determines whether this user is an admin. /// /// /// /// true if this user is admin; otherwise, false. /// public static bool IsAdmin(this IUser user) { if (user == null) throw new ArgumentNullException("user"); return user.Groups != null && user.Groups.Any(x => x.Alias == Constants.Security.AdminGroupAlias); } public static int[] CalculateContentStartNodeIds(this IUser user, IEntityService entityService) { const string cacheKey = "AllContentStartNodes"; //try to look them up from cache so we don't recalculate var valuesInUserCache = FromUserCache(user, cacheKey); if (valuesInUserCache != null) return valuesInUserCache; var gsn = user.Groups.Where(x => x.StartContentId.HasValue).Select(x => x.StartContentId.Value).Distinct().ToArray(); var usn = user.StartContentIds; var vals = CombineStartNodes(UmbracoObjectTypes.Document, gsn, usn, entityService); ToUserCache(user, cacheKey, vals); return vals; } public static int[] CalculateMediaStartNodeIds(this IUser user, IEntityService entityService) { const string cacheKey = "AllMediaStartNodes"; //try to look them up from cache so we don't recalculate var valuesInUserCache = FromUserCache(user, cacheKey); if (valuesInUserCache != null) return valuesInUserCache; var gsn = user.Groups.Where(x => x.StartMediaId.HasValue).Select(x => x.StartMediaId.Value).Distinct().ToArray(); var usn = user.StartMediaIds; var vals = CombineStartNodes(UmbracoObjectTypes.Media, gsn, usn, entityService); ToUserCache(user, cacheKey, vals); return vals; } private static int[] FromUserCache(IUser user, string cacheKey) { var entityUser = user as User; if (entityUser != null) { object allContentStartNodes; if (entityUser.AdditionalData.TryGetValue(cacheKey, out allContentStartNodes)) { var asArray = allContentStartNodes as int[]; if (asArray != null) return asArray; } } return null; } private static void ToUserCache(IUser user, string cacheKey, int[] vals) { var entityUser = user as User; if (entityUser != null) { entityUser.AdditionalData[cacheKey] = vals; } } private static bool StartsWithPath(string test, string path) { return test.StartsWith(path) && test.Length > path.Length && test[path.Length] == ','; } //TODO: Unit test this internal static int[] CombineStartNodes(UmbracoObjectTypes objectType, int[] groupSn, int[] userSn, IEntityService entityService) { // assume groupSn and userSn each don't contain duplicates var asn = groupSn.Concat(userSn).Distinct().ToArray(); //TODO: Change this to a more optimal lookup just to retrieve paths var paths = entityService.GetAll(objectType, asn).ToDictionary(x => x.Id, x => x.Path); paths[-1] = "-1"; // entityService does not get that one var lsn = new List(); foreach (var sn in groupSn) { string snp; if (paths.TryGetValue(sn, out snp) == false) continue; // ignore if (lsn.Any(x => StartsWithPath(snp, paths[x]))) continue; // skip if something above this sn lsn.RemoveAll(x => StartsWithPath(paths[x], snp)); // remove anything below this sn lsn.Add(sn); } var usn = new List(); foreach (var sn in userSn) { if (groupSn.Contains(sn)) continue; string snp; if (paths.TryGetValue(sn, out snp) == false) continue; // ignore if (usn.Any(x => StartsWithPath(paths[x], snp))) continue; // skip if something below this sn usn.RemoveAll(x => StartsWithPath(snp, paths[x])); // remove anything above this sn usn.Add(sn); } foreach (var sn in usn) { var snp = paths[sn]; // has to be here now lsn.RemoveAll(x => StartsWithPath(snp, paths[x]) || StartsWithPath(paths[x], snp)); // remove anything above or below this sn lsn.Add(sn); } return lsn.ToArray(); } } }