Ejemplo n.º 1
0
        public JsonResult json_savePermissions()
        {
            var pars = Request.Params;

            string roleIds = pars["roleIds"];


            using (TransactionScope t = new TransactionScope())
            {
                using (Models.prjEntities ctx = new Models.prjEntities())
                {
                    //Delete all related permissions first
                    var allUserPermissions =
                        (
                            from p in ctx.permission
                            select p
                        );
                    foreach (var pi in allUserPermissions)
                    {
                        ctx.permission.DeleteObject(pi);
                        ctx.SaveChanges();
                    }
                    if (!String.IsNullOrEmpty(roleIds))
                    {
                        string[] roles = roleIds.Split(',');
                        foreach (var role in roles)
                        {
                            var roleId = Convert.ToInt32(role);
                            //Check if already exists
                            var roleUserExists =
                                (
                                    from p in ctx.permission
                                    where p.user_id == 1 && p.role_id == (int)roleId
                                    select new { temp = 1 }
                                );

                            if (roleUserExists.Count() != 0)
                            {
                                continue;
                            }

                            //Build New Permission Object
                            var newPermission = new Models.permission()
                            {
                                user_id = 1,
                                role_id = roleId
                            };

                            ctx.permission.AddObject(newPermission);
                            ctx.SaveChanges();
                        }
                    }

                    t.Complete();
                }
            }

            return(Json(new { result = "Done" }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult json_savePermissions()
        {
            var pars = Request.Params;

            string roleIds = pars["roleIds"];

            using (TransactionScope t = new TransactionScope())
            {
                using (Models.prjEntities ctx = new Models.prjEntities())
                {
                    //Delete all related permissions first
                    var allUserPermissions =
                    (
                            from p in ctx.permission
                            select p
                    );
                    foreach (var pi in allUserPermissions)
                    {
                        ctx.permission.DeleteObject(pi);
                        ctx.SaveChanges();
                    }
                    if (!String.IsNullOrEmpty(roleIds))
                    {
                        string[] roles = roleIds.Split(',');
                        foreach (var role in roles)
                        {
                            var roleId = Convert.ToInt32(role);
                            //Check if already exists
                            var roleUserExists =
                            (
                               from p in ctx.permission
                               where p.user_id == 1 && p.role_id == (int)roleId
                               select new { temp = 1 }
                            );

                            if (roleUserExists.Count() != 0)
                                continue;

                            //Build New Permission Object
                            var newPermission = new Models.permission()
                            {
                                user_id = 1,
                                role_id = roleId
                            };

                            ctx.permission.AddObject(newPermission);
                            ctx.SaveChanges();
                        }
                    }

                    t.Complete();
                }

            }

            return Json(new { result = "Done" }, JsonRequestBehavior.AllowGet);
        }