DocSpace-buildtools/products/ASC.People/Server/Api/ContactsController.cs

129 lines
3.8 KiB
C#
Raw Normal View History

2022-03-01 11:52:52 +00:00
namespace ASC.People.Api;
2022-02-28 19:23:39 +00:00
2022-03-05 12:57:46 +00:00
public class ContactsController : PeopleControllerBase
2022-02-28 19:23:39 +00:00
{
2022-03-05 12:57:46 +00:00
private readonly EmployeeFullDtoHelper _employeeFullDtoHelper;
2022-02-28 20:25:25 +00:00
2022-03-05 12:57:46 +00:00
public ContactsController(
UserManager userManager,
PermissionContext permissionContext,
ApiContext apiContext,
UserPhotoManager userPhotoManager,
IHttpClientFactory httpClientFactory,
EmployeeFullDtoHelper employeeFullDtoHelper)
: base(userManager, permissionContext, apiContext, userPhotoManager, httpClientFactory)
2022-02-28 19:23:39 +00:00
{
2022-03-05 12:57:46 +00:00
_employeeFullDtoHelper = employeeFullDtoHelper;
2022-02-28 19:23:39 +00:00
}
[Delete("{userid}/contacts")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto DeleteMemberContactsFromBody(string userid, [FromBody] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return DeleteMemberContacts(userid, inDto);
2022-02-28 19:23:39 +00:00
}
[Delete("{userid}/contacts")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto DeleteMemberContactsFromForm(string userid, [FromForm] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return DeleteMemberContacts(userid, inDto);
2022-02-28 19:23:39 +00:00
}
[Create("{userid}/contacts")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto SetMemberContactsFromBody(string userid, [FromBody] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return SetMemberContacts(userid, inDto);
2022-02-28 19:23:39 +00:00
}
[Create("{userid}/contacts")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto SetMemberContactsFromForm(string userid, [FromForm] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return SetMemberContacts(userid, inDto);
2022-02-28 19:23:39 +00:00
}
[Update("{userid}/contacts")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto UpdateMemberContactsFromBody(string userid, [FromBody] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return UpdateMemberContacts(userid, inDto);
2022-02-28 19:23:39 +00:00
}
[Update("{userid}/contacts")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-05 13:20:51 +00:00
public EmployeeFullDto UpdateMemberContactsFromForm(string userid, [FromForm] UpdateMemberRequestDto inDto)
2022-02-28 19:23:39 +00:00
{
2022-03-05 13:20:51 +00:00
return UpdateMemberContacts(userid, inDto);
2022-03-05 12:57:46 +00:00
}
2022-03-05 13:20:51 +00:00
private EmployeeFullDto DeleteMemberContacts(string userid, UpdateMemberRequestDto inDto)
2022-03-05 12:57:46 +00:00
{
var user = GetUserInfo(userid);
if (_userManager.IsSystemUser(user.Id))
{
throw new SecurityException();
}
2022-03-05 13:20:51 +00:00
DeleteContacts(inDto.Contacts, user);
2022-03-05 12:57:46 +00:00
_userManager.SaveUserInfo(user);
return _employeeFullDtoHelper.GetFull(user);
}
2022-03-05 13:20:51 +00:00
private EmployeeFullDto SetMemberContacts(string userid, UpdateMemberRequestDto inDto)
2022-03-05 12:57:46 +00:00
{
var user = GetUserInfo(userid);
if (_userManager.IsSystemUser(user.Id))
{
throw new SecurityException();
}
user.ContactsList.Clear();
2022-03-05 13:20:51 +00:00
UpdateContacts(inDto.Contacts, user);
2022-03-05 12:57:46 +00:00
_userManager.SaveUserInfo(user);
return _employeeFullDtoHelper.GetFull(user);
}
2022-03-05 13:20:51 +00:00
private EmployeeFullDto UpdateMemberContacts(string userid, UpdateMemberRequestDto inDto)
2022-03-05 12:57:46 +00:00
{
var user = GetUserInfo(userid);
if (_userManager.IsSystemUser(user.Id))
{
throw new SecurityException();
}
2022-03-05 13:20:51 +00:00
UpdateContacts(inDto.Contacts, user);
2022-03-05 12:57:46 +00:00
_userManager.SaveUserInfo(user);
return _employeeFullDtoHelper.GetFull(user);
}
private void DeleteContacts(IEnumerable<Contact> contacts, UserInfo user)
{
_permissionContext.DemandPermissions(new UserSecurityProvider(user.Id), Constants.Action_EditUser);
if (contacts == null)
{
return;
}
if (user.ContactsList == null)
{
user.ContactsList = new List<string>();
}
foreach (var contact in contacts)
{
var index = user.ContactsList.IndexOf(contact.Type);
if (index != -1)
{
//Remove existing
user.ContactsList.RemoveRange(index, 2);
}
}
2022-02-28 19:23:39 +00:00
}
}