Example #1
0
        public ActionResult AddRole(string teema, AccountRoleModel model)
        {
            if (ModelState.IsValid)
            {
                int myRoleId = entities.TeemaAccesses.Single(ta => ta.Teema.Name == teema && ta.User.Username == User.Identity.Name).RoleId;
                if (model.Role != TeemaRoles.Undefined)
                {
                    if (myRoleId < (int)model.Role)
                    {
                        model.Role = (TeemaRoles)myRoleId;
                    }

                    if (!entities.TeemaAccesses.Any(ta => ta.Teema.Name == teema && ta.User.Username == model.Username))
                    {
                        TeemaAccess ta = new TeemaAccess()
                        {
                            TeemaId = entities.Teemas.Single(t => t.Name == teema).Id,
                            UserId  = entities.Users.Single(u => u.Username == model.Username).Id,
                            RoleId  = (int)model.Role
                        };
                        entities.TeemaAccesses.Add(ta);
                        entities.SaveChanges();
                    }
                }

                model.Username = null;
                model.Role     = TeemaRoles.Undefined;
            }
            return(PartialView(model));
        }
Example #2
0
        public ActionResult EditRoles(string teema, List <AccountRoleModel> model)
        {
            if (ModelState.IsValid)
            {
                TeemaDBEntities entities = new TeemaDBEntities();

                List <AccountRoleModel> originalModels = new List <AccountRoleModel>();
                int teemaId = entities.Teemas.First(t => t.Name == teema).Id;

                foreach (TeemaAccess ta in entities.TeemaAccesses.Where(ta => ta.TeemaId == teemaId))
                {
                    originalModels.Add(new AccountRoleModel()
                    {
                        Username = ta.User.Username, Role = (TeemaRoles)ta.RoleId
                    });
                }

                List <AccountRoleModel> toRemove = new List <AccountRoleModel>();
                foreach (AccountRoleModel roleModel in originalModels)
                {
                    if (!model.Any(m => m.Username == roleModel.Username && m.Role == roleModel.Role))
                    {
                        toRemove.Add(roleModel);
                    }
                }

                List <AccountRoleModel> toAdd = new List <AccountRoleModel>();
                foreach (AccountRoleModel roleModel in model)
                {
                    if (!originalModels.Any(o => o.Username == roleModel.Username && o.Role == roleModel.Role))
                    {
                        toAdd.Add(roleModel);
                    }
                }

                //deletes roles that have been removed
                if (toRemove.Count() > 0)
                {
                    foreach (AccountRoleModel role in toRemove)
                    {
                        int         userId = entities.Users.First(u => u.Username == role.Username).Id;
                        TeemaAccess access = entities.TeemaAccesses.First(ta => ta.TeemaId == teemaId && ta.UserId == userId);
                        entities.TeemaAccesses.Remove(access);
                    }
                    entities.SaveChanges();
                }

                //adds roles that have been added to the original model
                if (toAdd.Count() > 0)
                {
                    int myRoleId = entities.TeemaAccesses.Single(ta => ta.Teema.Name == teema && ta.User.Username == User.Identity.Name).RoleId;
                    foreach (AccountRoleModel role in toAdd)
                    {
                        if (role.Role != TeemaRoles.Undefined)
                        {
                            if (myRoleId < (int)role.Role)
                            {
                                role.Role = (TeemaRoles)myRoleId;
                            }
                            int userId = entities.Users.First(u => u.Username == role.Username).Id;
                            entities.TeemaAccesses.Add(new TeemaAccess()
                            {
                                UserId  = userId,
                                TeemaId = teemaId,
                                RoleId  = (int)role.Role,
                            });
                        }
                    }
                    entities.SaveChanges();
                }
            }
            return(RedirectToAction("EditRoles", new { teema = teema }));
        }