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 ...
///
/// Retrieve User Names based on UserIds
///
/// a comma seperated list of UserIds
/// a collection of UserNames
[HttpGet("Profile/{ids}/GetNames")]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task>> GetNames(
[FromRoute] string ids
)
{
//
// Do Action ...
try
{
//
ValidationProvider.NotEmpty(ids);
//
var xIdList = ids.ParseListString();
var result = await IdentityManager.GetUserNamesAsync(
xIdList
);
//
// Return Result
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// get user ids and retrieve corresponding user names
///
/// an instance of XUserNameIdRequest which represent required UserIds collection
/// a collection of XUserNameIdResponse instance
[HttpPost("Profile/GetNameIds")]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task>> 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;
}
}
///
/// Get Profile Object of Specific User
///
/// specifies which user profile must be retrieved
/// an instance of XUserProfileDto
[RequireXPowered]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[HttpGet("Profile/{userSelectByParam?}")]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// Query Users
/// for specifiy users role for query user 'role: string' and 'forceRole: boolean' in header
///
/// how to filter results based on XQuery structure
/// an string which represent user role
/// if it's true the user must has exact role, otherwise top level users also listed
/// an instance of XQueryResult of XUserProfileDto
[RequireXPowered]
[HttpGet("Profile/Query")]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledAdmin)]
[Authorize(Policy = XPolicies.EnabledAgent)]
public async Task>> QueryProfiles(
[FromQuery] XQuery query, [FromHeader] string role, [FromHeader] bool forceRole = false
)
{
//
// Do Action ...
try
{
//
var userId = User.Identity.Name;
//
var result = new XQueryResult();
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;
}
}
///
/// Query Specified User's Profile Images
///
/// determine's which user profile must be retrieved
/// how to filter results based on XQuery structure
/// an instance of XQueryResult of XProfileImage
[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>> 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 ...
///
/// Update User Profile based on XProfileUpdateRequest (FirstName/LastName/DateOfBirth)
///
/// user update info, an instance of XProfileUpdateRequest
/// determine's which user profile must be retrieved
/// an instance of XUserProfileDto
[RequireXPowered]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
[HttpPost("Profile/Update/{userSelectByParam?}")]
public async Task> 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;
}
}
///
/// Update a User Profile based on XProfileUpdateRequest Full
///
/// user update info, an instance of XProfileUpdateRequest
/// determine's which user profile must be retrieved
/// an instance of XUserProfileDto
[RequireXPowered]
[Authorize(Policy = LocalApi.PolicyName)]
[HttpPost("Profile/{userSelectByParam?}")]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public async Task> 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 ...
///
/// Add a New Profile Image
///
/// an specific File to upload, IFormFile
/// an instance of XUserProfileDto
[RequireXPowered]
[HttpPost("Profile/Avatar")]
[RequestSizeLimit(966_367_641)]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// Add a New Collection of Profile Images
///
/// a collection of Files to upload, IFormFileCollection
/// an instance of XUserProfileDto
[RequireXPowered]
[HttpPost("Profile/Avatars")]
[RequestSizeLimit(966_367_641)]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// Remove Profile Images
///
/// a comma seperated list of avatarIds to remove
/// an instance of XUserProfileDto
[RequireXPowered]
[Authorize(Policy = XPolicies.User)]
[HttpDelete("Profile/Avatars/{ids}")]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> RemoveAvatars(
[FromRoute] string ids
)
{
//
// Do Action ...
try
{
//
ValidationProvider.NotEmpty(ids);
var idList = ids.Split(",");
var idCollection = new Collection();
//
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;
}
}
///
/// set Specified Profile Image as Avatar
///
/// an integer which reperesent AvatarId to set as current Avatar
/// an instance of XUserProfileDto
[RequireXPowered]
[HttpPost("Profile/Avatars/{id:int}/Set")]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> 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
}
}