Ejemplo n.º 1
0
        public ActionResult BulkCreate()
        {
            var model = new BulkUserViewModel();
            model.RoleNames = this.roleService.GetRoles().OrderBy(r => r.RoleName).Select(r => r.RoleName);

            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult BulkCreate(BulkUserViewModel model, FormCollection form)
        {
            if (!this.ModelState.IsValid)
            {
                model.RoleNames = this.roleService.GetRoles().OrderBy(r => r.RoleName).Select(r => r.RoleName);
                return View(model);
            }

            var roles = this.roleService.GetRoles().OrderBy(r => r.RoleName).ToList();

            var emails = model.Emails.Replace(';', ',').Split(',').Select(e => e.Trim()).Distinct();

            foreach (var email in emails)
            {
                User user = this.userService.RetrieveUserByEMail(email);

                if (user == null)
                {
                    user = new User() { Name = this.GetNameFromEmail(email), Email = email };

                    this.userService.CreateUser(user);

                    int k = 1;

                    foreach (var role in roles)
                    {
                        if (!string.IsNullOrEmpty(form["Role" + k]) && form["Role" + k].Contains("true"))
                        {
                            this.roleService.AddUserToRole(role, user);
                        }

                        k++;
                    }
                }

                var invitationLink = this.GetInvitationPage();
                var expiration = DateTime.UtcNow.AddDays(30);
                this.invitationService.InviteUser(user, invitationLink, expiration, model.PersonalMessage, false);
            }

            return RedirectToAction("Index");
        }