11using System . Configuration ;
2+ using System . Data . Entity ;
23using System . Linq ;
34using System . Net . Http ;
45using System . Threading . Tasks ;
56using System . Web . Http ;
67using eFormAPI . Common . API ;
78using eFormAPI . Common . Models . Auth ;
89using eFormAPI . Common . Models . User ;
10+ using eFormAPI . Web . Infrastructure . Consts ;
911using eFormAPI . Web . Infrastructure . Data ;
1012using eFormAPI . Web . Infrastructure . Data . Entities ;
1113using eFormAPI . Web . Infrastructure . Identity ;
1214using Microsoft . AspNet . Identity ;
1315using Microsoft . AspNet . Identity . Owin ;
14- using Microsoft . Owin . Security ;
1516
1617namespace eFormAPI . Web . Controllers
1718{
@@ -20,25 +21,19 @@ namespace eFormAPI.Web.Controllers
2021 public class AccountController : ApiController
2122 {
2223 private EformUserManager _userManager ;
24+ private readonly EformRoleManager _eformRoleManager ;
25+ private readonly BaseDbContext _dbContext ;
2326
24- public AccountController ( )
27+ public AccountController ( BaseDbContext dbContext )
2528 {
29+ _eformRoleManager = new EformRoleManager (
30+ new EformRoleStore ( new BaseDbContext ( ) ) ) ;
31+ ;
32+ _dbContext = dbContext ;
2633 }
2734
28- public AccountController ( EformUserManager userManager ,
29- ISecureDataFormat < AuthenticationTicket > accessTokenFormat )
30- {
31- UserManager = userManager ;
32- AccessTokenFormat = accessTokenFormat ;
33- }
34-
35- public EformUserManager UserManager
36- {
37- get => _userManager ?? Request . GetOwinContext ( ) . GetUserManager < EformUserManager > ( ) ;
38- private set => _userManager = value ;
39- }
40-
41- public ISecureDataFormat < AuthenticationTicket > AccessTokenFormat { get ; private set ; }
35+ private EformUserManager UserManager =>
36+ _userManager ?? Request . GetOwinContext ( ) . GetUserManager < EformUserManager > ( ) ;
4237
4338 // GET api/account/user-info
4439 [ Route ( "user-info" ) ]
@@ -106,6 +101,44 @@ await UserManager.SendEmailAsync(user.Id, "Reset Password",
106101 return new OperationResult ( false ) ;
107102 }
108103
104+
105+ [ HttpGet ]
106+ [ AllowAnonymous ]
107+ [ Route ( "reset-admin-password" ) ]
108+ public async Task < OperationResult > ResetAdminPassword ( string code )
109+ {
110+ var securityCode = ConfigurationManager . AppSettings [ "restore:securityCode" ] ;
111+ if ( string . IsNullOrEmpty ( securityCode ) )
112+ {
113+ return new OperationResult ( false , "Please setup security code on server." ) ;
114+ }
115+ var defaultPassword = ConfigurationManager . AppSettings [ "restore:defaultPassword" ] ;
116+ if ( code != securityCode )
117+ {
118+ return new OperationResult ( false , "Invalid security code." ) ;
119+ }
120+ var role = await _eformRoleManager . FindByNameAsync ( EformRoles . Admin ) ;
121+ var user = _dbContext . Users . Include ( x => x . Roles )
122+ . FirstOrDefault ( x => x . Roles . Any ( y => y . RoleId == role . Id ) ) ;
123+ if ( user == null )
124+ {
125+ return new OperationResult ( false , "Admin user not found" ) ;
126+ }
127+ var removeResult = await UserManager . RemovePasswordAsync ( user . Id ) ;
128+ if ( ! removeResult . Succeeded )
129+ {
130+ return new OperationResult ( false ,
131+ "Error while removing old password. \n " + string . Join ( " " , removeResult . Errors ) ) ;
132+ }
133+ var addPasswordResult = await UserManager . AddPasswordAsync ( user . Id , defaultPassword ) ;
134+ if ( ! addPasswordResult . Succeeded )
135+ {
136+ return new OperationResult ( false ,
137+ "Error while adding new password. \n " + string . Join ( " " , addPasswordResult . Errors ) ) ;
138+ }
139+ return new OperationResult ( true , $ "Your email: { user . Email } . Password has been reset.") ;
140+ }
141+
109142 // POST: /account/reset-password
110143 [ HttpPost ]
111144 [ Route ( "reset-password" ) ]
0 commit comments