Esempio n. 1
0
        public ActionResult Save(TeamModels team)
        {
            if (!ModelState.IsValid)
            {
                List <ApplicationUser> usersInDb = context.Users.ToList();
                var index = usersInDb.FindIndex(m => m.UserName == "*****@*****.**");
                usersInDb.RemoveAt(index);
                var viewModel = new NewTeamViewModel
                {
                    Team  = team,
                    Users = usersInDb
                };
                return(View("TeamForm", viewModel));
            }
            if (team.Id == 0)
            {
                context.Teams.Add(team);
            }
            else
            {
                var dbTeam = context.Teams.SingleOrDefault(s => s.Id == team.Id);
                if (dbTeam == null)
                {
                    return(HttpNotFound());
                }

                dbTeam.Title        = team.Title;
                dbTeam.TeamLeaderId = team.TeamLeaderId;
            }
            context.SaveChanges();

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //create IronManRules for this user
                var ironManRules = new IronManRuleModels
                {
                    StartDate      = model.StartDate,
                    DurationInDays = model.Duration
                };
                _context.IronManRules.Add(ironManRules);
                _context.SaveChanges();

                //take the name from the model.string field and save it in the TeamModels table
                var team = new TeamModels
                {
                    Name = model.TeamName,
                    IronManRuleModelsId = ironManRules.Id,
                    LinkToPhoto         = "defaultTeamImage.png"
                };
                _context.Teams.Add(team);
                _context.SaveChanges();

                //get the Id from the newly created Team and save it in the AspNetUsers Table
                var user = new ApplicationUser
                {
                    UserName     = model.Email,
                    Email        = model.Email,
                    FirstName    = model.FirstName,
                    LastName     = model.LastName,
                    Zip          = model.Zip,
                    TeamModelsId = team.Id
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //Seeding users for new installs. Incrementally comment out each section and run application.

                    //Temp Code to add Administrative user role
                    //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
                    //var roleManager = new RoleManager<IdentityRole>(roleStore);
                    //await roleManager.CreateAsync(new IdentityRole("CanManageAllData"));
                    //await UserManager.AddToRoleAsync(user.Id, "CanManageAllData");

                    //Temp Code to add Team Captain user
                    //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
                    //var roleManager = new RoleManager<IdentityRole>(roleStore);
                    //await roleManager.CreateAsync(new IdentityRole("CanManageTeamData"));
                    //await UserManager.AddToRoleAsync(user.Id, "CanManageTeamData");

                    //Temp Code to add Participant user
                    //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
                    //var roleManager = new RoleManager<IdentityRole>(roleStore);
                    //await roleManager.CreateAsync(new IdentityRole("CanManagePersonalData"));
                    //await UserManager.AddToRoleAsync(user.Id, "CanManagePersonalData");

                    //Making every new user a Participant
                    await UserManager.AddToRoleAsync(user.Id, "CanManageTeamData");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }