public DatabaseOperationResult ChangePassword(ChangePasswordViewModel model) { var result = new DatabaseOperationResult(); if (string.IsNullOrWhiteSpace(model.NewPassword)) { result.AddError("Password is not provided"); return result; } var user = _unitOfWork.UserRepository.FindById(_sessionWrapper.UserId); if (user == null) { result.AddError("User not found"); return result; } string oldPassword = _webHelper.EncryptToMd5(model.OldPassword); bool oldPasswordIsValid = oldPassword == user.Password; if (!oldPasswordIsValid) result.AddError("Old password doesn't match."); if (oldPasswordIsValid) { user.Password = _webHelper.EncryptToMd5(model.NewPassword); _unitOfWork.UserRepository.Update(user); _unitOfWork.Save(); } return result; }
public DatabaseOperationResult ChangePassword(ChangePasswordViewModel model) { var result = new DatabaseOperationResult(); if (string.IsNullOrWhiteSpace(model.NewPassword)) { result.AddError("Password is not provided"); return(result); } var user = _unitOfWork.UserRepository.FindById(_sessionWrapper.UserId); if (user == null) { result.AddError("User not found"); return(result); } string oldPassword = _webHelper.EncryptToMd5(model.OldPassword); bool oldPasswordIsValid = oldPassword == user.Password; if (!oldPasswordIsValid) { result.AddError("Old password doesn't match."); } if (oldPasswordIsValid) { user.Password = _webHelper.EncryptToMd5(model.NewPassword); _unitOfWork.UserRepository.Update(user); _unitOfWork.Save(); } return(result); }
public DatabaseOperationResult RegisterUser(RegisterViewModel model) { var result = new DatabaseOperationResult(); model.Email = model.Email.Trim(); if (this.FindUserByEmail(model.Email) != null) { result.AddError("The email has already been taken."); return(result); } if (this.FindUserByUsername(model.Username) != null) { result.AddError("The username has already been taken."); return(result); } var encryptedPassword = _webHelper.EncryptToMd5(model.Password); var currentIpAddress = _webHelper.GetCurrentIpAddress(); var getUserRole = _unitOfWork.UserRoleRepository.Table() .FirstOrDefault(x => x.Name == SystemUserRoleNames.User); if (getUserRole == null) { result.AddError("User acceptance is currently off, please contact to administrator."); return(result); } _unitOfWork.UserRepository.Insert(new User { Email = model.Email.Trim(), IPAddress = currentIpAddress, LastActivity = System.DateTime.Now, Password = encryptedPassword, Username = model.Username, UserRoleId = getUserRole.Id }); _unitOfWork.Save(); return(result); }
public DatabaseOperationResult WallpaperInsert(WallpaperViewModel model) { var result = new DatabaseOperationResult(); if (model.file == null && model.file.ContentLength == 0) { result.AddError("File not found!"); return(result); } if (((model.file.ContentLength / 1024) / 1024) > 2) { result.AddError("File must be less than 2 MB"); return(result); } System.Drawing.Image resolution = System.Drawing.Image.FromStream(model.file.InputStream); if (resolution.Width < 1024 || resolution.Height < 768) { result.AddError("The file must be greater than 1024 pixels wide and greater than to 768 pixels tall at least."); return(result); } var fileExtension = System.IO.Path.GetExtension(model.file.FileName); var allowedExtensions = new List <string> { ".jpg", ".jpeg", ".tiff", ".png" }; if (!string.IsNullOrWhiteSpace(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } else { result.AddError("The file extension not provided. Please try again"); return(result); } if (!allowedExtensions.Contains(fileExtension)) { result.AddError("The file extension not supported, allowed extension is \"*.jpg\", \"*.jpeg\", \"*.tiff\""); return(result); } if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads"))) { System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads")); } var lastWallpaper = _unitOfWork.WallpaperRepository.Table().OrderByDescending(x => x.Id).FirstOrDefault(); string fileName = lastWallpaper == null ? "walltage-1" : "walltage-" + (lastWallpaper.Id + 1); model.ImgPath = string.Format("{0}{1}", fileName, fileExtension); string filePath = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), model.ImgPath); var tagList = PrepareTagList(model.Tags); model.Name = System.IO.Path.GetFileNameWithoutExtension(model.file.FileName); _unitOfWork.WallpaperRepository.Insert(new Domain.Entities.Wallpaper { AddedBy = _sessionWrapper.UserName, AddedDate = DateTime.Now, CategoryId = model.CategoryId, ImgPath = model.ImgPath, ResolutionId = model.ResolutionId, Size = model.file.ContentLength, UploaderId = _sessionWrapper.UserId, TagList = tagList }); _unitOfWork.Save(true); if (!System.IO.File.Exists(filePath)) { model.file.SaveAs(filePath); if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs"))) { System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs")); } System.Drawing.Image image = System.Drawing.Image.FromFile(filePath); image = _webHelper.CreateThumbnail(image, new System.Drawing.Size(256, 250), true); image.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads/Thumbs/") + fileName); image.Dispose(); } return(result); }
public DatabaseOperationResult RegisterUser(RegisterViewModel model) { var result = new DatabaseOperationResult(); model.Email = model.Email.Trim(); if (this.FindUserByEmail(model.Email) != null) { result.AddError("The email has already been taken."); return result; } if (this.FindUserByUsername(model.Username) != null) { result.AddError("The username has already been taken."); return result; } var encryptedPassword = _webHelper.EncryptToMd5(model.Password); var currentIpAddress = _webHelper.GetCurrentIpAddress(); var getUserRole = _unitOfWork.UserRoleRepository.Table() .FirstOrDefault(x => x.Name == SystemUserRoleNames.User); if (getUserRole == null) { result.AddError("User acceptance is currently off, please contact to administrator."); return result; } _unitOfWork.UserRepository.Insert(new User { Email = model.Email.Trim(), IPAddress = currentIpAddress, LastActivity = System.DateTime.Now, Password = encryptedPassword, Username = model.Username, UserRoleId = getUserRole.Id }); _unitOfWork.Save(); return result; }
public DatabaseOperationResult WallpaperInsert(WallpaperViewModel model) { var result = new DatabaseOperationResult(); if (model.file == null && model.file.ContentLength == 0) { result.AddError("File not found!"); return result; } if (((model.file.ContentLength / 1024) / 1024) > 2) { result.AddError("File must be less than 2 MB"); return result; } System.Drawing.Image resolution = System.Drawing.Image.FromStream(model.file.InputStream); if (resolution.Width < 1024 || resolution.Height < 768) { result.AddError("The file must be greater than 1024 pixels wide and greater than to 768 pixels tall at least."); return result; } var fileExtension = System.IO.Path.GetExtension(model.file.FileName); var allowedExtensions = new List<string> { ".jpg", ".jpeg", ".tiff", ".png" }; if (!string.IsNullOrWhiteSpace(fileExtension)) fileExtension = fileExtension.ToLowerInvariant(); else { result.AddError("The file extension not provided. Please try again"); return result; } if (!allowedExtensions.Contains(fileExtension)) { result.AddError("The file extension not supported, allowed extension is \"*.jpg\", \"*.jpeg\", \"*.tiff\""); return result; } if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads"))) System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads")); var lastWallpaper = _unitOfWork.WallpaperRepository.Table().OrderByDescending(x => x.Id).FirstOrDefault(); string fileName = lastWallpaper == null ? "walltage-1" : "walltage-" + (lastWallpaper.Id + 1); model.ImgPath = string.Format("{0}{1}", fileName, fileExtension); string filePath = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), model.ImgPath); var tagList = PrepareTagList(model.Tags); model.Name = System.IO.Path.GetFileNameWithoutExtension(model.file.FileName); _unitOfWork.WallpaperRepository.Insert(new Domain.Entities.Wallpaper { AddedBy = _sessionWrapper.UserName, AddedDate = DateTime.Now, CategoryId = model.CategoryId, ImgPath = model.ImgPath, Name = model.Name, ResolutionId = model.ResolutionId, Size = model.file.ContentLength, UploaderId = _sessionWrapper.UserId, TagList = tagList }); _unitOfWork.Save(true); if (!System.IO.File.Exists(filePath)) { model.file.SaveAs(filePath); if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs"))) System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs")); System.Drawing.Image image = System.Drawing.Image.FromFile(filePath); image = _webHelper.CreateThumbnail(image, new System.Drawing.Size(256, 250), true); image.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads/Thumbs/") + fileName); image.Dispose(); } return result; }