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 ...
///
/// Retrieve User Names based on UserIds
///
/// a comma seperated list of UserIds
/// a collection of UserNames
[RequireXPowered]
[HttpGet("Profile/{ids}/GetNames")]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task>> 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;
}
}
///
/// get user ids and retrieve corresponding user names
///
/// an instance of XUserNameIdRequest which represent required UserIds collection
/// a collection of XUserNameIdResponse instance
[RequireXPowered]
[HttpPost("Profile/GetNameIds")]
[Authorize(Policy = XPolicies.User)]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task>> 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;
}
}
///
/// Get Profile Object of Specific User
///
/// determine's which user profile must be retrieved
/// an instance of XUserProfileDto
[RequireXPowered]
[Authorize(Policy = XPolicies.User)]
[HttpGet("Profile/{userSelectByParam?}")]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// 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 = XPolicies.EnabledAdmin)]
[Authorize(Policy = XPolicies.EnabledAgent)]
public async Task>> QueryProfiles(
[FromQuery] XQuery query, [FromHeader] string role, [FromHeader] bool forceRole = false
)
{
//
// Do Action ...
try
{
//
var tokens = await RetrieveTokensAsXTokenResponse();
var result = new XQueryResult();
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;
}
}
///
/// 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 = 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;
}
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 ...
///
/// 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 = XPolicies.EnabledUser)]
[HttpPost("Profile/Update/{userSelectByParam?}")]
public async Task> 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;
}
}
///
/// 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]
[HttpPost("Profile/{userSelectByParam?}")]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public async Task> 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 ...
///
/// 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 = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// 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 = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// 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 = XPolicies.EnabledUser)]
public async Task> 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;
}
}
///
/// 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 = XPolicies.EnabledUser)]
public async Task> 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
}
}