511 lines
16 KiB
C#
511 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using xIdentityHelper;
|
|
using xCommons.Attributes;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Dtos;
|
|
using xIdentityModels.Models;
|
|
using xModels.Dtos;
|
|
using static IdentityServer4.IdentityServerConstants;
|
|
|
|
namespace xIds.Controllers
|
|
{
|
|
public partial class AccountController
|
|
{
|
|
//
|
|
#region Retrieve Actions ...
|
|
/// <summary>
|
|
/// Retrieve User Names based on UserIds
|
|
/// </summary>
|
|
/// <param name="ids">a comma seperated list of UserIds</param>
|
|
/// <returns>a collection of UserNames</returns>
|
|
[HttpGet("Profile/{ids}/GetNames")]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<IEnumerable<string>>> GetNames(
|
|
[FromRoute] string ids
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
ValidationProvider.NotEmpty(ids);
|
|
|
|
//
|
|
var xIdList = ids.ParseListString<string>();
|
|
var result = await IdentityManager.GetUserNamesAsync(
|
|
xIdList
|
|
);
|
|
|
|
//
|
|
// Return Result
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// get user ids and retrieve corresponding user names
|
|
/// </summary>
|
|
/// <param name="model">an instance of <see>XUserNameIdRequest</see> which represent required UserIds collection</param>
|
|
/// <returns>a collection of <see>XUserNameIdResponse</see> instance</returns>
|
|
[HttpPost("Profile/GetNameIds")]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<IEnumerable<XUserNameIdResponse>>> GetNameIds(
|
|
[FromBody] XUserNameIdRequest model
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
ValidationProvider.NotNull(model);
|
|
|
|
//
|
|
var result = await IdentityManager.GetUserNameIdsAsync(
|
|
model
|
|
);
|
|
|
|
//
|
|
// Return Result
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Profile Object of Specific User
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">specifies which user profile must be retrieved</param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[HttpGet("Profile/{userSelectByParam?}")]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<XUserProfileDto>> GetProfile(
|
|
[FromRoute] string userSelectByParam = ""
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
if (userSelectByParam.IsNullOrEmpty())
|
|
{
|
|
userSelectByParam = User.Identity.Name;
|
|
}
|
|
|
|
//
|
|
var result = await IdentityManager
|
|
.GetUserProfileAsync(
|
|
userSelectByParam,
|
|
User.Identity.Name
|
|
);
|
|
|
|
//
|
|
// Return Result
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Query Users
|
|
/// for specifiy users role for query user 'role: string' and 'forceRole: boolean' in header
|
|
/// </summary>
|
|
/// <param name="query">how to filter results based on <see>XQuery</see> structure</param>
|
|
/// <param name="role">an string which represent user role</param>
|
|
/// <param name="forceRole">if it's true the user must has exact role, otherwise top level users also listed</param>
|
|
/// <returns>an instance of <see>XQueryResult</see> of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[HttpGet("Profile/Query")]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
|
[Authorize(Policy = XPolicies.EnabledAgent)]
|
|
public async Task<ActionResult<XQueryResult<XUserProfileDto>>> QueryProfiles(
|
|
[FromQuery] XQuery query, [FromHeader] string role, [FromHeader] bool forceRole = false
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
var userId = User.Identity.Name;
|
|
|
|
//
|
|
var result = new XQueryResult<XUserProfileDto>();
|
|
if (role.IsNullOrEmpty())
|
|
{
|
|
//
|
|
result = await IdentityManager.QueryUsers(
|
|
userId,
|
|
query
|
|
);
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = await IdentityManager.QueryInRoleUsers(
|
|
userId,
|
|
role,
|
|
query,
|
|
forceRole
|
|
);
|
|
}
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var exception = GetExceptionActionResult(ex);
|
|
return exception;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Query Specified User's Profile Images
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">determine's which user profile must be retrieved</param>
|
|
/// <param name="query">how to filter results based on <see>XQuery</see> structure</param>
|
|
/// <returns>an instance of <see>XQueryResult</see> of <see>XProfileImage</see></returns>
|
|
[RequireXPowered]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
|
[HttpGet("Profile/Avatars/Query/{userSelectByParam?}")]
|
|
public async Task<ActionResult<XQueryResult<XProfileImageDto>>> QueryAvatars(
|
|
[FromQuery] XQuery query, [FromRoute] string userSelectByParam = null
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
if (userSelectByParam.IsNullOrEmpty())
|
|
{
|
|
userSelectByParam = User.Identity.Name;
|
|
}
|
|
|
|
//
|
|
var result = await IdentityManager.QueryAvatars(
|
|
userSelectByParam,
|
|
query
|
|
);
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var exception = GetExceptionActionResult(ex);
|
|
return exception;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Update Actions ...
|
|
/// <summary>
|
|
/// Update User Profile based on XProfileUpdateRequest (FirstName/LastName/DateOfBirth)
|
|
/// </summary>
|
|
/// <param name="model">user update info, an instance of <see>XProfileUpdateRequest</see></param>
|
|
/// <param name="userSelectByParam">determine's which user profile must be retrieved</param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
[HttpPost("Profile/Update/{userSelectByParam?}")]
|
|
public async Task<ActionResult<XUserProfileDto>> UpdateProfile(
|
|
[FromBody] XProfileUpdateRequest model, [FromRoute] string userSelectByParam = null
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
if (userSelectByParam.IsNullOrEmpty())
|
|
{
|
|
userSelectByParam = User.Identity.Name;
|
|
}
|
|
|
|
//
|
|
var result = await IdentityManager.ProfileUpdateAsync(
|
|
userSelectByParam,
|
|
User.Identity.Name,
|
|
model
|
|
);
|
|
|
|
//
|
|
// Return Result
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a User Profile based on XProfileUpdateRequest Full
|
|
/// </summary>
|
|
/// <param name="model">user update info, an instance of <see>XProfileUpdateRequest</see></param>
|
|
/// <param name="userSelectByParam">determine's which user profile must be retrieved</param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[HttpPost("Profile/{userSelectByParam?}")]
|
|
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
|
|
public async Task<ActionResult<XUserProfileDto>> FullUpdateProfile(
|
|
[FromBody] XProfileUpdateRequest model, [FromRoute] string userSelectByParam = null
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
if (userSelectByParam.IsNullOrEmpty())
|
|
{
|
|
userSelectByParam = User.Identity.Name;
|
|
}
|
|
|
|
//
|
|
var result = await IdentityManager.FullProfileUpdateAsync(
|
|
userSelectByParam,
|
|
User.Identity.Name,
|
|
model
|
|
);
|
|
|
|
//
|
|
// Return Result
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Avatar Actions ...
|
|
/// <summary>
|
|
/// Add a New Profile Image
|
|
/// </summary>
|
|
/// <param name="file">an specific File to upload, <see>IFormFile</see></param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[HttpPost("Profile/Avatar")]
|
|
[RequestSizeLimit(966_367_641)]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<XUserProfileDto>> AddAvatar(
|
|
[FromForm] IFormFile file
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
ValidationProvider.NotNull(file);
|
|
|
|
//
|
|
// Get Request ...
|
|
var result = await IdentityManager
|
|
.AddAvatar(
|
|
User.Identity.Name,
|
|
file
|
|
);
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var exResult = GetExceptionActionResult(ex);
|
|
return exResult;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a New Collection of Profile Images
|
|
/// </summary>
|
|
/// <param name="files">a collection of Files to upload, <see>IFormFileCollection</see></param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[HttpPost("Profile/Avatars")]
|
|
[RequestSizeLimit(966_367_641)]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<XUserProfileDto>> AddAvatars(
|
|
[FromForm] IFormFileCollection files
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
ValidationProvider.NotZeroChilds(files);
|
|
|
|
//
|
|
// Get Request ...
|
|
var result = await IdentityManager.AddAvatars(
|
|
User.Identity.Name,
|
|
files
|
|
);
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var exResult = GetExceptionActionResult(ex);
|
|
return exResult;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Profile Images
|
|
/// </summary>
|
|
/// <param name="ids">a comma seperated list of avatarIds to remove</param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[HttpDelete("Profile/Avatars/{ids}")]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<XUserProfileDto>> RemoveAvatars(
|
|
[FromRoute] string ids
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
ValidationProvider.NotEmpty(ids);
|
|
var idList = ids.Split(",");
|
|
var idCollection = new Collection<int>();
|
|
|
|
//
|
|
foreach (var s in idList)
|
|
{
|
|
idCollection.Add(int.Parse(s));
|
|
}
|
|
|
|
//
|
|
// Get Request ...
|
|
var result = await IdentityManager
|
|
.RemoveAvatar(
|
|
User.Identity.Name,
|
|
idCollection.ToArray()
|
|
);
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// set Specified Profile Image as Avatar
|
|
/// </summary>
|
|
/// <param name="id">an integer which reperesent AvatarId to set as current Avatar</param>
|
|
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
|
[RequireXPowered]
|
|
[HttpPost("Profile/Avatars/{id:int}/Set")]
|
|
[Authorize(Policy = XPolicies.User)]
|
|
[Authorize(Policy = LocalApi.PolicyName)]
|
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
public async Task<ActionResult<XUserProfileDto>> SetAvatar(
|
|
[FromRoute] int id
|
|
)
|
|
{
|
|
//
|
|
// Do Action ...
|
|
try
|
|
{
|
|
//
|
|
var result = await IdentityManager
|
|
.SetAvatar(
|
|
User.Identity.Name,
|
|
id
|
|
);
|
|
|
|
//
|
|
return Ok(result
|
|
.ToDynamicObject());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var exception = GetExceptionActionResult(ex);
|
|
return exception;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |