Files
xSaherElmIds/Controllers/Account/AccountController+Admin.cs
T
2026-03-22 14:08:25 +03:30

151 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using xIdentityHelper;
using xCommons.Attributes;
using xCommons.Extensions;
using xIdentityModels.Dtos;
using static IdentityServer4.IdentityServerConstants;
namespace xIds.Controllers
{
public partial class AccountController
{
//
#region Admin Actions ...
/// <summary>
/// Ban Specific Users
/// </summary>
/// <param name="model">an instance of <see>XUserNameIdRequest</see> which represents user identifier list to Ban</param>
/// <returns>a list of banned users identifiers</returns>
[RequireXPowered]
[HttpPost("Ban")]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledAdmin)]
public async Task<ActionResult<IEnumerable<string>>> Ban(
[FromBody] XUserNameIdRequest model
)
{
//
// Do Action ...
try
{
//
// Validate Args ...
await ValidationProvider
.GroupValidationBuilder()
.AddNotNull(model)
.AddNotZeroChilds(model.Ids)
.ValidateGroupAsync();
//
var result = await IdentityManager
.Ban(
User.Identity.Name,
model
);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var exResult = GetExceptionActionResult(ex);
return exResult;
}
}
/// <summary>
/// UnBann Specific Users
/// </summary>
/// <param name="model">an instance of <see>XUserNameIdRequest</see> which represents user identifier list to Ban</param>
/// <returns>a list of unbanned users identifiers</returns>
[RequireXPowered]
[HttpPost("UnBan")]
[Authorize(Policy = LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledAdmin)]
public async Task<ActionResult<IEnumerable<string>>> UnBan(
[FromBody] XUserNameIdRequest model
)
{
//
// Do Action ...
try
{
//
// Validate Args ...
await ValidationProvider
.GroupValidationBuilder()
.AddNotNull(model)
.AddNotZeroChilds(model.Ids)
.ValidateGroupAsync();
//
var result = await IdentityManager
.UnBan(
User.Identity.Name,
model
);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var exResult = GetExceptionActionResult(ex);
return exResult;
}
}
/// <summary>
/// Check Specific User is Banned or not
/// </summary>
/// <param name="userSelectByParam">specified user's identifier</param>
/// <returns>a boolean value which represent user banned or not</returns>
[RequireXPowered]
[Authorize(Policy = LocalApi.PolicyName)]
[HttpGet("IsBanned/{userSelectByParam?}")]
[Authorize(Policy = XPolicies.EnabledAdmin)]
[Authorize(Policy = XPolicies.EnabledAgent)]
public async Task<ActionResult<bool>> IsBanned(
[FromRoute] string userSelectByParam = null
)
{
//
// Do Action ...
try
{
//
// Validate Args ...
if (userSelectByParam.IsNullOrEmpty())
{
userSelectByParam = User.Identity.Name;
}
ValidationProvider.NotEmpty(userSelectByParam);
//
var result = await IdentityManager
.IsBanned(
User.Identity.Name,
userSelectByParam
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var exResult = GetExceptionActionResult(ex);
return exResult;
}
}
#endregion
}
}