From 6721eb3b1879450f22fb73a12a70ac305cd8b139 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 16 Oct 2026 21:33:03 +0330 Subject: [PATCH] last ... --- Controllers/XAccountControllerBase.cs | 1981 +++++++++++++++++++++++++ 1 file changed, 1981 insertions(+) create mode 100644 Controllers/XAccountControllerBase.cs diff --git a/Controllers/XAccountControllerBase.cs b/Controllers/XAccountControllerBase.cs new file mode 100644 index 0000000..e29fc04 --- /dev/null +++ b/Controllers/XAccountControllerBase.cs @@ -0,0 +1,1981 @@ +using System; +using System.Linq; +using xModels.Dtos; +using xCommons.Providers; +using xCommons.Extensions; +using xCommons.Attributes; +using xExceptions.Constants; +using System.Threading.Tasks; +using xIdentityModels.Models; +using xCommons.Configurations; +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using xIdentityService.Interfaces; +using Microsoft.Extensions.Logging; +using xIdentityService.Controllers; +using Microsoft.AspNetCore.Authorization; +using xIdentityModels.Dtos; +using xIdentityModels.Navigations; +using xIdentityModels.Constants; +using Microsoft.AspNetCore.Http; +using static xIdentityHelper.XApiScopeHelper; + +namespace xIdentityHelper.Controllers +{ + public abstract class XAccountControllerBase : XBaseAccountController + { + protected XAccountControllerBase( + ILogger logger, + XAppConfiguration appConfiguration, + IXIdentityProvider IdentityProvider, + XValidationProvider validationProvider + ) : base(logger, appConfiguration, IdentityProvider, validationProvider) + { } + + // + #region Authentication Actions ... + /// + /// Logout User + /// + [RequireXPowered] + [HttpPost("Logout")] + [Authorize(Policy = XPolicies.User)] + public virtual async Task Logout() + { + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + await IdentityProvider + .Logout(tokens); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Validate a Revision Checksum + /// + /// + /// + [HttpPost("Validate")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task> Validate( + [FromBody] XValidateRevisionRequest model + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty(model.Revision) + .ValidateGroupAsync(); + + // + // Get Request ... + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = tokens.ValidateRevisionChecksum( + model.Revision, + IdentityProvider.RevisionSecretKey + ); + + // + // Return Result + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Request AccessToken for Specific XApiScope + /// + /// a member of XApiScope + /// an instance of XTokenResponse + [AllowAnonymous] + [RequireXPowered] + [HttpPost ("RequestScopeAccessToken")] + public virtual async Task> RequestScopeAccessToken ( + [FromHeader] XApiScope scope + ) { + // + // Do Action ... + try { + // + // Validate Args ... + var scopeName = string.Empty; + try { + scopeName = scope.GetStringValue (); + } catch { } + ValidationProvider.NotEmpty (scopeName); + + // + var result = await IdentityProvider + .RequestScopeAccessToken (scopeName); + + // + return Ok (result + .ToDynamicObject ()); + } catch (Exception ex) { + // + var result = GetExceptionActionResult (ex); + return result; + } + } + #endregion + + // + #region Password Actions ... + /// + /// Change a User's Password ... + /// + /// + /// for this action you have to provide: + /// - Lang: client device currently used locale, such as: en-US. + /// - Password: user's current active password. + /// - NewPassword: user's new password to change. + /// - Device: user's client device which is an instance of XDevice. + /// - ReturnUrl: client application Login URL. + /// + /// Sample request: + /// + /// { + /// "lang": "fa-IR", + /// "password": "" + /// "newPassword": "", + /// "returnUrl": "http://localhost/login", + /// "device": { + /// "os":"Mac", + /// "browser":"Chrome", + /// "osVersion":"mac-os-x-15", + /// "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) x-framework-test/0.0.0 Chrome/87.0.4280.141 Electron/11.2.0 Safari/537.36", + /// "deviceType":3, + /// "identifier":"[Mac]-[mac-os-x-15]-[3]-[Chrome]-[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) x-framework-test/0.0.0 Chrome/87.0.4280.141 Electron/11.2.0 Safari/537.36]", + /// "token":"96f400dd9aa5c57a8cec9d6f4775bad3" + /// } + /// } + /// + /// + /// an instance of XActionRequest class which provider requirement for action + /// + [RequireXPowered] + [HttpPost("ChangePassword")] + [Authorize(Policy = XPolicies.User)] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task ChangePassword( + [FromBody] XActionRequest model + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull( + model, + model.Device + ) + .AddNotEmpty( + model.Lang, + model.Password, + model.NewPassword, + model.ReturnUrl + ) + .ValidateGroupAsync(); + + // + // Get User Select By ... + var userSelectByParam = GetUserSelectByParam( + model, + forceNotNull: false + ); + if (userSelectByParam.IsNullOrEmpty()) + { + userSelectByParam = User.Identity.Name; + } + ValidationProvider.NotEmpty(userSelectByParam); + + // + // Get Request ... + var tokens = await RetrieveTokensAsXTokenResponse(); + await IdentityProvider + .ChangePassword(tokens, model); + + // + return Ok(); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + #endregion + + // + #region Admin Actions ... + /// + /// Ban Specific Users + /// + /// an instance of XUserNameIdRequest which represents user identifier list to Ban + /// a list of banned users identifiers + [RequireXPowered] + [HttpPost("Ban")] + [Authorize(Policy = XPolicies.EnabledAdmin)] + public virtual async Task>> Ban( + [FromBody] XUserNameIdRequest model + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotZeroChilds(model.Ids) + .ValidateGroupAsync(); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .Ban( + tokens, + model + ); + + // + return Ok(result) + .ToDynamicObject(); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// UnBann Specific Users + /// + /// an instance of XUserNameIdRequest which represents user identifier list to Ban + /// a list of unbanned users identifiers + [RequireXPowered] + [HttpPost("UnBan")] + [Authorize(Policy = XPolicies.EnabledAdmin)] + public virtual async Task>> UnBan( + [FromBody] XUserNameIdRequest model + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotZeroChilds(model.Ids) + .ValidateGroupAsync(); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .UnBan( + tokens, + model + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Check Specific User is Banned or not + /// + /// specified user's identifier + /// a boolean value which represent user banned or not + [RequireXPowered] + [HttpGet("IsBanned/{userSelectByParam?}")] + [Authorize(Policy = XPolicies.EnabledAdmin)] + [Authorize(Policy = XPolicies.EnabledAgent)] + public virtual async Task> IsBanned( + [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 + .IsBanned( + tokens, + userSelectByParam + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + #endregion + + // + #region Friendship Actions ... + /// + /// Follow a User + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollowing + [RequireXPowered] + [HttpPost("Friendship/{destUser}/Follow")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task> Follow( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .Follow( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Cancel Following Request + /// + /// a user identifier which represent destination user + /// a boolean value + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpPost("Friendship/{destUser}/Cancel")] + public virtual async Task> Cancel( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .Cancel( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Unfollow a Follower + /// + /// a user identifier which represent destination user + /// + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpPost("Friendship/{destUser}/UnFollowFollower")] + public virtual async Task UnFollowFollower( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + await IdentityProvider + .UnFollowFollower( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Unfollow Following + /// + /// a user identifier which represent destination user + /// + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpPost("Friendship/{destUser}/UnFollowFollowing")] + public virtual async Task UnFollowFollowing( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + await IdentityProvider + .UnFollowFollowing( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Block a Follower + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollowing + [RequireXPowered] + [HttpPost("Friendship/{destUser}/Block")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task> Block( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .Block( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + + // + return exResult; + } + } + + /// + /// Unblock a Blocked User + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollowing + [RequireXPowered] + [HttpPost("Friendship/{destUser}/UnBlock")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task> UnBlock( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .UnBlock( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Accept a Following Request + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollower + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpPost("Friendship/{destUser}/AcceptRequest")] + public virtual async Task> AcceptRequest( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .AcceptRequest( + tokens, + destUser + ); + + // + // Prepare Push Message ... + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Reject a Following Request + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollower + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpPost("Friendship/{destUser}/RejectRequest")] + public virtual async Task> RejectRequest( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .RejectRequest( + tokens, + destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + #endregion + + // + #region Friendship Getters ... + /// + /// Get Friendship Info Model between Specific User and Current User + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipInfoDto + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/FriendshipInfo")] + public virtual async Task> FriendshipInfo( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFriendshipInfo( + tokens, + destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Check a User IsFollower of Requested User + /// + /// a user identifier which represent destination user + /// a boolean value + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/IsFollower")] + public virtual async Task> IsFollower( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .IsFollower( + tokens, + destUser + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Follower State of a User + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipState + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/FollowerState")] + public virtual async Task> FollowerState( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowerState( + tokens, + destUser + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Specific Follower + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollower + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/Follower")] + public virtual async Task> Follower( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollower( + tokens, + destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Followers List Of Current User + /// + /// a collection of XFriendshipFollower + [RequireXPowered] + [HttpGet("Friendship/Followers")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> Followers() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowers(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + + // + return exResult; + } + } + + /// + /// Get All Followers List Includes Blocked, Requested and etc + /// of Current User + /// + /// a collection of XFriendshipFollower + [RequireXPowered] + [HttpGet("Friendship/AllFollowers")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> AllFollowers() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetAllFollowers(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get All Followers List Includes Blocked, Requested and etc + /// of Current User based On Query Model ... + /// + /// a Query Result of XFriendDto + [RequireXPowered] + [HttpGet("Friendship/QueryFollowers")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> QueryFollowers( + [FromQuery] XQuery query, + [FromQuery] string destUser + ) + { + // + // Do Action ... + try + { + // + // Normalizing Dest User ... + var userInfo = await GetUserInfo(); + bool isAdmin = userInfo.Roles.Contains(XUserRoles.ADMIN); + if (destUser.IsNullOrEmpty()) + { + destUser = userInfo.UserName; + } + bool isDestUserValid = isAdmin || destUser == userInfo.UserId || destUser == userInfo.Email || destUser == userInfo.PhoneNumber || destUser == userInfo.UserName; + if (!isDestUserValid) + { + XException.NotAllowed.Throw(); + } + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .QueryFollowers( + query: query, + tokens: tokens, + destUser: destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Check a User is in Followings of Current User + /// + /// a user identifier which represent destination user + /// a boolean value + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/IsFollowing")] + public virtual async Task> IsFollowing( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .IsFollowing( + tokens, + destUser + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Following State Relation between Specific User + /// and Current User + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipState + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/FollowingState")] + public virtual async Task> FollowingState( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowingState( + tokens, + destUser + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Specific Following Model + /// + /// a user identifier which represent destination user + /// an instance of XFriendshipFollowing + [RequireXPowered] + [Authorize(Policy = XPolicies.EnabledUser)] + [HttpGet("Friendship/{destUser}/Following")] + public virtual async Task> Following( + [FromRoute] string destUser + ) + { + // + // Do Action ... + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(destUser); + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowing( + tokens, + destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get Followings of Current User + /// + /// a collection of XFriendshipFollowing + [RequireXPowered] + [HttpGet("Friendship/Followings")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> Followings() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowings(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get All Following List Includes Blocked, Requested and etc + /// of Current User + /// + /// a collection of XFriendshipFollowing + [RequireXPowered] + [HttpGet("Friendship/AllFollowings")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> AllFollowings() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetAllFollowings(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Return Following UserName's List of Current User + /// + /// a collection of UserNames + [RequireXPowered] + [HttpGet("Friendship/FollowingsList")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> FollowingsList() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowingList(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Return Followers UserName's List of Current User + /// + /// a collection of UserNames + [RequireXPowered] + [HttpGet("Friendship/FollowersList")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> FollowersList() + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .GetFollowersList(tokens); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + + /// + /// Get All Followings List Includes Blocked, Requested and etc + /// of Current User based On Query Model ... + /// + /// a Query Result of XFriendDto + [RequireXPowered] + [HttpGet("Friendship/QueryFollowings")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> QueryFollowings( + [FromQuery] XQuery query, + [FromQuery] string destUser + ) + { + // + // Do Action ... + try + { + // + // Normalizing Dest User ... + var userInfo = await GetUserInfo(); + bool isAdmin = userInfo.Roles.Contains(XUserRoles.ADMIN); + if (destUser.IsNullOrEmpty()) + { + destUser = userInfo.UserName; + } + bool isDestUserValid = isAdmin || destUser == userInfo.UserId || destUser == userInfo.Email || destUser == userInfo.PhoneNumber || destUser == userInfo.UserName; + if (!isDestUserValid) + { + XException.NotAllowed.Throw(); + } + + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider + .QueryFollowings( + query: query, + tokens: tokens, + destUser: destUser + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exResult = GetExceptionActionResult(ex); + return exResult; + } + } + #endregion + + // + #region Profile Actions ... + // + #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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 virtual 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 + #endregion + + // + #region Registration ... + /// + /// Invite a User to Register on Dashboard + /// + /// + /// for this action you have to provide: + /// - Lang: client device currently used locale, such as: en-US. + /// - Email: which email address is going to invite. + /// - Device: user's client device which is an instance of XDevice. + /// - ReturnUrl: client application Registration URL. + /// notice in this step you had to provide all this informations as FormData + /// + /// Sample request: + /// + /// { + /// "lang": "fa-IR", + /// "email": "", + /// "returnUrl": "", + /// "device": { + /// "os":"Mac", + /// "browser":"Chrome", + /// "osVersion":"mac-os-x-15", + /// "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) x-framework-test/0.0.0 Chrome/87.0.4280.141 Electron/11.2.0 Safari/537.36", + /// "deviceType":3, + /// "identifier":"[Mac]-[mac-os-x-15]-[3]-[Chrome]-[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) x-framework-test/0.0.0 Chrome/87.0.4280.141 Electron/11.2.0 Safari/537.36]", + /// "token":"96f400dd9aa5c57a8cec9d6f4775bad3" + /// } + /// } + /// + /// + /// an instance of XActionRequest class which provider requirement for action + /// an instance of XActionResponse + [RequireXPowered] + [HttpPost ("InviteUser")] + [Authorize (Policy = XPolicies.EnabledAdmin)] + [Authorize (Policy = XPolicies.EnabledAgent)] + public virtual async Task> InviteUser ( + [FromBody] XActionRequest model + ) { + // + // Do Action ... + try { + // + // Validate Args ... + if (!ModelState.IsValid) { + XException.InvalidArgs.Throw (); + } + + // + await ValidationProvider + .GroupValidationBuilder () + .AddNotEmpty ( + model.Lang, + model.Email, + model.ReturnUrl) + .AddEmailAddress (model.Email) + .AddNotNull (model.Device) + .ValidateGroupAsync (); + + // + // Get Request ... + var tokens = await RetrieveTokensAsXTokenResponse (); + var result = await IdentityProvider + .InviteUser ( + tokens, + model + ); + + // + return Ok (result + .ToDynamicObject ()); + } catch (Exception ex) { + // + var result = GetExceptionActionResult (ex); + return result; + } + } + + /// + /// Check User is Confirmed Email or not + /// + /// a boolean value + [RequireXPowered] + [HttpGet ("IsConfirmedEmail")] + [Authorize (Policy = XPolicies.User)] + public virtual async Task> IsConfirmedEmail () { + // + // Do Action ... + try { + // + // Get Result ... + var tokens = await RetrieveTokensAsXTokenResponse (); + var result = await IdentityProvider + .IsConfirmedEmail ( + tokens, + User.Identity.Name + ); + + // + // Return Result + return Ok (result); + } catch (Exception ex) { + // + var result = GetExceptionActionResult (ex); + + // + return result; + } + } + + /// + /// Check User is Confirmed Mobile or not + /// + /// a boolean value + [RequireXPowered] + [HttpGet ("IsConfirmedMobile")] + [Authorize (Policy = XPolicies.User)] + public virtual async Task> IsConfirmedMobile () { + // + // Do Action ... + try { + // + // Get Result ... + var tokens = await RetrieveTokensAsXTokenResponse (); + var result = await IdentityProvider + .IsConfirmedMobile ( + tokens, + User.Identity.Name + ); + + // + // Return Result + return Ok (result); + } catch (Exception ex) { + // + var result = GetExceptionActionResult (ex); + + // + return result; + } + } + #endregion + + // + #region Search ... + /// + /// Query Open To Search Users Profiles ... + /// Query all Users Which their Profiles is Open To Search ... + /// + /// how to filter results based on XQuery structure + /// an instance of XQueryResult of XUserProfileDto + [RequireXPowered] + [HttpGet("OpenToSearch/Query")] + [Authorize(Policy = XPolicies.EnabledUser)] + public virtual async Task>> QueryOpenToSearchProfiles( + [FromQuery] XQuery query + ) + { + // + // Do Action ... + try + { + // + var tokens = await RetrieveTokensAsXTokenResponse(); + var result = await IdentityProvider.QueryOpenToSearchUsers( + tokens, + query + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var exception = GetExceptionActionResult(ex); + return exception; + } + } + #endregion + } +} \ No newline at end of file