Initial ...
This commit is contained in:
@@ -0,0 +1,528 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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;
|
||||
|
||||
namespace xApi.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>
|
||||
[RequireXPowered]
|
||||
[HttpGet("Profile/{ids}/GetNames")]
|
||||
[Authorize(Policy = XPolicies.User)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<string>>> GetNames(
|
||||
[FromRoute] string ids
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
ValidationProvider.NotEmpty(ids);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.GetUserNames(
|
||||
tokens,
|
||||
ids
|
||||
);
|
||||
|
||||
//
|
||||
// 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>
|
||||
[RequireXPowered]
|
||||
[HttpPost("Profile/GetNameIds")]
|
||||
[Authorize(Policy = XPolicies.User)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<XUserNameIdResponse>>> GetNameIds(
|
||||
[FromBody] XUserNameIdRequest model
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
ValidationProvider.NotNull(model);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.GetUserNameIds(
|
||||
tokens,
|
||||
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">determine's which user profile must be retrieved</param>
|
||||
/// <returns>an instance of <see>XUserProfileDto</see></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(Policy = XPolicies.User)]
|
||||
[HttpGet("Profile/{userSelectByParam?}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XUserProfileDto>> GetProfile(
|
||||
[FromRoute] string userSelectByParam = ""
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (userSelectByParam.IsNullOrEmpty())
|
||||
{
|
||||
userSelectByParam = User.Identity.Name;
|
||||
}
|
||||
ValidationProvider.NotEmpty(userSelectByParam);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider
|
||||
.GetUserProfile(
|
||||
tokens,
|
||||
userSelectByParam
|
||||
);
|
||||
|
||||
//
|
||||
// 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 = 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 tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = new XQueryResult<XUserProfileDto>();
|
||||
if (role.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
result = await identityProvider.QueryUsers(
|
||||
tokens,
|
||||
query
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
result = await identityProvider.QueryInRoleUsers(
|
||||
tokens,
|
||||
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 = 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;
|
||||
}
|
||||
ValidationProvider.NotEmpty(userSelectByParam);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.QueryAvatars(
|
||||
tokens,
|
||||
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 <see>XProfileUpdateRequest</see> (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 = XPolicies.EnabledUser)]
|
||||
[HttpPost("Profile/Update/{userSelectByParam?}")]
|
||||
public async Task<ActionResult<XUserProfileDto>> UpdateProfile(
|
||||
[FromBody] XProfileUpdateRequest model, [FromRoute] string userSelectByParam = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (userSelectByParam.IsNullOrEmpty())
|
||||
{
|
||||
userSelectByParam = User.Identity.Name;
|
||||
}
|
||||
ValidationProvider.NotEmpty(userSelectByParam);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.ProfileUpdateAsync(
|
||||
tokens,
|
||||
userSelectByParam,
|
||||
model
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
// Return Result
|
||||
return Ok(result
|
||||
.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update a User Profile based on <see>XProfileUpdateRequest</see> 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]
|
||||
[HttpPost("Profile/{userSelectByParam?}")]
|
||||
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
|
||||
public async Task<ActionResult<XUserProfileDto>> FullUpdateProfile(
|
||||
[FromBody] XProfileUpdateRequest model, [FromRoute] string userSelectByParam = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (userSelectByParam.IsNullOrEmpty())
|
||||
{
|
||||
userSelectByParam = User.Identity.Name;
|
||||
}
|
||||
ValidationProvider.NotEmpty(userSelectByParam);
|
||||
|
||||
//
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.FullProfileUpdateAsync(
|
||||
tokens,
|
||||
userSelectByParam,
|
||||
model
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
// 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 = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XUserProfileDto>> AddAvatar(
|
||||
[FromForm] IFormFile file
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotNull(file);
|
||||
|
||||
//
|
||||
// Get Request ...
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider
|
||||
.AddAvatar(
|
||||
tokens,
|
||||
file
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
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 = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XUserProfileDto>> AddAvatars(
|
||||
[FromForm] IFormFileCollection files
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotZeroChilds(files);
|
||||
|
||||
//
|
||||
// Get Request ...
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider.AddAvatars(
|
||||
tokens,
|
||||
files
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
return Ok(result
|
||||
.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var exResult = GetExceptionActionResult(ex);
|
||||
return exResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XUserProfileDto>> SetAvatar(
|
||||
[FromRoute] int id
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Get Request ...
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider
|
||||
.SetAvatar(
|
||||
tokens,
|
||||
id
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
return Ok(result
|
||||
.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var exception = GetExceptionActionResult(ex);
|
||||
return exception;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XUserProfileDto>> RemoveAvatars(
|
||||
[FromRoute] string ids
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do Action ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotEmpty(ids);
|
||||
|
||||
//
|
||||
// Get Request ...
|
||||
var tokens = await RetrieveTokensAsXTokenResponse();
|
||||
var result = await identityProvider
|
||||
.RemoveAvatars(
|
||||
tokens,
|
||||
ids
|
||||
);
|
||||
|
||||
//
|
||||
// Prepare Push Message ...
|
||||
|
||||
//
|
||||
return Ok(result
|
||||
.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user