public ActionResult DissociateRole(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var user = db.Users.Find(id);

            if (user == null)
            {
                return(HttpNotFound());
            }
            var context         = new ESportsEventsContext();
            var roleManager     = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var allRoles        = roleManager.Roles.ToList();
            var associatedRoles = user.Roles.Select(r => allRoles.FirstOrDefault(ar => ar.Id == r.RoleId).Name);

            if (!associatedRoles.Any())
            {
                return(this.RedirectToAction("Index", "Users"));
            }

            var model = new DissociateRoleBindingModel
            {
                AssociatedRoles = associatedRoles,
                Id       = id,
                Username = user.UserName
            };

            return(this.View(model));
        }
Exemple #2
0
 private void CreateNoVenue(ESportsEventsContext context)
 {
     context.Venues.Add(new Venue()
     {
         Name = "No venue", Country = context.Countries.FirstOrDefault(c => c.Name == "No country"), City = context.Cities.FirstOrDefault(c => c.Name == "No city")
     });
     context.SaveChanges();
 }
Exemple #3
0
 private void CreateNoTeam(ESportsEventsContext context)
 {
     context.Teams.Add(new Team()
     {
         Name = "No team", Country = context.Countries.FirstOrDefault(c => c.Name == "No country"), City = context.Cities.FirstOrDefault(c => c.Name == "No city")
     });
     context.SaveChanges();
 }
Exemple #4
0
 private void CreateNoCountry(ESportsEventsContext context)
 {
     context.Countries.Add(new Country()
     {
         Name = "No country"
     });
     context.SaveChanges();
 }
Exemple #5
0
        //public static bool IsInGivenRole(this RegisteredUser user, string roleName)
        //{
        //    var context = new ESportsEventsContext();
        //    //var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        //    var userRoles = Roles.GetRolesForUser(user.UserName);

        //    return userRoles.Contains(roleName);
        //}

        public static IEnumerable <string> GetRoles(this RegisteredUser user)
        {
            var context     = new ESportsEventsContext();
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var userRoles   = user.Roles.Select(r => roleManager.FindById(r.RoleId).Name);

            return(userRoles);
        }
Exemple #6
0
        public static bool IsInGivenRole(this RegisteredUser user, string roleName)
        {
            var context     = new ESportsEventsContext();
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var userRoles   = user.Roles.Select(r => roleManager.FindById(r.RoleId).Name);

            return(userRoles.Contains(roleName));
        }
        public async Task <ActionResult> DissociateRole([Bind(Include = "Id, RoleToRemove, Username")] DissociateRoleBindingModel bind)
        {
            if (bind.Id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var user = db.Users.Find(bind.Id);

            if (ModelState.IsValid)
            {
                var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                if (string.IsNullOrEmpty(bind.RoleToRemove))
                {
                    this.ModelState.AddModelError("role", "There are no more roles to be removed");
                    var context         = new ESportsEventsContext();
                    var roleManager     = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                    var allRoles        = roleManager.Roles.ToList();
                    var AssociatedRoles = user.Roles.Select(r => allRoles.FirstOrDefault(ar => ar.Id == r.RoleId).Name);
                    bind.AssociatedRoles = AssociatedRoles;
                    //bind.Username = user.UserName;
                    return(this.View(bind));
                }
                var result = await userManager.RemoveFromRoleAsync(user.Id, bind.RoleToRemove);

                foreach (var @event in user.AdministratedEvents)
                {
                    @event.EventAdmins.Remove(user);
                }
                this.db.SaveChanges();
                if (User.Identity.GetUserId() == user.Id)
                {
                    await this.SignInAsync(user, true);
                }
                //userManager.Update(user);
                if (result == IdentityResult.Failed())
                {
                    this.ModelState.AddModelError("role", "This user can not be dissociated this role");
                    var context         = new ESportsEventsContext();
                    var roleManager     = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                    var allRoles        = roleManager.Roles.ToList();
                    var AssociatedRoles = user.Roles.Select(r => allRoles.FirstOrDefault(ar => ar.Id == r.RoleId).Name);
                    bind.AssociatedRoles = AssociatedRoles;
                    //bind.Username = user.UserName;
                    return(this.View(bind));
                }

                return(this.RedirectToAction("DissociateRole", "Users"));
            }

            return(this.View());
        }
Exemple #8
0
        public void ConfigureRoles()
        {
            var context     = new ESportsEventsContext();
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var userManager = new UserManager <RegisteredUser>(new UserStore <RegisteredUser>(context));

            if (!roleManager.RoleExists("Admin"))
            {
                var role = new IdentityRole("Admin");
                roleManager.Create(role);
                this.CreateAdministrator(userManager);
            }

            if (!roleManager.RoleExists("EventAdmin"))
            {
                var role = new IdentityRole("EventAdmin");
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("Buyer"))
            {
                var role = new IdentityRole("Buyer");
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("ArticleAuthor"))
            {
                var role = new IdentityRole("ArticleAuthor");
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("Volunteer"))
            {
                var role = new IdentityRole("Volunteer");
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("Guest"))
            {
                var role = new IdentityRole("Guest");
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("PlayerAdmin"))
            {
                var role = new IdentityRole("PlayerAdmin");
                roleManager.Create(role);
            }
            if (!roleManager.RoleExists("LocationAdmin"))
            {
                var role = new IdentityRole("LocationAdmin");
                roleManager.Create(role);
            }
            if (!context.Countries.Any())
            {
                this.CreateNoCountry(context);
            }

            if (!context.Cities.Any())
            {
                this.CreateNoCity(context);
            }

            if (!context.Teams.Any())
            {
                this.CreateNoTeam(context);
            }

            if (!context.Venues.Any())
            {
                this.CreateNoVenue(context);
            }
        }