Skip to content

Commit

Permalink
Users: Code styling and removing outdated comment from AccountController
Browse files Browse the repository at this point in the history
  • Loading branch information
LombiqTechnologies authored and BenedekFarkas committed Mar 9, 2019
1 parent a53d30d commit ba722cf
Showing 1 changed file with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
using Orchard.Localization;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Security;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
using Orchard.Security;
using Orchard.Themes;
using Orchard.Users.Services;
using Orchard.ContentManagement;
using Orchard.Users.Models;
using Orchard.UI.Notify;
using Orchard.Users.Events;
using Orchard.Users.Models;
using Orchard.Users.Services;
using Orchard.Utility.Extensions;

namespace Orchard.Users.Controllers {
Expand All @@ -26,9 +26,9 @@ public class AccountController : Controller {
private readonly IUserEventHandler _userEventHandler;

public AccountController(
IAuthenticationService authenticationService,
IAuthenticationService authenticationService,
IMembershipService membershipService,
IUserService userService,
IUserService userService,
IOrchardServices orchardServices,
IUserEventHandler userEventHandler) {
_authenticationService = authenticationService;
Expand All @@ -51,11 +51,9 @@ public ActionResult AccessDenied() {
if (currentUser == null) {
Logger.Information("Access denied to anonymous request on {0}", returnUrl);
var shape = _orchardServices.New.LogOn().Title(T("Access Denied").Text);
return new ShapeResult(this, shape);
return new ShapeResult(this, shape);
}

//TODO: (erikpo) Add a setting for whether or not to log access denieds since these can fill up a database pretty fast from bots on a high traffic site
//Suggestion: Could instead use the new AccessDenined IUserEventHandler method and let modules decide if they want to log this event?
Logger.Information("Access denied to user #{0} '{1}' on {2}", currentUser.Id, currentUser.UserName, returnUrl);

_userEventHandler.AccessDenied(currentUser);
Expand All @@ -69,7 +67,7 @@ public ActionResult LogOn(string returnUrl) {
return this.RedirectLocal(returnUrl);

var shape = _orchardServices.New.LogOn().Title(T("Log On").Text);
return new ShapeResult(this, shape);
return new ShapeResult(this, shape);
}

[HttpPost]
Expand All @@ -83,7 +81,7 @@ public ActionResult LogOn(string userNameOrEmail, string password, string return
var user = ValidateLogOn(userNameOrEmail, password);
if (!ModelState.IsValid) {
var shape = _orchardServices.New.LogOn().Title(T("Log On").Text);
return new ShapeResult(this, shape);
return new ShapeResult(this, shape);
}

_authenticationService.SignIn(user, rememberMe);
Expand Down Expand Up @@ -113,14 +111,14 @@ int MinPasswordLength {
public ActionResult Register() {
// ensure users can register
var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.UsersCanRegister ) {
if (!registrationSettings.UsersCanRegister) {
return HttpNotFound();
}

ViewData["PasswordLength"] = MinPasswordLength;

var shape = _orchardServices.New.Register();
return new ShapeResult(this, shape);
return new ShapeResult(this, shape);
}

[HttpPost]
Expand All @@ -129,7 +127,7 @@ public ActionResult Register() {
public ActionResult Register(string userName, string email, string password, string confirmPassword, string returnUrl = null) {
// ensure users can register
var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.UsersCanRegister ) {
if (!registrationSettings.UsersCanRegister) {
return HttpNotFound();
}

Expand All @@ -141,13 +139,13 @@ public ActionResult Register(string userName, string email, string password, str
var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, false));

if (user != null) {
if ( user.As<UserPart>().EmailStatus == UserStatus.Pending ) {
if (user.As<UserPart>().EmailStatus == UserStatus.Pending) {
var siteUrl = _orchardServices.WorkContext.CurrentSite.BaseUrl;
if(String.IsNullOrWhiteSpace(siteUrl)) {
if (String.IsNullOrWhiteSpace(siteUrl)) {
siteUrl = HttpContext.Request.ToRootUrlString();
}

_userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.MakeAbsolute(Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", nonce = nonce}), siteUrl));
_userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.MakeAbsolute(Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", nonce = nonce }), siteUrl));

_userEventHandler.SentChallengeEmail(user);
return RedirectToAction("ChallengeEmailSent", new { ReturnUrl = returnUrl });
Expand All @@ -163,20 +161,20 @@ public ActionResult Register(string userName, string email, string password, str

return this.RedirectLocal(returnUrl);
}

ModelState.AddModelError("_FORM", T(ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError)));
}

// If we got this far, something failed, redisplay form
var shape = _orchardServices.New.Register();
return new ShapeResult(this, shape);
return new ShapeResult(this, shape);
}

[AlwaysAccessible]
public ActionResult RequestLostPassword() {
// ensure users can request lost password
var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.EnableLostPassword ) {
if (!registrationSettings.EnableLostPassword) {
return HttpNotFound();
}

Expand All @@ -188,11 +186,11 @@ public ActionResult RequestLostPassword() {
public ActionResult RequestLostPassword(string username) {
// ensure users can request lost password
var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.EnableLostPassword ) {
if (!registrationSettings.EnableLostPassword) {
return HttpNotFound();
}

if(String.IsNullOrWhiteSpace(username)){
if (String.IsNullOrWhiteSpace(username)) {
ModelState.AddModelError("username", T("You must specify a username or e-mail."));
return View();
}
Expand All @@ -205,7 +203,7 @@ public ActionResult RequestLostPassword(string username) {
_userService.SendLostPasswordEmail(username, nonce => Url.MakeAbsolute(Url.Action("LostPassword", "Account", new { Area = "Orchard.Users", nonce = nonce }), siteUrl));

_orchardServices.Notifier.Information(T("Check your e-mail for the confirmation link."));

return RedirectToAction("LogOn");
}

Expand All @@ -226,31 +224,32 @@ public ActionResult ChangePassword() {
public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword) {
ViewData["PasswordLength"] = MinPasswordLength;

if ( !ValidateChangePassword(currentPassword, newPassword, confirmPassword) ) {
if (!ValidateChangePassword(currentPassword, newPassword, confirmPassword)) {
return View();
}

try {
var validated = _membershipService.ValidateUser(User.Identity.Name, currentPassword);

if ( validated != null ) {
if (validated != null) {
_membershipService.SetPassword(validated, newPassword);
_userEventHandler.ChangedPassword(validated);
return RedirectToAction("ChangePasswordSuccess");
}

ModelState.AddModelError("_FORM",
T("The current password is incorrect or the new password is invalid."));
return ChangePassword();
} catch {
}
catch {
ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid."));
return ChangePassword();
}
}

[AlwaysAccessible]
public ActionResult LostPassword(string nonce) {
if ( _userService.ValidateLostPassword(nonce) == null ) {
if (_userService.ValidateLostPassword(nonce) == null) {
return RedirectToAction("LogOn");
}

Expand All @@ -264,7 +263,7 @@ public ActionResult LostPassword(string nonce) {
[ValidateInput(false)]
public ActionResult LostPassword(string nonce, string newPassword, string confirmPassword) {
IUser user;
if ( (user = _userService.ValidateLostPassword(nonce)) == null ) {
if ((user = _userService.ValidateLostPassword(nonce)) == null) {
return Redirect("~/");
}

Expand Down Expand Up @@ -313,7 +312,7 @@ public ActionResult ChallengeEmailFail() {
public ActionResult ChallengeEmail(string nonce) {
var user = _userService.ValidateChallenge(nonce);

if ( user != null ) {
if (user != null) {
_userEventHandler.ConfirmedEmail(user);

return RedirectToAction("ChallengeEmailSuccess");
Expand All @@ -324,14 +323,14 @@ public ActionResult ChallengeEmail(string nonce) {

#region Validation Methods
private bool ValidateChangePassword(string currentPassword, string newPassword, string confirmPassword) {
if ( String.IsNullOrEmpty(currentPassword) ) {
if (String.IsNullOrEmpty(currentPassword)) {
ModelState.AddModelError("currentPassword", T("You must specify a current password."));
}
if ( newPassword == null || newPassword.Length < MinPasswordLength ) {
if (newPassword == null || newPassword.Length < MinPasswordLength) {
ModelState.AddModelError("newPassword", T("You must specify a new password of {0} or more characters.", MinPasswordLength));
}

if ( !String.Equals(newPassword, confirmPassword, StringComparison.Ordinal) ) {
if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal)) {
ModelState.AddModelError("_FORM", T("The new password and confirmation password do not match."));
}

Expand Down

0 comments on commit ba722cf

Please sign in to comment.