protected override void Handle(CreateOrganizationAdminRequest request, CreateOrganizationAdminResponse response) { var systemAccount = _systemAccountRepository.GetByIdentifier(request.Email); var organization = _organizationRepository.GetByKey(request.OrganizationKey); if (systemAccount == null) { var identityServiceResponse = _systemAccountIdentityServiceManager.Create(request.Username, request.Email); if (identityServiceResponse.Sucess) { systemAccount = _systemAccountFactory.Create(organization, request.Email, new Email(request.Email)); var systemAccountDto = Mapper.Map <SystemAccount, SystemAccountDto>(systemAccount); response.SystemAccountDto = systemAccountDto; var role = _roleFactory.Create(organization, "Default Organization Admin"); role.AddPermision(BasicAccessPermission.AccessUserInterfacePermission); role.AddPermision(OrganizationPermission.OrganizationViewPermission); role.AddPermision(OrganizationPermission.OrganizationEditPermission); role.AddPermision(StaffPermission.StaffAddRolePermission); role.AddPermision(StaffPermission.StaffCreateAccountPermission); role.AddPermision(StaffPermission.StaffEditPermission); role.AddPermision(StaffPermission.StaffLinkAccountPermission); role.AddPermision(StaffPermission.StaffRemoveRolePermission); role.AddPermision(StaffPermission.StaffViewPermission); role.AddPermision(RolePermission.RoleAddPermissionPermission); role.AddPermision(RolePermission.RoleEditPermission); role.AddPermision(RolePermission.RoleRemovePermissionPermission); role.AddPermision(RolePermission.RoleViewPermission); role.AddPermision(PatientPermission.PatientEditPermission); role.AddPermision(PatientPermission.PatientViewPermission); role.AddPermision(AssessmentPermission.AssessmentEditPermission); role.AddPermision(AssessmentPermission.AssessmentViewPermission); role.AddPermision(StaffPermission.ResetPasswordPermission); systemAccount.AddRole(role); } else { var result = identityServiceResponse.ErrorMessage; var dataErrorInfo = new DataErrorInfo(result, ErrorLevel.Error); response.SystemAccountDto = new SystemAccountDto(); response.SystemAccountDto.AddDataErrorInfo(dataErrorInfo); } } else { _systemAccountIdentityServiceManager.ResetPassword(systemAccount.Email.Address); } }
protected override void Handle(AssignAccountRequest request, AssignAccountResponse response) { if (request.SystemAccountDto.CreateNew) { var systemAccount = _systemAccountRepository.GetByIdentifier(request.SystemAccountDto.Identifier); var staff = _staffRepository.GetByKey(request.StaffKey); if (systemAccount != null) // account existing { var dataErrorInfo = new DataErrorInfo(string.Format("Cannot create account because an account with the email {0} already exists.", request.SystemAccountDto.Identifier), ErrorLevel.Error); response.SystemAccountDto = request.SystemAccountDto; response.SystemAccountDto.AddDataErrorInfo(dataErrorInfo); } else { // 1. create member login in Identity server // 2. Create System account in domain // 3. assign system account to the new staff // 4. error handling: if the login/account is taken or cannot create new login if (staff != null) { var identityServerResponse = _systemAccountIdentityServiceManager.Create(request.SystemAccountDto.Username, request.SystemAccountDto.Email); if (identityServerResponse.Sucess) { var organization = _organizationRepository.GetByKey(UserContext.OrganizationKey); systemAccount = _systemAccountFactory.Create(organization, request.SystemAccountDto.Email, new Email(request.SystemAccountDto.Email)); systemAccount.AssignToStaff(staff); var systemAccountDto = Mapper.Map <SystemAccount, SystemAccountDto>(systemAccount); response.SystemAccountDto = systemAccountDto; } else { var result = identityServerResponse.ErrorMessage; var dataErrorInfo = new DataErrorInfo(result, ErrorLevel.Error); response.SystemAccountDto = request.SystemAccountDto; response.SystemAccountDto.AddDataErrorInfo(dataErrorInfo); } } else { Logger.Error(string.Format("Tried assigning invalid staff {0} to systemaccount {1}", request.StaffKey, systemAccount.Key)); response.SystemAccountDto.AddDataErrorInfo(new DataErrorInfo("Invalid staff key.", ErrorLevel.Error)); } } } else { var systemAccount = _systemAccountRepository.GetByIdentifier(request.SystemAccountDto.Identifier); var staff = _staffRepository.GetByKey(request.StaffKey); if (systemAccount != null) // account existing { if (systemAccount.Staff == null) { systemAccount.AssignToStaff(staff); var systemAccountDto = Mapper.Map <SystemAccount, SystemAccountDto>(systemAccount); response.SystemAccountDto = systemAccountDto; } else { var dataErrorInfo = new DataErrorInfo(string.Format("Cannot link account because an account with the email {0} has been assigned to another staff.", request.SystemAccountDto.Identifier), ErrorLevel.Error); response.SystemAccountDto = request.SystemAccountDto; response.SystemAccountDto.AddDataErrorInfo(dataErrorInfo); } } else { var dataErrorInfo = new DataErrorInfo(string.Format("Cannot link account because an account with the email {0} does not exist.", request.SystemAccountDto.Identifier), ErrorLevel.Error); response.SystemAccountDto = request.SystemAccountDto; response.SystemAccountDto.AddDataErrorInfo(dataErrorInfo); } } }