DocSpace-buildtools/common/ASC.Core.Common/Data/DbUserService.cs

727 lines
24 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
using AutoMapper.QueryableExtensions;
2022-02-15 11:52:43 +00:00
namespace ASC.Core.Data;
[Scope]
public class ConfigureEFUserService : IConfigureNamedOptions<EFUserService>
2020-10-18 16:03:14 +00:00
{
2022-02-15 11:52:43 +00:00
private readonly DbContextManager<UserDbContext> _dbContextManager;
public ConfigureEFUserService(DbContextManager<UserDbContext> dbContextManager)
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
_dbContextManager = dbContextManager;
}
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
public void Configure(string name, EFUserService options)
{
2022-04-14 13:52:51 +00:00
options.DbId = name;
2022-03-25 16:26:06 +00:00
options.LazyUserDbContext = new Lazy<UserDbContext>(() => _dbContextManager.Get(name));
options.UserDbContextManager = _dbContextManager;
2022-02-15 11:52:43 +00:00
}
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
public void Configure(EFUserService options)
{
2022-03-25 16:26:06 +00:00
options.LazyUserDbContext = new Lazy<UserDbContext>(() => _dbContextManager.Value);
options.UserDbContextManager = _dbContextManager;
2022-02-15 11:52:43 +00:00
}
}
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
[Scope]
public class EFUserService : IUserService
{
2022-03-25 16:26:06 +00:00
internal UserDbContext UserDbContext => LazyUserDbContext.Value;
internal Lazy<UserDbContext> LazyUserDbContext;
internal DbContextManager<UserDbContext> UserDbContextManager;
2022-02-15 11:52:43 +00:00
private readonly PasswordHasher _passwordHasher;
2022-03-25 16:26:06 +00:00
public readonly MachinePseudoKeys MachinePseudoKeys;
2022-04-14 13:52:51 +00:00
internal string DbId { get; set; }
2022-02-15 11:52:43 +00:00
private readonly IMapper _mapper;
public EFUserService(
DbContextManager<UserDbContext> userDbContextManager,
PasswordHasher passwordHasher,
MachinePseudoKeys machinePseudoKeys,
IMapper mapper)
{
2022-03-25 16:26:06 +00:00
UserDbContextManager = userDbContextManager;
2022-02-15 11:52:43 +00:00
_passwordHasher = passwordHasher;
2022-03-25 16:26:06 +00:00
MachinePseudoKeys = machinePseudoKeys;
LazyUserDbContext = new Lazy<UserDbContext>(() => UserDbContextManager.Value);
2022-02-15 11:52:43 +00:00
_mapper = mapper;
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
public Group GetGroup(int tenant, Guid id)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
return GetGroupQuery(tenant)
.Where(r => r.Id == id)
.ProjectTo<Group>(_mapper.ConfigurationProvider)
.FirstOrDefault();
}
2019-11-25 11:49:21 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerable<Group> GetGroups(int tenant)
{
return GetGroupQuery(tenant)
.ProjectTo<Group>(_mapper.ConfigurationProvider)
.ToList();
}
2020-11-24 10:15:11 +00:00
2022-02-15 11:52:43 +00:00
public UserInfo GetUser(int tenant, Guid id)
{
return GetUserQuery(tenant)
.Where(r => r.Id == id)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider)
.FirstOrDefault();
}
2020-02-20 14:57:48 +00:00
2022-02-15 11:52:43 +00:00
public UserInfo GetUser(int tenant, string email)
{
return GetUserQuery(tenant)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider)
.FirstOrDefault(r => r.Email == email && !r.Removed);
}
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
public UserInfo GetUserByUserName(int tenant, string userName)
{
return GetUserQuery(tenant)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider)
.FirstOrDefault(r => r.UserName == userName && !r.Removed);
}
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
public UserInfo GetUserByPasswordHash(int tenant, string login, string passwordHash)
{
2022-03-09 17:15:51 +00:00
ArgumentNullOrEmptyException.ThrowIfNullOrEmpty(login);
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
if (Guid.TryParse(login, out var userId))
2020-12-28 13:22:08 +00:00
{
2022-02-15 11:52:43 +00:00
RegeneratePassword(tenant, userId);
2020-12-28 13:22:08 +00:00
2022-02-15 11:52:43 +00:00
var pwdHash = GetPasswordHash(userId, passwordHash);
var oldHash = Hasher.Base64Hash(passwordHash, HashAlg.SHA256);
2021-08-06 14:30:51 +00:00
2022-02-15 11:52:43 +00:00
var q = GetUserQuery(tenant)
.Where(r => !r.Removed)
.Where(r => r.Id == userId)
.Join(UserDbContext.UserSecurity, r => r.Id, r => r.UserId, (user, security) => new DbUserSecurity
{
User = user,
UserSecurity = security
})
.Where(r => r.UserSecurity.PwdHash == pwdHash || r.UserSecurity.PwdHash == oldHash) //todo: remove old scheme
;
if (tenant != Tenant.DefaultTenant)
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => r.UserSecurity.Tenant == tenant);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return q.Select(r => r.User)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider)
.FirstOrDefault();
}
else
{
var q = GetUserQuery(tenant)
.Where(r => !r.Removed)
.Where(r => r.Email == login);
var users = q.ProjectTo<UserInfo>(_mapper.ConfigurationProvider).ToList();
UserInfo result = null;
foreach (var user in users)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
RegeneratePassword(tenant, user.Id);
2022-02-15 11:52:43 +00:00
var pwdHash = GetPasswordHash(user.Id, passwordHash);
var oldHash = Hasher.Base64Hash(passwordHash, HashAlg.SHA256);
2022-02-15 11:52:43 +00:00
var any = UserDbContext.UserSecurity
.Any(r => r.UserId == user.Id && (r.PwdHash == pwdHash || r.PwdHash == oldHash));//todo: remove old scheme
2022-02-15 11:52:43 +00:00
if (any)
{
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
return user;
}
2022-02-15 11:52:43 +00:00
//need for regenerate all passwords only
//todo: remove with old scheme
result = user;
}
2019-11-25 09:49:12 +00:00
}
2020-12-23 10:12:07 +00:00
2022-02-15 11:52:43 +00:00
return result;
}
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerable<UserInfo> GetUsersAllTenants(IEnumerable<Guid> userIds)
{
var q = UserDbContext.Users
.Where(r => userIds.Contains(r.Id))
.Where(r => !r.Removed);
2022-02-15 11:52:43 +00:00
return q.ProjectTo<UserInfo>(_mapper.ConfigurationProvider).ToList();
}
2022-02-15 11:52:43 +00:00
//todo: remove
private void RegeneratePassword(int tenant, Guid userId)
{
var q = UserDbContext.UserSecurity
.Where(r => r.UserId == userId);
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
q = q.Where(r => r.Tenant == tenant);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
var h2 = q.Select(r => new { r.Tenant, r.PwdHashSha512 })
.Take(1)
.FirstOrDefault();
2022-02-15 11:52:43 +00:00
if (h2 == null || string.IsNullOrEmpty(h2.PwdHashSha512))
{
return;
2021-11-16 17:40:15 +00:00
}
2022-02-15 11:52:43 +00:00
var password = Crypto.GetV(h2.PwdHashSha512, 1, false);
var passwordHash = _passwordHasher.GetClientPassword(password);
SetUserPasswordHash(h2.Tenant, userId, passwordHash);
}
2020-12-28 13:22:08 +00:00
2022-02-15 11:52:43 +00:00
public UserGroupRef GetUserGroupRef(int tenant, Guid groupId, UserGroupRefType refType)
{
IQueryable<UserGroup> q = UserDbContext.UserGroups;
2020-12-28 13:22:08 +00:00
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
q = q.Where(r => r.Tenant == tenant);
}
2022-02-15 11:52:43 +00:00
return q.Where(r => r.GroupId == groupId && r.RefType == refType && !r.Removed)
.ProjectTo<UserGroupRef>(_mapper.ConfigurationProvider).SingleOrDefault();
}
2020-12-28 13:22:08 +00:00
2022-02-15 11:52:43 +00:00
public IDictionary<string, UserGroupRef> GetUserGroupRefs(int tenant)
{
IQueryable<UserGroup> q = UserDbContext.UserGroups;
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => r.Tenant == tenant);
}
2022-02-15 11:52:43 +00:00
return q.ProjectTo<UserGroupRef>(_mapper.ConfigurationProvider)
.AsEnumerable().ToDictionary(r => r.CreateKey(), r => r);
}
2022-02-15 11:52:43 +00:00
public DateTime GetUserPasswordStamp(int tenant, Guid id)
{
var stamp = UserDbContext.UserSecurity
.Where(r => r.Tenant == tenant)
.Where(r => r.UserId == id)
.Select(r => r.LastModified)
.FirstOrDefault();
2022-02-15 11:52:43 +00:00
return stamp ?? DateTime.MinValue;
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public byte[] GetUserPhoto(int tenant, Guid id)
{
var photo = UserDbContext.Photos
.Where(r => r.Tenant == tenant)
.Where(r => r.UserId == id)
.Select(r => r.Photo)
.FirstOrDefault();
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return photo ?? Array.Empty<byte>();
}
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerable<UserInfo> GetUsers(int tenant)
{
return GetUserQuery(tenant)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider)
.ToList();
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public IQueryable<UserInfo> GetUsers(int tenant, 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)
{
2022-04-14 13:52:51 +00:00
var userDbContext = UserDbContextManager.GetNew(DbId);
2022-02-15 11:52:43 +00:00
var totalQuery = GetUserQuery(userDbContext, tenant);
totalQuery = GetUserQueryForFilter(totalQuery, isAdmin, employeeStatus, includeGroups, excludeGroups, activationStatus, text);
total = totalQuery.Count();
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
var q = GetUserQuery(userDbContext, tenant);
q = GetUserQueryForFilter(q, isAdmin, employeeStatus, includeGroups, excludeGroups, activationStatus, text);
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (!string.IsNullOrEmpty(sortBy))
{
q = q.OrderBy(sortBy, sortOrderAsc);
2019-11-15 15:04:05 +00:00
}
2022-02-15 11:52:43 +00:00
if (offset != 0)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
q = q.Skip((int)offset);
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
if (limit != 0)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
q = q.Take((int)limit);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
count = q.Count();
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return q.ProjectTo<UserInfo>(_mapper.ConfigurationProvider);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public IQueryable<UserInfo> GetUsers(int tenant, out int total)
{
2022-04-14 13:52:51 +00:00
var userDbContext = UserDbContextManager.GetNew(DbId);
2022-02-15 11:52:43 +00:00
total = userDbContext.Users.Count(r => r.Tenant == tenant);
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return GetUserQuery(userDbContext, tenant)
.ProjectTo<UserInfo>(_mapper.ConfigurationProvider);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public void RemoveGroup(int tenant, Guid id)
{
RemoveGroup(tenant, id, false);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public void RemoveGroup(int tenant, Guid id, bool immediate)
{
var ids = CollectGroupChilds(tenant, id);
var stringIds = ids.Select(r => r.ToString()).ToList();
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
using var tr = UserDbContext.Database.BeginTransaction();
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
UserDbContext.Acl.RemoveRange(UserDbContext.Acl.Where(r => r.Tenant == tenant && ids.Any(i => i == r.Subject)));
UserDbContext.Subscriptions.RemoveRange(UserDbContext.Subscriptions.Where(r => r.Tenant == tenant && stringIds.Any(i => i == r.Recipient)));
UserDbContext.SubscriptionMethods.RemoveRange(UserDbContext.SubscriptionMethods.Where(r => r.Tenant == tenant && stringIds.Any(i => i == r.Recipient)));
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
var userGroups = UserDbContext.UserGroups.Where(r => r.Tenant == tenant && ids.Any(i => i == r.GroupId));
var groups = UserDbContext.Groups.Where(r => r.Tenant == tenant && ids.Any(i => i == r.Id));
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (immediate)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
UserDbContext.UserGroups.RemoveRange(userGroups);
UserDbContext.Groups.RemoveRange(groups);
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
else
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
foreach (var ug in userGroups)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
ug.Removed = true;
ug.LastModified = DateTime.UtcNow;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
foreach (var g in groups)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
g.Removed = true;
g.LastModified = DateTime.UtcNow;
2019-11-25 09:49:12 +00:00
}
2019-11-15 15:04:05 +00:00
}
2022-02-15 11:52:43 +00:00
UserDbContext.SaveChanges();
tr.Commit();
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public void RemoveUser(int tenant, Guid id)
{
RemoveUser(tenant, id, false);
}
2019-11-29 13:47:05 +00:00
2022-02-15 11:52:43 +00:00
public void RemoveUser(int tenant, Guid id, bool immediate)
{
using var tr = UserDbContext.Database.BeginTransaction();
UserDbContext.Acl.RemoveRange(UserDbContext.Acl.Where(r => r.Tenant == tenant && r.Subject == id));
UserDbContext.Subscriptions.RemoveRange(UserDbContext.Subscriptions.Where(r => r.Tenant == tenant && r.Recipient == id.ToString()));
UserDbContext.SubscriptionMethods.RemoveRange(UserDbContext.SubscriptionMethods.Where(r => r.Tenant == tenant && r.Recipient == id.ToString()));
UserDbContext.Photos.RemoveRange(UserDbContext.Photos.Where(r => r.Tenant == tenant && r.UserId == id));
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
var userGroups = UserDbContext.UserGroups.Where(r => r.Tenant == tenant && r.UserId == id);
var users = UserDbContext.Users.Where(r => r.Tenant == tenant && r.Id == id);
var userSecurity = UserDbContext.UserSecurity.Where(r => r.Tenant == tenant && r.UserId == id);
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (immediate)
{
UserDbContext.UserGroups.RemoveRange(userGroups);
UserDbContext.Users.RemoveRange(users);
UserDbContext.UserSecurity.RemoveRange(userSecurity);
}
else
{
foreach (var ug in userGroups)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
ug.Removed = true;
ug.LastModified = DateTime.UtcNow;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
foreach (var u in users)
{
u.Removed = true;
u.Status = EmployeeStatus.Terminated;
u.TerminatedDate = DateTime.UtcNow;
u.LastModified = DateTime.UtcNow;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
UserDbContext.SaveChanges();
2019-11-29 13:47:05 +00:00
2022-02-15 11:52:43 +00:00
tr.Commit();
}
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
public void RemoveUserGroupRef(int tenant, Guid userId, Guid groupId, UserGroupRefType refType)
{
RemoveUserGroupRef(tenant, userId, groupId, refType, false);
}
public void RemoveUserGroupRef(int tenant, Guid userId, Guid groupId, UserGroupRefType refType, bool immediate)
{
using var tr = UserDbContext.Database.BeginTransaction();
var userGroups = UserDbContext.UserGroups.Where(r => r.Tenant == tenant && r.UserId == userId && r.GroupId == groupId && r.RefType == refType);
if (immediate)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
UserDbContext.UserGroups.RemoveRange(userGroups);
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
else
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
foreach (var u in userGroups)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
u.LastModified = DateTime.UtcNow;
u.Removed = true;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
}
var user = UserDbContext.Users.First(r => r.Tenant == tenant && r.Id == userId);
user.LastModified = DateTime.UtcNow;
UserDbContext.SaveChanges();
2019-11-29 13:47:05 +00:00
2022-02-15 11:52:43 +00:00
tr.Commit();
}
public Group SaveGroup(int tenant, Group group)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(group);
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
if (group.Id == default)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
group.Id = Guid.NewGuid();
}
2022-02-15 11:52:43 +00:00
group.LastModified = DateTime.UtcNow;
group.Tenant = tenant;
2022-02-15 11:52:43 +00:00
var dbGroup = _mapper.Map<Group, DbGroup>(group);
UserDbContext.AddOrUpdate(r => r.Groups, dbGroup);
UserDbContext.SaveChanges();
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return group;
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
public UserInfo SaveUser(int tenant, UserInfo user)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(user);
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
if (string.IsNullOrEmpty(user.UserName))
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
throw new ArgumentOutOfRangeException("Empty username.");
}
2022-02-15 11:52:43 +00:00
if (user.Id == default)
{
user.Id = Guid.NewGuid();
}
2022-02-15 11:52:43 +00:00
if (user.CreateDate == default)
{
user.CreateDate = DateTime.UtcNow;
}
2022-02-15 11:52:43 +00:00
user.LastModified = DateTime.UtcNow;
user.Tenant = tenant;
user.UserName = user.UserName.Trim();
user.Email = user.Email.Trim();
2022-02-15 11:52:43 +00:00
using var tx = UserDbContext.Database.BeginTransaction();
var any = GetUserQuery(tenant)
.Any(r => r.UserName == user.UserName && r.Id != user.Id && !r.Removed);
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (any)
{
throw new ArgumentOutOfRangeException("Duplicate username.");
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
any = GetUserQuery(tenant)
.Any(r => r.Email == user.Email && r.Id != user.Id && !r.Removed);
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (any)
{
throw new ArgumentOutOfRangeException("Duplicate email.");
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
UserDbContext.AddOrUpdate(r => r.Users, _mapper.Map<UserInfo, User>(user));
UserDbContext.SaveChanges();
tx.Commit();
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
return user;
}
2019-11-25 09:49:12 +00:00
2022-03-09 17:15:51 +00:00
public UserGroupRef SaveUserGroupRef(int tenant, UserGroupRef userGroupRef)
2022-02-15 11:52:43 +00:00
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(userGroupRef);
2019-11-15 15:04:05 +00:00
2022-03-09 17:15:51 +00:00
userGroupRef.LastModified = DateTime.UtcNow;
userGroupRef.Tenant = tenant;
2022-02-15 11:52:43 +00:00
using var tr = UserDbContext.Database.BeginTransaction();
2019-11-25 09:49:12 +00:00
2022-03-09 17:15:51 +00:00
var user = GetUserQuery(tenant).FirstOrDefault(a => a.Tenant == tenant && a.Id == userGroupRef.UserId);
2022-02-15 11:52:43 +00:00
if (user != null)
{
2022-03-09 17:15:51 +00:00
user.LastModified = userGroupRef.LastModified;
UserDbContext.AddOrUpdate(r => r.UserGroups, _mapper.Map<UserGroupRef, UserGroup>(userGroupRef));
2022-02-15 11:52:43 +00:00
}
2019-11-29 13:47:05 +00:00
2022-02-15 11:52:43 +00:00
UserDbContext.SaveChanges();
tr.Commit();
2019-11-25 09:49:12 +00:00
2022-03-09 17:15:51 +00:00
return userGroupRef;
2022-02-15 11:52:43 +00:00
}
2019-11-29 13:47:05 +00:00
2022-02-15 11:52:43 +00:00
public void SetUserPasswordHash(int tenant, Guid id, string passwordHash)
{
var h1 = GetPasswordHash(id, passwordHash);
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
var us = new UserSecurity
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
Tenant = tenant,
UserId = id,
PwdHash = h1,
PwdHashSha512 = null,//todo: remove
LastModified = DateTime.UtcNow
};
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
UserDbContext.AddOrUpdate(r => r.UserSecurity, us);
UserDbContext.SaveChanges();
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
public void SetUserPhoto(int tenant, Guid id, byte[] photo)
{
using var tr = UserDbContext.Database.BeginTransaction();
2019-11-15 15:04:05 +00:00
2022-02-15 11:52:43 +00:00
var userPhoto = UserDbContext.Photos.FirstOrDefault(r => r.UserId == id && r.Tenant == tenant);
if (photo != null && photo.Length != 0)
2019-11-15 15:04:05 +00:00
{
2022-02-15 11:52:43 +00:00
if (userPhoto == null)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
userPhoto = new UserPhoto
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
Tenant = tenant,
UserId = id,
Photo = photo
};
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
else
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
userPhoto.Photo = photo;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
UserDbContext.AddOrUpdate(r => r.Photos, userPhoto);
2019-11-15 15:04:05 +00:00
}
2022-02-15 11:52:43 +00:00
else if (userPhoto != null)
2020-12-23 10:12:07 +00:00
{
2022-02-15 11:52:43 +00:00
UserDbContext.Photos.Remove(userPhoto);
2020-12-23 10:12:07 +00:00
}
2022-02-15 11:52:43 +00:00
UserDbContext.SaveChanges();
tr.Commit();
}
2022-02-15 11:52:43 +00:00
private IQueryable<User> GetUserQuery(int tenant)
{
return GetUserQuery(UserDbContext, tenant);
}
2022-02-15 11:52:43 +00:00
private IQueryable<User> GetUserQuery(UserDbContext userDbContext, int tenant)
{
var q = userDbContext.Users.AsQueryable();
var where = false;
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
q = q.Where(r => r.Tenant == tenant);
where = true;
2019-11-25 09:49:12 +00:00
}
2022-02-15 11:52:43 +00:00
if (!where)
2019-11-25 09:49:12 +00:00
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => 1 == 0);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return q;
}
2022-02-15 11:52:43 +00:00
private IQueryable<DbGroup> GetGroupQuery(int tenant)
{
var q = UserDbContext.Groups.Where(r => true);
2022-02-15 11:52:43 +00:00
if (tenant != Tenant.DefaultTenant)
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => r.Tenant == tenant);
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
return q;
}
2022-02-15 11:52:43 +00:00
private IQueryable<User> GetUserQueryForFilter(
IQueryable<User> q,
bool isAdmin,
EmployeeStatus? employeeStatus,
List<List<Guid>> includeGroups,
List<Guid> excludeGroups,
EmployeeActivationStatus? activationStatus,
string text)
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => !r.Removed);
2022-02-15 11:52:43 +00:00
if (includeGroups != null && includeGroups.Count > 0)
{
foreach (var ig in includeGroups)
2019-08-14 12:12:48 +00:00
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => r.Groups.Any(a => !a.Removed && a.Tenant == r.Tenant && a.UserId == r.Id && ig.Any(r => r == a.GroupId)));
2019-08-14 12:12:48 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-08-14 12:12:48 +00:00
2022-02-15 11:52:43 +00:00
if (excludeGroups != null && excludeGroups.Count > 0)
{
foreach (var eg in excludeGroups)
{
2022-02-15 11:52:43 +00:00
q = q.Where(r => !r.Groups.Any(a => !a.Removed && a.Tenant == r.Tenant && a.UserId == r.Id && a.GroupId == eg));
}
2022-02-15 11:52:43 +00:00
}
2022-02-15 11:52:43 +00:00
if (!isAdmin && employeeStatus == null)
{
q = q.Where(r => r.Status != EmployeeStatus.Terminated);
}
2022-02-15 11:52:43 +00:00
if (employeeStatus != null)
{
switch (employeeStatus)
{
2022-02-15 11:52:43 +00:00
case EmployeeStatus.LeaveOfAbsence:
case EmployeeStatus.Terminated:
if (isAdmin)
{
q = q.Where(u => u.Status == EmployeeStatus.Terminated);
}
else
{
q = q.Where(u => false);
}
break;
case EmployeeStatus.All:
2022-03-17 15:01:39 +00:00
if (!isAdmin)
{
q = q.Where(r => r.Status != EmployeeStatus.Terminated);
}
2022-02-15 11:52:43 +00:00
break;
case EmployeeStatus.Default:
case EmployeeStatus.Active:
q = q.Where(u => u.Status == EmployeeStatus.Active);
break;
}
}
2019-11-25 09:49:12 +00:00
2022-02-15 11:52:43 +00:00
if (activationStatus != null)
{
q = q.Where(r => r.ActivationStatus == activationStatus.Value);
2019-11-25 09:49:12 +00:00
}
2020-02-25 08:02:13 +00:00
2022-02-15 11:52:43 +00:00
if (!string.IsNullOrEmpty(text))
2020-02-25 08:02:13 +00:00
{
2022-02-15 11:52:43 +00:00
q = q.Where(
u => u.FirstName.Contains(text) ||
u.LastName.Contains(text) ||
u.Title.Contains(text) ||
u.Location.Contains(text) ||
u.Email.Contains(text));
}
2022-02-15 11:52:43 +00:00
return q;
}
private List<Guid> CollectGroupChilds(int tenant, Guid id)
{
var result = new List<Guid>();
var childs = UserDbContext.Groups
.Where(r => r.Tenant == tenant)
.Where(r => r.ParentId == id)
.Select(r => r.Id);
foreach (var child in childs)
{
result.Add(child);
result.AddRange(CollectGroupChilds(tenant, child));
2020-02-25 08:02:13 +00:00
}
2022-02-15 11:52:43 +00:00
result.Add(id);
return result.Distinct().ToList();
}
public UserInfo GetUser(int tenant, Guid id, Expression<Func<User, UserInfo>> exp)
{
var q = GetUserQuery(tenant).Where(r => r.Id == id);
if (exp != null)
{
2022-02-15 11:52:43 +00:00
return q.Select(exp).FirstOrDefault();
}
else
{
return q.ProjectTo<UserInfo>(_mapper.ConfigurationProvider).FirstOrDefault();
}
}
2022-02-15 11:52:43 +00:00
protected string GetPasswordHash(Guid userId, string password)
{
2022-03-25 16:26:06 +00:00
return Hasher.Base64Hash(password + userId + Encoding.UTF8.GetString(MachinePseudoKeys.GetMachineConstant()), HashAlg.SHA512);
2022-02-15 11:52:43 +00:00
}
}
public class DbUserSecurity
{
public User User { get; set; }
public UserSecurity UserSecurity { get; set; }
}