using System.Collections.Generic; using System.Linq; using xCommons.Extensions; using xIdentityModels.Configurations; using xIdentityModels.Constants; using xIdentityModels.Dtos; using xIdentityModels.Models; namespace xIdentityModels.Extensions { public static class xIdentityModelsExtensions { /// /// Converts a User Entity to Profile Dto ... /// /// /// /// /// /// /// /// public static XUserProfileDto ToXProfileDto( this XUser source, bool isAdmin = false, bool isSameUser = false, IEnumerable roles = null, XFriendshipInfoDto friendship = null, XIdentityProfile profileConfig = null ) { // XUserProfileDto result = null; // if (roles == null) { roles = new List(); } // // Prepare Dto ... if (!source.IsNullOrDefault()) { // result = new XUserProfileDto { Bio = source.Bio, UserId = source.Id, Email = source.Email, Roles = roles.ToList(), Avatar = source.Avatar, IsEnable = source.IsEnable, IsBanned = source.IsBanned, UserName = source.UserName, LastName = source.LastName, FriendshipInfo = friendship, FirstName = source.FirstName, LastLogin = source.LastLogin, CoverImage = source.CoverImage, PhoneNumber = source.PhoneNumber, DateOfBirth = source.DateOfBirth, CreationDate = source.CreationDate, OpenToSearch = source.OpenToSearch, Gender = (XGender)(int)source.Gender, EmailConfirmed = source.EmailConfirmed, PhoneNumberConfirmed = source.PhoneNumberConfirmed, Avatars = source.Avatars.Select(pfi => { // return new XProfileImageDto { Id = pfi.Id, Name = pfi.Name, Path = pfi.Path, Thumb = pfi.Thumb, ThumbPath = pfi.ThumbPath, CreationDate = pfi.CreationDate }; }).ToList(), }; } // // Apply Profiling Configuration ... // bool isPassed = false; // // Handle Friendship ... isPassed = !isSameUser; if (!isPassed) { result.FriendshipInfo = null; } // // Handle Email Info ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsEmail); if (!isPassed) { result.Email = string.Empty; result.EmailConfirmed = false; } // // Handle PhoneNumber Info ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsPhoneNumber); if (!isPassed) { source.PhoneNumber = string.Empty; source.PhoneNumberConfirmed = false; } // // Handle Last Login ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsLastLogin); if (!isPassed) { result.LastLogin = null; } // // Handle Date Of Birth ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsDateOfBirth); if (!isPassed) { result.DateOfBirth = null; } // // Handle Creation Date ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsCreationDate); if (!isPassed) { result.CreationDate = null; } // // Handle Roles ... isPassed = isAdmin || isSameUser || (!profileConfig.IsNullOrDefault() && profileConfig.ContainsRoles); if (!isPassed) { result.Roles = new List(); } // return result; } /// /// Check Specified User Has Access to Provided Roles ... /// /// /// /// public static bool HasAccess( this XUserClaimsInfoDto source, string[] roles ) { // var result = !source.IsNullOrDefault() && source.Roles .Any(r => roles .Any(ar => r.ToNormalString() .Contains(ar.ToNormalString()))); // return result; } /// /// Check Specified User Contains Specified UserSelectByParam or not ... /// /// /// /// public static bool ContainsUserSelectByParam( this XUser source, string userSelectByParam ) { // var result = !source.IsNull() && !userSelectByParam.IsNullOrEmpty() && (source.Id == userSelectByParam || source.Email.ToNormalString() == userSelectByParam.ToNormalString() || source.UserName.ToNormalString() == userSelectByParam.ToNormalString() || source.PhoneNumber.ToNormalString() == userSelectByParam.ToNormalString()); // return result; } } }