public ActionResult InviteBrokers(BrokerInputViewModel broker) { var brokerUser = this.usersService.GetAll() .Where(u => u.UserName.Contains(broker.UserName)) .FirstOrDefault(); var userId = this.User.Identity.GetUserId(); var currentlyLoggedUser = this.usersService.GetUserDetailsById(userId); var agencyEncodedId = this.agenciesService.EncodeId((int)currentlyLoggedUser.MyOwnAgencyId); if (brokerUser.MyOwnAgencyId == null && brokerUser.AgencyWorkForId == null) { var agency = this.agenciesService.GetById((int)currentlyLoggedUser.MyOwnAgencyId); brokerUser.AgencyWorkForId = agency.Id; brokerUser.AgencyWorkFor = agency; var role = this.rolesService.GetRoleByName("Broker"); brokerUser.Roles.Add(new IdentityUserRole() { RoleId = role.Id }); agency.Brokers.Add(brokerUser); this.agenciesService.Update(agency); this.usersService.Update(brokerUser); } else { return(this.RedirectToAction("ErrorUserAlredyIsInAgency", "Error")); } return(this.RedirectToAction("AgencyDetails", "Agencies", new { id = agencyEncodedId })); }
public ActionResult InviteBrokers(BrokerInputViewModel broker) { var brokerUser = this.usersService.GetAll() .Where(u => u.UserName.Contains(broker.UserName)) .FirstOrDefault(); var userId = this.User.Identity.GetUserId(); var currentlyLoggedUser = this.usersService.GetUserDetailsById(userId); var agencyEncodedId = this.agenciesService.EncodeId((int)currentlyLoggedUser.MyOwnAgencyId); if (brokerUser.MyOwnAgencyId == null && brokerUser.AgencyWorkForId == null) { var agency = this.agenciesService.GetById((int)currentlyLoggedUser.MyOwnAgencyId); brokerUser.AgencyWorkForId = agency.Id; brokerUser.AgencyWorkFor = agency; var role = this.rolesService.GetRoleByName("Broker"); brokerUser.Roles.Add(new IdentityUserRole() { RoleId = role.Id }); agency.Brokers.Add(brokerUser); this.agenciesService.Update(agency); this.usersService.Update(brokerUser); } else { return this.RedirectToAction("ErrorUserAlredyIsInAgency", "Error"); } return this.RedirectToAction("AgencyDetails", "Agencies", new { id = agencyEncodedId }); }