예제 #1
0
        public ActionResult RequestAccess(RequestInviteViewModel model)
        {
            if (ModelState.IsValid)
            {
                var invitationRequest = new InvitationRequest()
                {
                    RequestDate = DateTimeOffset.Now,
                    Company     = model.Company + " " + model.Comments,
                    Email       = model.Email,
                    Name        = model.Name,
                    Status      = RequestStatus.New
                };

                Database.InvitationRequests.Add(invitationRequest);

                Database.SaveChanges();

                var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(Database));

                var admins = from role in roleManager.Roles
                             where role.Name == "System Administrator"
                             from u in role.Users
                             select u.UserId;

                IUserMailer mailer = new UserMailer();
                foreach (var admin in admins)
                {
                    Database.SystemMessages.Add(new SystemMessage()
                    {
                        UserId   = admin,
                        DateSent = DateTimeOffset.Now,
                        Message  = $"{model.Name} from {model.Company} is requesting access."
                    });

                    // send the admin an email
                    var email = Database.Users.Where(u => u.Id == admin).FirstOrDefault().Email;

                    try {
                        mailer.InvitationRequest(invitationRequest, email).Send();
                    } catch (Exception ex) {
                        return(View("MailDown", ex));
                    }
                    return(View("RequestAccessConfirmation"));
                }

                return(View("RequestAccessConfirmation"));
            }

            return(View(model));
        }