Skip to content

Commit 3d6c6e1

Browse files
committed
Added fixes and enhancements for backend
1 parent e3964f2 commit 3d6c6e1

File tree

5 files changed

+86
-22
lines changed

5 files changed

+86
-22
lines changed

eFormAPI/eFormAPI/App_Start/AutofacConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Autofac;
44
using Autofac.Integration.WebApi;
55
using eFormAPI.Web.Infrastructure.Data;
6+
using eFormAPI.Web.Infrastructure.Identity;
67

78
namespace eFormAPI.Web
89
{
@@ -21,6 +22,7 @@ public static void ConfigureContainer()
2122
builder.RegisterWebApiFilterProvider(config);
2223
// Set the dependency resolver to be Autofac.
2324
builder.RegisterType<BaseDbContext>().InstancePerRequest();
25+
builder.RegisterType<EformRoleManager>().InstancePerRequest();
2426
Container = builder.Build();
2527
}
2628
}

eFormAPI/eFormAPI/Controllers/AccountController.cs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System.Configuration;
2+
using System.Data.Entity;
23
using System.Linq;
34
using System.Net.Http;
45
using System.Threading.Tasks;
56
using System.Web.Http;
67
using eFormAPI.Common.API;
78
using eFormAPI.Common.Models.Auth;
89
using eFormAPI.Common.Models.User;
10+
using eFormAPI.Web.Infrastructure.Consts;
911
using eFormAPI.Web.Infrastructure.Data;
1012
using eFormAPI.Web.Infrastructure.Data.Entities;
1113
using eFormAPI.Web.Infrastructure.Identity;
1214
using Microsoft.AspNet.Identity;
1315
using Microsoft.AspNet.Identity.Owin;
14-
using Microsoft.Owin.Security;
1516

1617
namespace 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")]

eFormAPI/eFormAPI/Controllers/CasesController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ public OperationDataResult<ReplyElement> Edit(int id)
5151
}
5252
}
5353

54+
[HttpGet]
55+
public OperationResult Delete(int id)
56+
{
57+
try
58+
{
59+
var core = _coreHelper.GetCore();
60+
61+
return core.CaseDeleteResult(id)
62+
? new OperationResult(true, $"Case #{id} deleted successfully")
63+
: new OperationResult(false, "Case could not be removed");
64+
}
65+
catch (Exception)
66+
{
67+
return new OperationResult(false, "Case could not be removed");
68+
}
69+
}
70+
5471
[HttpPost]
5572
public OperationResult Update(ReplyRequest model)
5673
{

eFormAPI/eFormAPI/Controllers/TemplateFilesController.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Http.Headers;
88
using System.Web;
99
using System.Web.Http;
10+
using Castle.Components.DictionaryAdapter.Xml;
1011
using eFormAPI.Common.API;
1112
using eFormAPI.Web.Infrastructure.Helpers;
1213

@@ -73,12 +74,21 @@ public OperationResult RotateImage(string fileName)
7374
{
7475
return new OperationResult(false, "File not found");
7576
}
76-
77-
var img = Image.FromFile(filePath);
78-
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
79-
img.Save(filePath);
80-
img.Dispose();
81-
77+
try
78+
{
79+
var img = Image.FromFile(filePath);
80+
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
81+
img.Save(filePath);
82+
img.Dispose();
83+
}
84+
catch (Exception e)
85+
{
86+
if (e.Message == "A generic error occurred in GDI+.")
87+
{
88+
return new OperationResult(true);
89+
}
90+
return new OperationResult(false, "Error while rotate image.");
91+
}
8292
return new OperationResult(true, "Image rotated successfully.");
8393
}
8494

eFormAPI/eFormAPI/Web.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<add key="header:secondaryTextVisible" value="True" />
3333
<add key="header:imageLink" value="" />
3434
<add key="header:imageLinkVisible" value="True" />
35+
<add key="restore:securityCode" value="code" />
36+
<add key="restore:defaultPassword" value="Qq1234567$" />
3537
</appSettings>
3638
<!--
3739
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

0 commit comments

Comments
 (0)