Skip to content
This repository has been archived by the owner on Dec 14, 2017. It is now read-only.

Commit

Permalink
add setting for usernames unique across tenants
Browse files Browse the repository at this point in the history
  • Loading branch information
brockallen committed Jan 10, 2013
1 parent 8ef4c11 commit 5e1caed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SecuritySettings
public bool MultiTenant { get; set; }
public string DefaultTenant { get; set; }
public bool EmailIsUsername { get; set; }
public bool UsernamesUniqueAcrossTenants { get; set; }
public bool RequireAccountVerification { get; set; }
public bool AllowLoginAfterAccountCreation { get; set; }
public int AccountLockoutFailedLoginAttempts { get; set; }
Expand All @@ -30,6 +31,7 @@ public SecuritySettings()
MultiTenant = GetAppSettings("MultiTenant", false);
DefaultTenant = GetAppSettings("DefaultTenant", "default");
EmailIsUsername = GetAppSettings("EmailIsUsername", false);
UsernamesUniqueAcrossTenants = GetAppSettings("UsernamesUniqueAcrossTenants", false);
RequireAccountVerification = GetAppSettings("RequireAccountVerification", true);
AllowLoginAfterAccountCreation = GetAppSettings("AllowLoginAfterAccountCreation", true);
AccountLockoutFailedLoginAttempts = GetAppSettings("AccountLockoutFailedLoginAttempts", 10);
Expand All @@ -45,6 +47,5 @@ private T GetAppSettings<T>(string name, T defaultValue)
if (val != null) return (T)Convert.ChangeType(val, typeof(T));
return defaultValue;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ public virtual bool UsernameExists(string tenant, string username)
if (String.IsNullOrWhiteSpace(tenant)) return false;
if (String.IsNullOrWhiteSpace(username)) return false;

return this.userRepository.GetAll().Where(x => x.Tenant == tenant && x.Username == username).Any();
if (SecuritySettings.Instance.UsernamesUniqueAcrossTenants)
{
return this.userRepository.GetAll().Where(x => x.Username == username).Any();
}
else
{
return this.userRepository.GetAll().Where(x => x.Tenant == tenant && x.Username == username).Any();
}
}

public virtual bool EmailExists(string email)
Expand Down Expand Up @@ -165,16 +172,22 @@ public virtual UserAccount CreateAccount(string tenant, string username, string
throw new ValidationException("Email is invalid.");
}

if ((!SecuritySettings.Instance.EmailIsUsername && UsernameExists(tenant, username))
|| EmailExists(tenant, email))
if (UsernameExists(tenant, username))
{
throw new ValidationException("Username/Email already in use.");
var msg = SecuritySettings.Instance.EmailIsUsername ? "Email" : "Username";
throw new ValidationException(msg + " already in use.");
}

if (EmailExists(tenant, username))
{
throw new ValidationException("Email already in use.");
}

using (var tx = new TransactionScope())
{
var account = UserAccount.Create(tenant, username, password, email);
this.userRepository.Add(account);

if (this.notificationService != null)
{
if (SecuritySettings.Instance.RequireAccountVerification)
Expand All @@ -186,6 +199,7 @@ public virtual UserAccount CreateAccount(string tenant, string username, string
this.notificationService.SendAccountVerified(account);
}
}

this.userRepository.SaveChanges();
tx.Complete();

Expand Down

0 comments on commit 5e1caed

Please sign in to comment.