DocSpace-buildtools/common/ASC.Core.Common/Context/Impl/UserManager.cs

674 lines
24 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
2019-08-15 12:04:42 +00:00
using System.Linq;
2020-02-25 08:02:13 +00:00
using System.Linq.Expressions;
2020-01-17 13:58:26 +00:00
2019-10-31 11:28:30 +00:00
using ASC.Collections;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-08-15 12:04:42 +00:00
using ASC.Core.Caching;
2020-02-25 08:02:13 +00:00
using ASC.Core.Common.EF;
2019-05-15 14:56:09 +00:00
using ASC.Core.Tenants;
2019-08-30 12:40:57 +00:00
using ASC.Core.Users;
2020-01-17 13:58:26 +00:00
2019-10-31 11:28:30 +00:00
using Microsoft.AspNetCore.Http;
2019-08-30 12:40:57 +00:00
2019-05-15 14:56:09 +00:00
namespace ASC.Core
2019-10-31 11:28:30 +00:00
{
public class UserManagerConstants
{
public IDictionary<Guid, UserInfo> SystemUsers { get; }
2020-08-12 09:58:08 +00:00
internal Constants Constants { get; }
2019-10-31 11:28:30 +00:00
public UserManagerConstants(Constants constants)
{
2019-09-24 10:32:12 +00:00
SystemUsers = Configuration.Constants.SystemAccounts.ToDictionary(a => a.ID, a => new UserInfo { ID = a.ID, LastName = a.Name });
SystemUsers[Constants.LostUser.ID] = Constants.LostUser;
SystemUsers[Constants.OutsideUser.ID] = Constants.OutsideUser;
2019-10-31 11:28:30 +00:00
SystemUsers[constants.NamingPoster.ID] = constants.NamingPoster;
Constants = constants;
}
}
2019-09-24 10:32:12 +00:00
2019-05-15 14:56:09 +00:00
public class UserManager
{
2019-10-31 11:28:30 +00:00
private IDictionary<Guid, UserInfo> SystemUsers { get => UserManagerConstants.SystemUsers; }
private IHttpContextAccessor Accessor { get; }
private IUserService UserService { get; }
private TenantManager TenantManager { get; }
private PermissionContext PermissionContext { get; }
private UserManagerConstants UserManagerConstants { get; }
private Constants Constants { get; }
private Tenant tenant;
2020-09-29 11:51:34 +00:00
private Tenant Tenant { get { return tenant ??= TenantManager.GetCurrentTenant(); } }
2019-10-31 11:28:30 +00:00
2020-02-27 08:44:55 +00:00
public UserManager()
{
}
2019-10-31 11:28:30 +00:00
public UserManager(
2020-02-20 14:57:48 +00:00
IUserService service,
2019-10-31 11:28:30 +00:00
TenantManager tenantManager,
PermissionContext permissionContext,
2019-09-24 10:32:12 +00:00
UserManagerConstants userManagerConstants)
2019-05-15 14:56:09 +00:00
{
2020-02-20 14:57:48 +00:00
UserService = service;
2019-10-31 11:28:30 +00:00
TenantManager = tenantManager;
PermissionContext = permissionContext;
UserManagerConstants = userManagerConstants;
Constants = UserManagerConstants.Constants;
2020-06-15 15:39:52 +00:00
}
public UserManager(
IUserService service,
TenantManager tenantManager,
PermissionContext permissionContext,
UserManagerConstants userManagerConstants,
IHttpContextAccessor httpContextAccessor)
: this(service, tenantManager, permissionContext, userManagerConstants)
{
Accessor = httpContextAccessor;
2019-05-15 14:56:09 +00:00
}
public void ClearCache()
{
2020-06-15 15:39:52 +00:00
if (UserService is ICachedService service)
2019-05-15 14:56:09 +00:00
{
2020-06-15 15:39:52 +00:00
service.InvalidateCache();
2019-05-15 14:56:09 +00:00
}
}
#region Users
public UserInfo[] GetUsers()
2019-05-15 14:56:09 +00:00
{
return GetUsers(EmployeeStatus.Default);
2019-05-15 14:56:09 +00:00
}
public UserInfo[] GetUsers(EmployeeStatus status)
2019-05-15 14:56:09 +00:00
{
return GetUsers(status, EmployeeType.All);
2019-05-15 14:56:09 +00:00
}
public UserInfo[] GetUsers(EmployeeStatus status, EmployeeType type)
2019-05-15 14:56:09 +00:00
{
var users = GetUsersInternal().Where(u => (u.Status & status) == u.Status);
2019-05-15 14:56:09 +00:00
switch (type)
{
case EmployeeType.User:
users = users.Where(u => !u.IsVisitor(this));
2019-05-15 14:56:09 +00:00
break;
case EmployeeType.Visitor:
users = users.Where(u => u.IsVisitor(this));
2019-05-15 14:56:09 +00:00
break;
}
return users.ToArray();
2019-10-31 11:28:30 +00:00
}
2019-11-25 09:49:12 +00:00
public IQueryable<UserInfo> GetUsers(
bool isAdmin,
EmployeeStatus? employeeStatus,
List<List<Guid>> includeGroups,
List<Guid> excludeGroups,
EmployeeActivationStatus? activationStatus,
string text,
string sortBy,
bool sortOrderAsc,
long limit,
long offset,
out int total,
out int count)
2019-10-31 11:28:30 +00:00
{
2019-11-25 09:49:12 +00:00
return UserService.GetUsers(Tenant.TenantId, isAdmin, employeeStatus, includeGroups, excludeGroups, activationStatus, text, sortBy, sortOrderAsc, limit, offset, out total, out count);
2019-10-31 11:28:30 +00:00
}
2019-05-15 14:56:09 +00:00
public DateTime GetMaxUsersLastModified()
2019-05-15 14:56:09 +00:00
{
return UserService.GetUsers(Tenant.TenantId, default)
2019-05-15 14:56:09 +00:00
.Values
.Select(g => g.LastModified)
.DefaultIfEmpty()
.Max();
}
public string[] GetUserNames(EmployeeStatus status)
2019-05-15 14:56:09 +00:00
{
return GetUsers(status)
2019-05-15 14:56:09 +00:00
.Select(u => u.UserName)
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
}
public UserInfo GetUserByUserName(string username)
2019-05-15 14:56:09 +00:00
{
return GetUsersInternal()
2019-05-15 14:56:09 +00:00
.FirstOrDefault(u => string.Compare(u.UserName, username, StringComparison.CurrentCultureIgnoreCase) == 0) ?? Constants.LostUser;
}
public UserInfo GetUserBySid(string sid)
2019-05-15 14:56:09 +00:00
{
return GetUsersInternal()
2019-05-15 14:56:09 +00:00
.FirstOrDefault(u => u.Sid != null && string.Compare(u.Sid, sid, StringComparison.CurrentCultureIgnoreCase) == 0) ?? Constants.LostUser;
}
public UserInfo GetSsoUserByNameId(string nameId)
2019-05-15 14:56:09 +00:00
{
return GetUsersInternal()
2019-05-15 14:56:09 +00:00
.FirstOrDefault(u => !string.IsNullOrEmpty(u.SsoNameId) && string.Compare(u.SsoNameId, nameId, StringComparison.CurrentCultureIgnoreCase) == 0) ?? Constants.LostUser;
}
public bool IsUserNameExists(string username)
2019-05-15 14:56:09 +00:00
{
return GetUserNames(EmployeeStatus.All)
2019-05-15 14:56:09 +00:00
.Contains(username, StringComparer.CurrentCultureIgnoreCase);
}
public UserInfo GetUsers(Guid id)
{
2019-09-09 12:56:33 +00:00
if (IsSystemUser(id)) return SystemUsers[id];
var u = UserService.GetUser(Tenant.TenantId, id);
2019-05-15 14:56:09 +00:00
return u != null && !u.Removed ? u : Constants.LostUser;
2020-02-25 08:02:13 +00:00
}
public UserInfo GetUser(Guid id, Expression<Func<User, UserInfo>> exp)
{
if (IsSystemUser(id)) return SystemUsers[id];
var u = UserService.GetUser(Tenant.TenantId, id, exp);
return u != null && !u.Removed ? u : Constants.LostUser;
2019-05-15 14:56:09 +00:00
}
public UserInfo GetUsersByPasswordHash(int tenant, string login, string passwordHash)
{
var u = UserService.GetUserByPasswordHash(tenant, login, passwordHash);
2019-05-15 14:56:09 +00:00
return u != null && !u.Removed ? u : Constants.LostUser;
}
public bool UserExists(Guid id)
2019-05-15 14:56:09 +00:00
{
return UserExists(GetUsers(id));
2019-08-30 12:40:57 +00:00
}
public bool UserExists(UserInfo user)
{
return !user.Equals(Constants.LostUser);
2019-05-15 14:56:09 +00:00
}
public bool IsSystemUser(Guid id)
{
2019-09-09 12:56:33 +00:00
return SystemUsers.ContainsKey(id);
2019-05-15 14:56:09 +00:00
}
public UserInfo GetUserByEmail(string email)
2019-05-15 14:56:09 +00:00
{
if (string.IsNullOrEmpty(email)) return Constants.LostUser;
return GetUsersInternal()
2019-05-15 14:56:09 +00:00
.FirstOrDefault(u => string.Compare(u.Email, email, StringComparison.CurrentCultureIgnoreCase) == 0) ?? Constants.LostUser;
}
public UserInfo[] Search(string text, EmployeeStatus status)
2019-05-15 14:56:09 +00:00
{
return Search(text, status, Guid.Empty);
2019-05-15 14:56:09 +00:00
}
public UserInfo[] Search(string text, EmployeeStatus status, Guid groupId)
2019-05-15 14:56:09 +00:00
{
if (text == null || text.Trim() == string.Empty) return new UserInfo[0];
var words = text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (words.Length == 0) return new UserInfo[0];
var users = groupId == Guid.Empty ?
GetUsers(status) :
GetUsersByGroup(groupId).Where(u => (u.Status & status) == status);
2019-05-15 14:56:09 +00:00
var findUsers = new List<UserInfo>();
foreach (var user in users)
{
var properties = new string[]
{
user.LastName ?? string.Empty,
user.FirstName ?? string.Empty,
user.Title ?? string.Empty,
user.Location ?? string.Empty,
user.Email ?? string.Empty,
};
if (IsPropertiesContainsWords(properties, words))
{
findUsers.Add(user);
}
}
return findUsers.ToArray();
}
2020-10-12 13:52:31 +00:00
public UserInfo SaveUserInfo(UserInfo u)
2019-05-15 14:56:09 +00:00
{
2019-09-09 12:56:33 +00:00
if (IsSystemUser(u.ID)) return SystemUsers[u.ID];
if (u.ID == Guid.Empty) PermissionContext.DemandPermissions(Constants.Action_AddRemoveUser);
else PermissionContext.DemandPermissions(new UserSecurityProvider(u.ID), Constants.Action_EditUser);
2019-05-15 14:56:09 +00:00
if (Constants.MaxEveryoneCount <= GetUsersByGroup(Constants.GroupEveryone.ID).Length)
2019-05-15 14:56:09 +00:00
{
throw new TenantQuotaException("Maximum number of users exceeded");
}
if (u.Status == EmployeeStatus.Active)
{
var q = TenantManager.GetTenantQuota(Tenant.TenantId);
if (q.ActiveUsers < GetUsersByGroup(Constants.GroupUser.ID).Length)
2019-05-15 14:56:09 +00:00
{
throw new TenantQuotaException(string.Format("Exceeds the maximum active users ({0})", q.ActiveUsers));
}
2020-01-17 13:58:26 +00:00
}
if (u.Status == EmployeeStatus.Terminated && u.ID == TenantManager.GetCurrentTenant().OwnerId)
{
throw new InvalidOperationException("Can not disable tenant owner.");
}
2019-05-15 14:56:09 +00:00
var newUser = UserService.SaveUser(Tenant.TenantId, u);
2019-05-15 14:56:09 +00:00
return newUser;
}
public void DeleteUser(Guid id)
2019-05-15 14:56:09 +00:00
{
if (IsSystemUser(id)) return;
PermissionContext.DemandPermissions(Constants.Action_AddRemoveUser);
if (id == Tenant.OwnerId)
2019-05-15 14:56:09 +00:00
{
throw new InvalidOperationException("Can not remove tenant owner.");
}
UserService.RemoveUser(Tenant.TenantId, id);
2019-05-15 14:56:09 +00:00
}
public void SaveUserPhoto(Guid id, byte[] photo)
2019-05-15 14:56:09 +00:00
{
if (IsSystemUser(id)) return;
PermissionContext.DemandPermissions(new UserSecurityProvider(id), Constants.Action_EditUser);
2019-05-15 14:56:09 +00:00
UserService.SetUserPhoto(Tenant.TenantId, id, photo);
2019-05-15 14:56:09 +00:00
}
public byte[] GetUserPhoto(Guid id)
2019-05-15 14:56:09 +00:00
{
if (IsSystemUser(id)) return null;
return UserService.GetUserPhoto(Tenant.TenantId, id);
2019-05-15 14:56:09 +00:00
}
public IEnumerable<Guid> GetUserGroupsId(Guid id)
2019-05-15 14:56:09 +00:00
{
return GetUserGroupsGuids(id);
2019-05-15 14:56:09 +00:00
}
public List<GroupInfo> GetUserGroups(Guid id)
2019-05-15 14:56:09 +00:00
{
return GetUserGroups(id, IncludeType.Distinct, Guid.Empty);
2019-05-15 14:56:09 +00:00
}
public List<GroupInfo> GetUserGroups(Guid id, Guid categoryID)
2019-05-15 14:56:09 +00:00
{
return GetUserGroups(id, IncludeType.Distinct, categoryID);
2019-05-15 14:56:09 +00:00
}
public List<GroupInfo> GetUserGroups(Guid userID, IncludeType includeType)
2019-05-15 14:56:09 +00:00
{
return GetUserGroups(userID, includeType, null);
2019-10-31 11:28:30 +00:00
}
internal List<GroupInfo> GetUserGroups(Guid userID, IncludeType includeType, Guid? categoryId)
2019-10-31 11:28:30 +00:00
{
var httpRequestDictionary = new HttpRequestDictionary<List<GroupInfo>>(Accessor?.HttpContext, "GroupInfo");
var fromCache = httpRequestDictionary.Get(userID.ToString());
if (fromCache != null)
{
return fromCache;
}
2019-05-15 14:56:09 +00:00
var result = new List<GroupInfo>();
var distinctUserGroups = new List<GroupInfo>();
var refs = GetRefsInternal();
2019-05-15 14:56:09 +00:00
IEnumerable<UserGroupRef> userRefs = null;
2019-08-15 13:41:38 +00:00
if (refs is UserGroupRefStore store)
2019-05-15 14:56:09 +00:00
{
userRefs = store.GetRefsByUser(userID);
}
2019-08-15 13:28:29 +00:00
var userRefsContainsNotRemoved = userRefs?.Where(r => !r.Removed && r.RefType == UserGroupRefType.Contains).ToList();
2019-05-15 14:56:09 +00:00
foreach (var g in GetGroupsInternal().Where(g => !categoryId.HasValue || g.CategoryID == categoryId))
2019-05-15 14:56:09 +00:00
{
if (((g.CategoryID == Constants.SysGroupCategoryId || userRefs == null) && IsUserInGroupInternal(userID, g.ID, refs)) ||
2019-05-15 14:56:09 +00:00
(userRefsContainsNotRemoved != null && userRefsContainsNotRemoved.Any(r => r.GroupId == g.ID)))
{
distinctUserGroups.Add(g);
}
}
if (IncludeType.Distinct == (includeType & IncludeType.Distinct))
{
result.AddRange(distinctUserGroups);
2019-10-31 11:28:30 +00:00
}
2019-09-09 12:56:33 +00:00
if (categoryId.HasValue)
{
result = result.Where(r => r.CategoryID.Equals(categoryId.Value)).ToList();
2019-10-31 11:28:30 +00:00
}
2019-05-15 14:56:09 +00:00
2019-08-15 13:16:39 +00:00
result.Sort((group1, group2) => string.Compare(group1.Name, group2.Name, StringComparison.Ordinal));
2019-10-31 11:28:30 +00:00
httpRequestDictionary.Add(userID.ToString(), result);
2019-08-30 12:40:57 +00:00
return result;
2019-05-15 14:56:09 +00:00
}
internal IEnumerable<Guid> GetUserGroupsGuids(Guid userID)
2019-10-31 11:28:30 +00:00
{
var httpRequestDictionary = new HttpRequestDictionary<List<Guid>>(Accessor?.HttpContext, "GroupInfoID");
var fromCache = httpRequestDictionary.Get(userID.ToString());
if (fromCache != null)
{
return fromCache;
}
2019-08-30 12:40:57 +00:00
2019-05-15 14:56:09 +00:00
var result = new List<Guid>();
var refs = GetRefsInternal();
2019-05-15 14:56:09 +00:00
2019-08-15 13:41:38 +00:00
if (refs is UserGroupRefStore store)
2019-05-15 14:56:09 +00:00
{
var userRefs = store.GetRefsByUser(userID);
if (userRefs != null)
{
2019-10-31 11:28:30 +00:00
var toAdd = userRefs.Where(r => !r.Removed &&
r.RefType == UserGroupRefType.Contains &&
2019-05-15 14:56:09 +00:00
!Constants.BuildinGroups.Any(g => g.ID.Equals(r.GroupId)))
.Select(r => r.GroupId);
result.AddRange(toAdd);
}
2019-10-31 11:28:30 +00:00
}
2019-08-30 12:40:57 +00:00
2019-10-31 11:28:30 +00:00
httpRequestDictionary.Add(userID.ToString(), result);
2019-05-15 14:56:09 +00:00
return result;
}
public bool IsUserInGroup(Guid userId, Guid groupId)
2019-05-15 14:56:09 +00:00
{
return IsUserInGroupInternal(userId, groupId, GetRefsInternal());
2019-05-15 14:56:09 +00:00
}
public UserInfo[] GetUsersByGroup(Guid groupId, EmployeeStatus employeeStatus = EmployeeStatus.Default)
2019-05-15 14:56:09 +00:00
{
var refs = GetRefsInternal();
return GetUsers(employeeStatus).Where(u => IsUserInGroupInternal(u.ID, groupId, refs)).ToArray();
2019-05-15 14:56:09 +00:00
}
public void AddUserIntoGroup(Guid userId, Guid groupId)
2019-05-15 14:56:09 +00:00
{
if (Constants.LostUser.ID == userId || Constants.LostGroupInfo.ID == groupId)
{
return;
}
PermissionContext.DemandPermissions(Constants.Action_EditGroups);
2019-05-15 14:56:09 +00:00
2020-10-12 19:39:23 +00:00
UserService.SaveUserGroupRef(Tenant.TenantId, new UserGroupRef(userId, groupId, UserGroupRefType.Contains));
2019-05-15 14:56:09 +00:00
2019-08-30 12:40:57 +00:00
ResetGroupCache(userId);
2019-05-15 14:56:09 +00:00
}
public void RemoveUserFromGroup(Guid userId, Guid groupId)
2019-05-15 14:56:09 +00:00
{
if (Constants.LostUser.ID == userId || Constants.LostGroupInfo.ID == groupId) return;
PermissionContext.DemandPermissions(Constants.Action_EditGroups);
2019-05-15 14:56:09 +00:00
UserService.RemoveUserGroupRef(Tenant.TenantId, userId, groupId, UserGroupRefType.Contains);
2019-05-15 14:56:09 +00:00
2019-08-30 12:40:57 +00:00
ResetGroupCache(userId);
2019-10-31 11:28:30 +00:00
}
internal void ResetGroupCache(Guid userID)
{
new HttpRequestDictionary<List<GroupInfo>>(Accessor?.HttpContext, "GroupInfo").Reset(userID.ToString());
new HttpRequestDictionary<List<Guid>>(Accessor?.HttpContext, "GroupInfoID").Reset(userID.ToString());
}
2019-05-15 14:56:09 +00:00
#endregion Users
2019-10-31 11:28:30 +00:00
2019-05-15 14:56:09 +00:00
#region Company
2019-10-31 11:28:30 +00:00
public GroupInfo[] GetDepartments()
2019-05-15 14:56:09 +00:00
{
return GetGroups();
2019-05-15 14:56:09 +00:00
}
public Guid GetDepartmentManager(Guid deparmentID)
2019-05-15 14:56:09 +00:00
{
return GetRefsInternal()
2019-05-15 14:56:09 +00:00
.Values
.Where(r => r.RefType == UserGroupRefType.Manager && r.GroupId == deparmentID && !r.Removed)
.Select(r => r.UserId)
.SingleOrDefault();
}
public void SetDepartmentManager(Guid deparmentID, Guid userID)
2019-05-15 14:56:09 +00:00
{
var managerId = GetDepartmentManager(deparmentID);
2019-05-15 14:56:09 +00:00
if (managerId != Guid.Empty)
{
UserService.RemoveUserGroupRef(
Tenant.TenantId,
2019-05-15 14:56:09 +00:00
managerId, deparmentID, UserGroupRefType.Manager);
}
if (userID != Guid.Empty)
{
2020-10-12 19:39:23 +00:00
UserService.SaveUserGroupRef(
Tenant.TenantId,
2019-05-15 14:56:09 +00:00
new UserGroupRef(userID, deparmentID, UserGroupRefType.Manager));
}
}
public UserInfo GetCompanyCEO()
2019-05-15 14:56:09 +00:00
{
var id = GetDepartmentManager(Guid.Empty);
return id != Guid.Empty ? GetUsers(id) : null;
2019-05-15 14:56:09 +00:00
}
public void SetCompanyCEO(Guid userId)
2019-05-15 14:56:09 +00:00
{
SetDepartmentManager(Guid.Empty, userId);
2019-05-15 14:56:09 +00:00
}
#endregion Company
#region Groups
public GroupInfo[] GetGroups()
2019-05-15 14:56:09 +00:00
{
return GetGroups(Guid.Empty);
2019-05-15 14:56:09 +00:00
}
public GroupInfo[] GetGroups(Guid categoryID)
2019-05-15 14:56:09 +00:00
{
return GetGroupsInternal()
2019-05-15 14:56:09 +00:00
.Where(g => g.CategoryID == categoryID)
.ToArray();
}
public GroupInfo GetGroupInfo(Guid groupID)
2019-05-15 14:56:09 +00:00
{
return GetGroupsInternal()
2019-05-15 14:56:09 +00:00
.SingleOrDefault(g => g.ID == groupID) ?? Constants.LostGroupInfo;
}
public GroupInfo GetGroupInfoBySid(string sid)
2019-05-15 14:56:09 +00:00
{
return GetGroupsInternal()
2019-05-15 14:56:09 +00:00
.SingleOrDefault(g => g.Sid == sid) ?? Constants.LostGroupInfo;
}
public DateTime GetMaxGroupsLastModified()
2019-05-15 14:56:09 +00:00
{
return UserService.GetGroups(Tenant.TenantId, default)
2019-05-15 14:56:09 +00:00
.Values
.Select(g => g.LastModified)
.DefaultIfEmpty()
.Max();
}
public GroupInfo SaveGroupInfo(GroupInfo g)
2019-05-15 14:56:09 +00:00
{
if (Constants.LostGroupInfo.Equals(g)) return Constants.LostGroupInfo;
if (Constants.BuildinGroups.Any(b => b.ID == g.ID)) return Constants.BuildinGroups.Single(b => b.ID == g.ID);
PermissionContext.DemandPermissions(Constants.Action_EditGroups);
2019-05-15 14:56:09 +00:00
var newGroup = UserService.SaveGroup(Tenant.TenantId, ToGroup(g));
2019-05-15 14:56:09 +00:00
return new GroupInfo(newGroup.CategoryId) { ID = newGroup.Id, Name = newGroup.Name, Sid = newGroup.Sid };
}
public void DeleteGroup(Guid id)
2019-05-15 14:56:09 +00:00
{
if (Constants.LostGroupInfo.Equals(id)) return;
if (Constants.BuildinGroups.Any(b => b.ID == id)) return;
PermissionContext.DemandPermissions(Constants.Action_EditGroups);
2019-05-15 14:56:09 +00:00
UserService.RemoveGroup(Tenant.TenantId, id);
2019-05-15 14:56:09 +00:00
}
#endregion Groups
private bool IsPropertiesContainsWords(IEnumerable<string> properties, IEnumerable<string> words)
{
foreach (var w in words)
{
var find = false;
foreach (var p in properties)
{
find = (2 <= w.Length) && (0 <= p.IndexOf(w, StringComparison.CurrentCultureIgnoreCase));
if (find) break;
}
if (!find) return false;
}
return true;
}
private IEnumerable<UserInfo> GetUsersInternal()
2019-05-15 14:56:09 +00:00
{
return UserService.GetUsers(Tenant.TenantId, default)
2019-05-15 14:56:09 +00:00
.Values
.Where(u => !u.Removed);
}
private IEnumerable<GroupInfo> GetGroupsInternal()
2019-05-15 14:56:09 +00:00
{
return UserService.GetGroups(Tenant.TenantId, default)
2019-05-15 14:56:09 +00:00
.Values
.Where(g => !g.Removed)
.Select(g => new GroupInfo(g.CategoryId) { ID = g.Id, Name = g.Name, Sid = g.Sid })
.Concat(Constants.BuildinGroups);
}
private IDictionary<string, UserGroupRef> GetRefsInternal()
2019-05-15 14:56:09 +00:00
{
return UserService.GetUserGroupRefs(Tenant.TenantId, default);
2019-05-15 14:56:09 +00:00
}
private bool IsUserInGroupInternal(Guid userId, Guid groupId, IDictionary<string, UserGroupRef> refs)
2019-05-15 14:56:09 +00:00
{
if (groupId == Constants.GroupEveryone.ID)
{
return true;
}
if (groupId == Constants.GroupAdmin.ID && (Tenant.OwnerId == userId || userId == Configuration.Constants.CoreSystem.ID || userId == Constants.NamingPoster.ID))
2019-05-15 14:56:09 +00:00
{
return true;
}
if (groupId == Constants.GroupVisitor.ID && userId == Constants.OutsideUser.ID)
{
return true;
}
UserGroupRef r;
if (groupId == Constants.GroupUser.ID || groupId == Constants.GroupVisitor.ID)
{
var visitor = refs.TryGetValue(UserGroupRef.CreateKey(Tenant.TenantId, userId, Constants.GroupVisitor.ID, UserGroupRefType.Contains), out r) && !r.Removed;
2019-05-15 14:56:09 +00:00
if (groupId == Constants.GroupVisitor.ID)
{
return visitor;
}
if (groupId == Constants.GroupUser.ID)
{
return !visitor;
}
}
return refs.TryGetValue(UserGroupRef.CreateKey(Tenant.TenantId, userId, groupId, UserGroupRefType.Contains), out r) && !r.Removed;
2019-05-15 14:56:09 +00:00
}
private Group ToGroup(GroupInfo g)
{
if (g == null) return null;
return new Group
{
Id = g.ID,
Name = g.Name,
ParentId = g.Parent != null ? g.Parent.ID : Guid.Empty,
CategoryId = g.CategoryID,
Sid = g.Sid
};
}
2019-10-31 11:28:30 +00:00
}
public static class UserManagerConfigExtension
2019-10-31 11:28:30 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddUserManagerService(this DIHelper services)
2020-07-17 10:52:28 +00:00
{
if (services.TryAddScoped<UserManager>())
{
2020-10-12 19:39:23 +00:00
services.TryAddSingleton<UserManagerConstants>();
2020-07-17 10:52:28 +00:00
return services
.AddUserService()
.AddTenantManagerService()
.AddConstantsService()
.AddPermissionContextService();
}
return services;
2019-10-31 11:28:30 +00:00
}
}
2019-05-15 14:56:09 +00:00
}