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
|
|
|
|
|
|
|
|
|
|
namespace ASC.Employee.Core.Controllers;
|
2022-02-28 20:25:25 +00:00
|
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
|
[DefaultRoute]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class GroupController : ControllerBase
|
2019-06-21 09:07:49 +00:00
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
private readonly UserManager _userManager;
|
|
|
|
|
private readonly ApiContext _apiContext;
|
|
|
|
|
private readonly GroupFullDtoHelper _groupFullDtoHelper;
|
|
|
|
|
private readonly MessageService _messageService;
|
|
|
|
|
private readonly MessageTarget _messageTarget;
|
|
|
|
|
private readonly PermissionContext _permissionContext;
|
2022-02-28 20:25:25 +00:00
|
|
|
|
|
2022-03-05 12:57:46 +00:00
|
|
|
|
public GroupController(
|
|
|
|
|
UserManager userManager,
|
|
|
|
|
ApiContext apiContext,
|
|
|
|
|
GroupFullDtoHelper groupFullDtoHelper,
|
|
|
|
|
MessageService messageService,
|
|
|
|
|
MessageTarget messageTarget,
|
|
|
|
|
PermissionContext permissionContext)
|
2019-06-21 09:07:49 +00:00
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_apiContext = apiContext;
|
|
|
|
|
_groupFullDtoHelper = groupFullDtoHelper;
|
|
|
|
|
_messageService = messageService;
|
|
|
|
|
_messageTarget = messageTarget;
|
|
|
|
|
_permissionContext = permissionContext;
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|
2019-06-21 09:07:49 +00:00
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet]
|
2022-02-28 20:25:25 +00:00
|
|
|
|
public IEnumerable<GroupSummaryDto> GetAll()
|
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
var result = _userManager.GetDepartments().Select(r => r);
|
|
|
|
|
if (!string.IsNullOrEmpty(_apiContext.FilterValue))
|
|
|
|
|
{
|
|
|
|
|
result = result.Where(r => r.Name.Contains(_apiContext.FilterValue, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.Select(x => new GroupSummaryDto(x, _userManager));
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|
2021-02-03 10:48:52 +00:00
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("full")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async IAsyncEnumerable<GroupDto> GetAllWithMembers()
|
2022-02-28 20:25:25 +00:00
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
var result = _userManager.GetDepartments().Select(r => r);
|
|
|
|
|
if (!string.IsNullOrEmpty(_apiContext.FilterValue))
|
|
|
|
|
{
|
|
|
|
|
result = result.Where(r => r.Name.Contains(_apiContext.FilterValue, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
foreach (var item in result)
|
|
|
|
|
{
|
|
|
|
|
yield return await _groupFullDtoHelper.Get(item, true);
|
|
|
|
|
}
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|
2022-06-07 18:00:08 +00:00
|
|
|
|
|
|
|
|
|
[HttpGet("search")]
|
|
|
|
|
public IEnumerable<GroupSummaryDto> GetTagsByName(string groupName)
|
|
|
|
|
{
|
|
|
|
|
groupName = (groupName ?? "").Trim();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(groupName))
|
|
|
|
|
{
|
|
|
|
|
return new List<GroupSummaryDto>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _userManager.GetDepartments()
|
|
|
|
|
.Where(x => x.Name.Contains(groupName))
|
|
|
|
|
.Select(x => new GroupSummaryDto(x, _userManager));
|
|
|
|
|
}
|
2019-06-21 09:07:49 +00:00
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("{groupid}")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> GetById(Guid groupid)
|
2022-02-28 20:25:25 +00:00
|
|
|
|
{
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await _groupFullDtoHelper.Get(GetGroupInfo(groupid), true);
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|
2020-11-06 09:03:49 +00:00
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("user/{userid}")]
|
2022-02-28 20:25:25 +00:00
|
|
|
|
public IEnumerable<GroupSummaryDto> GetByUserId(Guid userid)
|
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
return _userManager.GetUserGroups(userid).Select(x => new GroupSummaryDto(x, _userManager));
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|
2020-11-06 09:03:49 +00:00
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPost]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> AddGroup(GroupRequestDto inDto)
|
2022-02-28 20:25:25 +00:00
|
|
|
|
{
|
2022-03-05 12:57:46 +00:00
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
|
|
|
|
|
|
|
|
|
var group = _userManager.SaveGroupInfo(new GroupInfo { Name = inDto.GroupName });
|
|
|
|
|
|
|
|
|
|
TransferUserToDepartment(inDto.GroupManager, @group, true);
|
|
|
|
|
|
|
|
|
|
if (inDto.Members != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var member in inDto.Members)
|
|
|
|
|
{
|
|
|
|
|
TransferUserToDepartment(member, group, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_messageService.Send(MessageAction.GroupCreated, _messageTarget.Create(group.ID), group.Name);
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await _groupFullDtoHelper.Get(group, true);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPut("{groupid}")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> UpdateGroup(Guid groupid, GroupRequestDto inDto)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
|
|
|
|
var group = _userManager.GetGroups().SingleOrDefault(x => x.ID == groupid).NotFoundIfNull("group not found");
|
|
|
|
|
if (groupid == Constants.LostGroupInfo.ID)
|
|
|
|
|
{
|
|
|
|
|
throw new ItemNotFoundException("group not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.Name = inDto.GroupName ?? group.Name;
|
|
|
|
|
_userManager.SaveGroupInfo(group);
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
await RemoveMembersFrom(groupid, new GroupRequestDto { Members = _userManager.GetUsersByGroup(groupid, EmployeeStatus.All).Select(u => u.Id).Where(id => !inDto.Members.Contains(id)) });
|
2022-03-05 12:57:46 +00:00
|
|
|
|
|
|
|
|
|
TransferUserToDepartment(inDto.GroupManager, @group, true);
|
|
|
|
|
|
|
|
|
|
if (inDto.Members != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var member in inDto.Members)
|
|
|
|
|
{
|
|
|
|
|
TransferUserToDepartment(member, group, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_messageService.Send(MessageAction.GroupUpdated, _messageTarget.Create(groupid), group.Name);
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(groupid);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpDelete("{groupid}")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> DeleteGroup(Guid groupid)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
|
2022-05-17 09:29:24 +00:00
|
|
|
|
var @group = GetGroupInfo(groupid);
|
|
|
|
|
|
|
|
|
|
_userManager.DeleteGroup(groupid);
|
|
|
|
|
|
|
|
|
|
_messageService.Send(MessageAction.GroupDeleted, _messageTarget.Create(group.ID), group.Name);
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await _groupFullDtoHelper.Get(group, false);
|
2022-05-17 09:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPut("{groupid}/members/{newgroupid}")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> TransferMembersTo(Guid groupid, Guid newgroupid)
|
2022-05-17 09:29:24 +00:00
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
|
|
|
|
|
|
|
|
|
var oldgroup = GetGroupInfo(groupid);
|
|
|
|
|
|
|
|
|
|
var newgroup = GetGroupInfo(newgroupid);
|
|
|
|
|
|
|
|
|
|
var users = _userManager.GetUsersByGroup(oldgroup.ID);
|
|
|
|
|
foreach (var userInfo in users)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
TransferUserToDepartment(userInfo.Id, newgroup, false);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
2022-05-17 09:29:24 +00:00
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(newgroupid);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPost("{groupid}/members")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> SetMembersTo(Guid groupid, GroupRequestDto inDto)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-08-12 13:26:57 +00:00
|
|
|
|
await RemoveMembersFrom(groupid, new GroupRequestDto { Members = _userManager.GetUsersByGroup(groupid).Select(x => x.Id) });
|
|
|
|
|
await AddMembersTo(groupid, inDto);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(groupid);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPut("{groupid}/members")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> AddMembersTo(Guid groupid, GroupRequestDto inDto)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
|
|
|
|
|
|
|
|
|
var group = GetGroupInfo(groupid);
|
|
|
|
|
|
|
|
|
|
foreach (var userId in inDto.Members)
|
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
TransferUserToDepartment(userId, group, false);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(group.ID);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPut("{groupid}/manager")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> SetManager(Guid groupid, SetManagerRequestDto inDto)
|
2022-05-17 09:29:24 +00:00
|
|
|
|
{
|
|
|
|
|
var group = GetGroupInfo(groupid);
|
|
|
|
|
if (_userManager.UserExists(inDto.UserId))
|
|
|
|
|
{
|
|
|
|
|
_userManager.SetDepartmentManager(group.ID, inDto.UserId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ItemNotFoundException("user not found");
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(groupid);
|
2022-05-17 09:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpDelete("{groupid}/members")]
|
2022-08-12 13:26:57 +00:00
|
|
|
|
public async Task<GroupDto> RemoveMembersFrom(Guid groupid, GroupRequestDto inDto)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(Constants.Action_EditGroups, Constants.Action_AddRemoveUser);
|
|
|
|
|
|
|
|
|
|
var group = GetGroupInfo(groupid);
|
|
|
|
|
|
|
|
|
|
foreach (var userId in inDto.Members)
|
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
RemoveUserFromDepartment(userId, group);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 13:26:57 +00:00
|
|
|
|
return await GetById(group.ID);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 09:29:24 +00:00
|
|
|
|
private GroupInfo GetGroupInfo(Guid groupid)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
var group = _userManager.GetGroups().SingleOrDefault(x => x.ID == groupid).NotFoundIfNull("group not found");
|
|
|
|
|
if (group.ID == Constants.LostGroupInfo.ID)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
throw new ItemNotFoundException("group not found");
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
2022-05-17 09:29:24 +00:00
|
|
|
|
|
|
|
|
|
return @group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TransferUserToDepartment(Guid userId, GroupInfo group, bool setAsManager)
|
|
|
|
|
{
|
|
|
|
|
if (!_userManager.UserExists(userId) && userId != Guid.Empty)
|
2022-03-05 12:57:46 +00:00
|
|
|
|
{
|
2022-05-17 09:29:24 +00:00
|
|
|
|
return;
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 09:29:24 +00:00
|
|
|
|
if (setAsManager)
|
|
|
|
|
{
|
|
|
|
|
_userManager.SetDepartmentManager(@group.ID, userId);
|
|
|
|
|
}
|
|
|
|
|
_userManager.AddUserIntoGroup(userId, @group.ID);
|
2022-03-05 12:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RemoveUserFromDepartment(Guid userId, GroupInfo @group)
|
|
|
|
|
{
|
|
|
|
|
if (!_userManager.UserExists(userId))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUsers(userId);
|
|
|
|
|
_userManager.RemoveUserFromGroup(user.Id, @group.ID);
|
|
|
|
|
_userManager.SaveUserInfo(user);
|
2019-06-21 09:07:49 +00:00
|
|
|
|
}
|
2022-02-28 20:25:25 +00:00
|
|
|
|
}
|