diff --git a/eFormAPI/eFormAPI/App_Start/AutofacConfig.cs b/eFormAPI/eFormAPI/App_Start/AutofacConfig.cs index 2da54b6799..b4c8655724 100644 --- a/eFormAPI/eFormAPI/App_Start/AutofacConfig.cs +++ b/eFormAPI/eFormAPI/App_Start/AutofacConfig.cs @@ -3,6 +3,7 @@ using Autofac; using Autofac.Integration.WebApi; using eFormAPI.Web.Infrastructure.Data; +using eFormAPI.Web.Infrastructure.Identity; namespace eFormAPI.Web { @@ -21,6 +22,7 @@ public static void ConfigureContainer() builder.RegisterWebApiFilterProvider(config); // Set the dependency resolver to be Autofac. builder.RegisterType().InstancePerRequest(); + builder.RegisterType().InstancePerRequest(); Container = builder.Build(); } } diff --git a/eFormAPI/eFormAPI/Controllers/AccountController.cs b/eFormAPI/eFormAPI/Controllers/AccountController.cs index 74173c1af7..06f2d1acad 100644 --- a/eFormAPI/eFormAPI/Controllers/AccountController.cs +++ b/eFormAPI/eFormAPI/Controllers/AccountController.cs @@ -1,4 +1,5 @@ using System.Configuration; +using System.Data.Entity; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -6,12 +7,12 @@ using eFormAPI.Common.API; using eFormAPI.Common.Models.Auth; using eFormAPI.Common.Models.User; +using eFormAPI.Web.Infrastructure.Consts; using eFormAPI.Web.Infrastructure.Data; using eFormAPI.Web.Infrastructure.Data.Entities; using eFormAPI.Web.Infrastructure.Identity; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; -using Microsoft.Owin.Security; namespace eFormAPI.Web.Controllers { @@ -20,25 +21,19 @@ namespace eFormAPI.Web.Controllers public class AccountController : ApiController { private EformUserManager _userManager; + private readonly EformRoleManager _eformRoleManager; + private readonly BaseDbContext _dbContext; - public AccountController() + public AccountController(BaseDbContext dbContext) { + _eformRoleManager = new EformRoleManager( + new EformRoleStore(new BaseDbContext())); + ; + _dbContext = dbContext; } - public AccountController(EformUserManager userManager, - ISecureDataFormat accessTokenFormat) - { - UserManager = userManager; - AccessTokenFormat = accessTokenFormat; - } - - public EformUserManager UserManager - { - get => _userManager ?? Request.GetOwinContext().GetUserManager(); - private set => _userManager = value; - } - - public ISecureDataFormat AccessTokenFormat { get; private set; } + private EformUserManager UserManager => + _userManager ?? Request.GetOwinContext().GetUserManager(); // GET api/account/user-info [Route("user-info")] @@ -106,6 +101,44 @@ await UserManager.SendEmailAsync(user.Id, "Reset Password", return new OperationResult(false); } + + [HttpGet] + [AllowAnonymous] + [Route("reset-admin-password")] + public async Task ResetAdminPassword(string code) + { + var securityCode = ConfigurationManager.AppSettings["restore:securityCode"]; + if (string.IsNullOrEmpty(securityCode)) + { + return new OperationResult(false, "Please setup security code on server."); + } + var defaultPassword = ConfigurationManager.AppSettings["restore:defaultPassword"]; + if (code != securityCode) + { + return new OperationResult(false, "Invalid security code."); + } + var role = await _eformRoleManager.FindByNameAsync(EformRoles.Admin); + var user = _dbContext.Users.Include(x => x.Roles) + .FirstOrDefault(x => x.Roles.Any(y => y.RoleId == role.Id)); + if (user == null) + { + return new OperationResult(false, "Admin user not found"); + } + var removeResult = await UserManager.RemovePasswordAsync(user.Id); + if (!removeResult.Succeeded) + { + return new OperationResult(false, + "Error while removing old password. \n" + string.Join(" ", removeResult.Errors)); + } + var addPasswordResult = await UserManager.AddPasswordAsync(user.Id, defaultPassword); + if (!addPasswordResult.Succeeded) + { + return new OperationResult(false, + "Error while adding new password. \n" + string.Join(" ", addPasswordResult.Errors)); + } + return new OperationResult(true, $"Your email: {user.Email}. Password has been reset."); + } + // POST: /account/reset-password [HttpPost] [Route("reset-password")] diff --git a/eFormAPI/eFormAPI/Controllers/CasesController.cs b/eFormAPI/eFormAPI/Controllers/CasesController.cs index f0b995a24c..a42adfdbbb 100644 --- a/eFormAPI/eFormAPI/Controllers/CasesController.cs +++ b/eFormAPI/eFormAPI/Controllers/CasesController.cs @@ -51,6 +51,23 @@ public OperationDataResult Edit(int id) } } + [HttpGet] + public OperationResult Delete(int id) + { + try + { + var core = _coreHelper.GetCore(); + + return core.CaseDeleteResult(id) + ? new OperationResult(true, $"Case #{id} deleted successfully") + : new OperationResult(false, "Case could not be removed"); + } + catch (Exception) + { + return new OperationResult(false, "Case could not be removed"); + } + } + [HttpPost] public OperationResult Update(ReplyRequest model) { diff --git a/eFormAPI/eFormAPI/Controllers/TemplateFilesController.cs b/eFormAPI/eFormAPI/Controllers/TemplateFilesController.cs index 89d4541853..4366481b34 100644 --- a/eFormAPI/eFormAPI/Controllers/TemplateFilesController.cs +++ b/eFormAPI/eFormAPI/Controllers/TemplateFilesController.cs @@ -7,6 +7,7 @@ using System.Net.Http.Headers; using System.Web; using System.Web.Http; +using Castle.Components.DictionaryAdapter.Xml; using eFormAPI.Common.API; using eFormAPI.Web.Infrastructure.Helpers; @@ -73,12 +74,21 @@ public OperationResult RotateImage(string fileName) { return new OperationResult(false, "File not found"); } - - var img = Image.FromFile(filePath); - img.RotateFlip(RotateFlipType.Rotate90FlipNone); - img.Save(filePath); - img.Dispose(); - + try + { + var img = Image.FromFile(filePath); + img.RotateFlip(RotateFlipType.Rotate90FlipNone); + img.Save(filePath); + img.Dispose(); + } + catch (Exception e) + { + if (e.Message == "A generic error occurred in GDI+.") + { + return new OperationResult(true); + } + return new OperationResult(false, "Error while rotate image."); + } return new OperationResult(true, "Image rotated successfully."); } diff --git a/eFormAPI/eFormAPI/Web.config b/eFormAPI/eFormAPI/Web.config index 1780c4e0a5..59270217af 100644 --- a/eFormAPI/eFormAPI/Web.config +++ b/eFormAPI/eFormAPI/Web.config @@ -32,6 +32,8 @@ + +