public string GetOrganizationId() { string identity = _caller.Identity.Name; var user = usersRepository.Find(null, u => u.UserName == identity).Items?.FirstOrDefault(); organizationMemberRepository.ForceIgnoreSecurity(); var orgMember = organizationMemberRepository.Find(null, om => om.PersonId.Equals(user.PersonId))?.Items?.FirstOrDefault(); string organizationId; if (orgMember == null) { organizationId = organizationManager.GetDefaultOrganization().Id.ToString(); } else { organizationId = orgMember.OrganizationId.ToString(); } return(organizationId); }
public PaginatedList <OrganizationCard> MyOrganizations(Guid personId, bool IncludeAccessRequestedOrg = false) { _organzationMemberRepo.ForceIgnoreSecurity(); var orgMems = _organzationMemberRepo.Find(null, om => om.PersonId.Equals(personId)); var orgIds = orgMems.Items.Select(om => om.OrganizationId).Distinct().ToArray(); var orgs = _organizationRepo.Find(null, o => orgIds.Contains(o.Id)); _organzationMemberRepo.ForceSecurity(); PaginatedList <OrganizationCard> cards = new PaginatedList <OrganizationCard>(); foreach (Organization org in orgs.Items) { OrganizationCard card = new OrganizationCard { Id = org.Id.Value, Name = org.Name, CreatedOn = org.CreatedOn, CanLeave = true }; var orgMem = orgMems.Items.Where(om => om.OrganizationId == org.Id).FirstOrDefault(); if (orgMem != null) { card.JoinedOn = orgMem.CreatedOn; card.IsOrganizationMember = true; if (orgMem.IsAdministrator.HasValue && orgMem.IsAdministrator.Value) { card.CanDelete = true; } } Guid creatorPersonId = Guid.Empty; if (Guid.TryParse(org.CreatedBy, out creatorPersonId)) { Person creatorPerson = _personRepo.GetOne(creatorPersonId); if (creatorPerson != null) { card.CreatedBy = string.Format("{0}", creatorPerson.Name); } } if (string.IsNullOrEmpty(card.CreatedBy)) { card.CreatedBy = org.CreatedBy; } cards.Add(card); } if (IncludeAccessRequestedOrg) { //organizations requested for access var accessRequest = PendingOrganizationAccess(personId); if (accessRequest?.Items?.Count > 0) { cards.Items.AddRange(accessRequest.Items); } } cards.ParentId = personId; cards.TotalCount = cards.Items.Count; return(cards); }
public async Task <IActionResult> Register(SignUpViewModel signupModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } EmailVerification emailAddress = emailVerificationRepository.Find(null, p => p.Address.Equals(signupModel.Email, StringComparison.OrdinalIgnoreCase)).Items?.FirstOrDefault(); if (emailAddress != null) { organizationMemberRepository.ForceIgnoreSecurity(); var member = organizationMemberRepository.Find(null, m => m.PersonId == emailAddress.PersonId)?.Items?.FirstOrDefault(); organizationMemberRepository.ForceSecurity(); if (member != null) { //Already a member of the organization ModelState.AddModelError("Register", "Email address already exists"); return(BadRequest(ModelState)); } // Make a request to join organization var oldOrganization = organizationManager.GetDefaultOrganization(); if (oldOrganization != null) { //Update user if (!IsPasswordValid(signupModel.Password)) { ModelState.AddModelError("Password", PasswordRequirementMessage(signupModel.Password)); return(BadRequest(ModelState)); } var existingUser = await userManager.FindByEmailAsync(emailAddress.Address).ConfigureAwait(false); existingUser.Name = signupModel.Name; existingUser.ForcedPasswordChange = false; existingUser.PasswordHash = userManager.PasswordHasher.HashPassword(existingUser, signupModel.Password); var result = await userManager.UpdateAsync(existingUser).ConfigureAwait(true); if (!result.Succeeded) { return(GetErrorResult(result)); } Person person = personRepository.Find(null, p => p.Id == emailAddress.PersonId)?.Items?.FirstOrDefault(); person.Name = signupModel.Name; person.Department = signupModel.Department; personRepository.Update(person); //Create a new access request Model.Membership.AccessRequest accessRequest = new Model.Membership.AccessRequest() { OrganizationId = oldOrganization.Id, PersonId = person.Id, IsAccessRequested = true, AccessRequestedOn = DateTime.UtcNow }; accessRequestManager.AddAnonymousAccessRequest(accessRequest); return(Ok("Access Request has been created for existing user")); } } var user = new ApplicationUser() { Name = signupModel.Name, UserName = signupModel.Email, Email = signupModel.Email, ForcedPasswordChange = false //Set this property to not show password reset secreen for new user }; RandomPassword randomPass = new RandomPassword(); string passwordString = ""; bool isPasswordProvided = false; if (string.IsNullOrWhiteSpace(signupModel.Password)) { passwordString = randomPass.GenerateRandomPassword(); isPasswordProvided = false; } else { passwordString = signupModel.Password; isPasswordProvided = true; } var loginResult = await userManager.CreateAsync(user, passwordString).ConfigureAwait(false); bool IsEmailAllowed = emailSender.IsEmailAllowed(); if (!loginResult.Succeeded) { return(GetErrorResult(loginResult)); } else { //Add person email var emailIds = new List <EmailVerification>(); var personEmail = new EmailVerification() { PersonId = Guid.Empty, Address = signupModel.Email, IsVerified = false }; emailIds.Add(personEmail); Person newPerson = new Person() { Company = signupModel.Organization, Department = signupModel.Department, Name = signupModel.Name, EmailVerifications = emailIds }; var person = personRepository.Add(newPerson); var oldOrganization = organizationManager.GetDefaultOrganization(); if (oldOrganization != null) { //Add it to access requests Model.Membership.AccessRequest accessRequest = new Model.Membership.AccessRequest() { OrganizationId = oldOrganization.Id, PersonId = person.Id, IsAccessRequested = true, AccessRequestedOn = DateTime.UtcNow }; accessRequestManager.AddAnonymousAccessRequest(accessRequest); } if (IsEmailAllowed) { string code = await userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false); EmailMessage emailMessage = new EmailMessage(); EmailAddress address = new EmailAddress(user.Name, user.Email); emailMessage.To.Add(address); emailMessage.Body = SendConfirmationEmail(code, user.Id, passwordString, "en"); emailMessage.Subject = "Confirm your account at " + Constants.PRODUCT; await emailSender.SendEmailAsync(emailMessage).ConfigureAwait(false); } else { ModelState.AddModelError("Email", "Email is disabled. Verification email was not sent."); } //Update the user if (person != null) { var registeredUser = userManager.FindByNameAsync(user.UserName).Result; registeredUser.PersonId = (Guid)person.Id; registeredUser.ForcedPasswordChange = true; await userManager.UpdateAsync(registeredUser).ConfigureAwait(false); } } if (!IsEmailAllowed) { return(Ok(ModelState)); } else { return(Ok()); } }