Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created March 17, 2019 10:59
Show Gist options
  • Save dealproc/b1ee4d7a84d54e42c38747576e1a6025 to your computer and use it in GitHub Desktop.
Save dealproc/b1ee4d7a84d54e42c38747576e1a6025 to your computer and use it in GitHub Desktop.
Multi-Tenant Solutions - IAccountService.cs
public interface IAccountService {
IEnumerable<AccountPmo> List();
AccountPmo Get(string guid);
AccountPmo Save(AccountPmo model);
AccountPmo Delete(string guid);
}
public class AccountService : IAccountService {
readonly IAccountRepository _accountRepository;
readonly IGroupRepository _groupRepository;
readonly IRoleRepository _roleRepository;
public AccountService(IAccountRepository accountRepository, IGroupRepository groupRepository, IRoleRepository roleRepository) {
_accountRepository = accountRepository;
_groupRepository = groupRepository;
_roleRepository = roleRepository;
}
public IEnumerable<AccountPmo> List() {
return _accountRepository.GetAll().Select(AsModel);
}
public AccountPmo Get(string guid) {
var entity = _accountRepository.GetFirst(x => x.GUID == guid);
if (entity.DoesNotExist()) { return null; }
return AsModel(entity);
}
public AccountPmo Save(AccountPmo model) {
var entity = _accountRepository.GetFirst(x => x.GUID == model.GUID)
??
new DataModel.Users.Account() { CreatedOnUtc = DateTime.UtcNow };
entity.Code = model.Code;
entity.DatabaseName = model.DatabaseName;
entity.Description = model.Description;
entity.GUID = model.GUID;
entity.Hostname = model.Hostname;
entity.IsActive = model.IsActive;
entity.IsLockedOut = model.IsLockedOut;
entity.NumberOfDevices = model.NumberOfDevices;
entity.UpdatedOnUtc = DateTime.UtcNow;
var adminRoleNeeded = entity.Id == 0;
entity = _accountRepository.SaveOrUpdate(entity);
if (entity == null) { return null; }
if (adminRoleNeeded) {
var role = CreateAdminRole(entity);
var group = CreateAdminGroup(role);
var userRole = CreateUserRole(entity);
var userGroup = CreateUsersGroup(userRole);
}
return AsModel(entity);
}
public AccountPmo Delete(string guid) {
var account = _accountRepository.GetFirst(x => x.GUID == guid);
if (account.DoesNotExist()) { return null; }
_accountRepository.Delete(account);
return AsModel(account);
}
private DataModel.Users.Group CreateAdminGroup(DataModel.Users.Role role) {
var group = new DataModel.Users.Group() {
Account = role.Account,
AutoAssign = false,
CreatedOnUtc = DateTime.UtcNow,
UpdatedOnUtc = DateTime.UtcNow,
Description = "Account Administrators",
GroupKey = role.Account.Code + "_ADM",
Name = "ACCT Admins",
ProtectFromDeletion = true
};
group.AddAccount(role.Account, role);
return _groupRepository.SaveOrUpdate(group);
}
private DataModel.Users.Role CreateAdminRole(DataModel.Users.Account account) {
var role = new DataModel.Users.Role() {
Account = account,
HasAllPermissions = true,
AutoAssign = false,
IncludedRoles = _roleRepository.GetBy(x => x.AutoAssign).ToList(),
Name = "Administrator",
CreatedOnUtc = DateTime.UtcNow,
UpdatedOnUtc = DateTime.UtcNow,
GUID = Guid.NewGuid().ToString()
};
return _roleRepository.SaveOrUpdate(role);
}
private DataModel.Users.Role CreateUserRole(DataModel.Users.Account account) {
var role = new Core.DataModel.Users.Role {
Account = account,
HasAllPermissions = false,
AutoAssign = true,
Name = "Users",
CreatedOnUtc = DateTime.UtcNow,
UpdatedOnUtc = DateTime.UtcNow,
GUID = Guid.NewGuid().ToString()
};
return _roleRepository.SaveOrUpdate(role);
}
private DataModel.Users.Group CreateUsersGroup(DataModel.Users.Role role) {
var group = new DataModel.Users.Group {
Account = role.Account,
AutoAssign = true,
CreatedOnUtc = DateTime.UtcNow,
UpdatedOnUtc = DateTime.UtcNow,
Description = "Account Users",
GroupKey = role.Account.Code + "_USR",
Name = "ACT Users",
ProtectFromDeletion = true
};
group.AddAccount(role.Account, role);
return _groupRepository.SaveOrUpdate(group);
}
private static AccountPmo AsModel(DataModel.Users.Account entity) {
return new AccountPmo() {
Code = entity.Code,
DatabaseName = entity.DatabaseName,
Description = entity.Description,
GUID = entity.GUID,
Hostname = entity.Hostname,
IsActive = entity.IsActive,
IsLockedOut = entity.IsLockedOut,
NumberOfDevices = entity.NumberOfDevices
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment