예제 #1
0
        public async Task <IActionResult> Edit(string id, [Bind("Id,RoleId,NavigationMenuId")] RoleMenuPermission roleMenuPermission)
        {
            if (id != roleMenuPermission.RoleId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(roleMenuPermission);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoleMenuPermissionExists(roleMenuPermission.RoleId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["NavigationMenuId"] = new SelectList(_context.NavigationMenu, "Id", "Id", roleMenuPermission.NavigationMenuId);
            return(View(roleMenuPermission));
        }
예제 #2
0
        public async Task SetMenuForRole(RoleMenuPermission rmp)
        {
            DataTable table = new DataTable();

            table.Columns.Add("RoleId", typeof(int));
            table.Columns.Add("MenuId", typeof(int));
            DataRow row;


            foreach (var item in rmp.Menus)
            {
                row           = table.NewRow();
                row["RoleId"] = rmp.RoleId;
                row["MenuId"] = item.MenuId;
                table.Rows.Add(row);
            }

            using (IDbConnection cnn = new SqlConnection(GetConnectionString()))
            {
                var p = new DynamicParameters();
                p.Add("@RoleId", rmp.RoleId);
                p.AddDynamicParams(new{ Menus = table.AsTableValuedParameter("dbo.[MenuListForRole]") });
                string sql = "dbo.[spRole_AddMenu]";
                await cnn.ExecuteAsync(sql, p, commandType : CommandType.StoredProcedure);
            }
        }
예제 #3
0
        public async Task <bool> AddMenuItem(NavigationMenuViewModel vm) //TODO: dodać uprawnienia roli!!!!
        {
            try
            {
                NavigationMenu menuItem = new NavigationMenu
                {
                    Name           = vm.Name,
                    WithoutLinking = vm.WithoutLinking,
                    ActionName     = vm.ActionName,
                    ControllerName = vm.ControllerName,
                    IconClass      = vm.IconClass
                };
                var parent = await _db.NavigationMenu.FindAsync(vm.ParentMenuId);

                if (parent != null)
                {
                    menuItem.ParentMenuId = parent.Id;
                }
                _db.NavigationMenu.Add(menuItem);
                await _db.SaveChangesAsync();

                IdentityRole userRole = null;

                if (!string.IsNullOrEmpty(vm.RoleName))
                {
                    userRole = await _db.Roles.Where(r => r.Name == vm.RoleName).FirstOrDefaultAsync();
                }
                else
                {
                    userRole = await _db.Roles.Where(r => r.Name == "Admin").FirstOrDefaultAsync();
                }


                if (userRole != null)
                {
                    RoleMenuPermission rmp = new RoleMenuPermission
                    {
                        RoleId           = userRole.Id,
                        NavigationMenuId = menuItem.Id
                    };
                    _db.RoleMenuPermission.Add(rmp);
                    await _db.SaveChangesAsync();
                }


                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
                return(false);
            }
        }
예제 #4
0
        public async Task <IActionResult> Create([Bind("Id,RoleId,NavigationMenuId")] RoleMenuPermission roleMenuPermission)
        {
            if (ModelState.IsValid)
            {
                roleMenuPermission.RoleId = Guid.NewGuid().ToString();
                roleMenuPermission.Status = 1002;
                _context.Add(roleMenuPermission);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Roles"));
            }
            ViewData["NavigationMenuId"] = new SelectList(_context.NavigationMenu, "Id", "Id", roleMenuPermission.NavigationMenuId);
            return(RedirectToAction("Index", "Roles"));
        }
예제 #5
0
        public async Task <IHttpActionResult> PostMenuForRole(RoleMenuPermission rmp)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }

            rmp.Menus = rmp.Menus.Where(a => a.IsSelected).ToList();

            if (rmp.Menus.Count < 1)
            {
                return(BadRequest("No menu is selected for permission"));
            }

            await _repo.SetMenuForRole(rmp);

            return(Ok());
        }
예제 #6
0
        public async Task <IActionResult> CreateRoles(IdentityRole role, List <string> roles, string rolename)
        {
            List <RoleMenuExtRMP> p = new List <RoleMenuExtRMP>();

            role.Name = rolename;

            await roleManager.CreateAsync(role);

            var local = _context.Set <IdentityRole>().Local.FirstOrDefault(entry => entry.Id.Equals(role.Id));

            // check if local is not null
            if (local != null)
            {
                // detach
                _context.Entry(local).State = EntityState.Detached;
            }

            foreach (var x in roles)
            {
                RoleMenuExtRMP permission = new RoleMenuExtRMP();

                permission.NavigationMenuId = new Guid(x.Substring(2, 36));
                permission.TempNavMenu      = x.Substring(0, 1);

                p.Add(permission);
            }

            var count = p.GroupBy(x => x.NavigationMenuId).Select(c => new
            {
                id     = c.Key,
                count  = c.Select(s => s.RoleId).Distinct().Count(),
                status = c.Select(s => s.TempNavMenu).ToList()
            }).ToList();

            foreach (var c in count)
            {
                RoleMenuPermission permission = new RoleMenuPermission();
                var alpha = string.Join("", c.status);

                var parent = await _context.NavigationMenu.Where(x => x.Id == c.id).Select(x => x.ParentMenuId).SingleOrDefaultAsync();

                var subparent = await _context.NavigationMenu.Where(x => x.Id == parent.Value).Select(x => x.ParentMenuId).SingleOrDefaultAsync();

                var home = _context.RoleMenuPermission.Any(x => x.NavigationMenuId == Guid.Parse("3C10AD2B-9D75-4E68-811A-A5E7B7B782EE") && x.RoleId == role.Id);

                if (!home)
                {
                    permission.RoleId           = Guid.NewGuid().ToString();
                    permission.NavigationMenuId = Guid.Parse("3C10AD2B-9D75-4E68-811A-A5E7B7B782EE");
                    permission.RoleId           = role.Id;
                    permission.Status           = 5;

                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();
                }

                if (subparent != null)
                {
                    var checker = _context.RoleMenuPermission.Any(x => x.NavigationMenuId == subparent.Value && x.RoleId == role.Id);

                    if (!checker)
                    {
                        permission.RoleId           = Guid.NewGuid().ToString();
                        permission.NavigationMenuId = subparent.Value;
                        permission.RoleId           = role.Id;
                        permission.Status           = 5;

                        _context.RoleMenuPermission.Add(permission);
                        await _context.SaveChangesAsync();
                    }
                }

                if (parent != null)
                {
                    var checker = _context.RoleMenuPermission.Any(x => x.NavigationMenuId == parent.Value && x.RoleId == role.Id);

                    if (!checker)
                    {
                        permission.RoleId           = Guid.NewGuid().ToString();
                        permission.NavigationMenuId = parent.Value;
                        permission.RoleId           = role.Id;
                        permission.Status           = 5;

                        _context.RoleMenuPermission.Add(permission);
                        await _context.SaveChangesAsync();
                    }
                }

                permission.RoleId           = Guid.NewGuid().ToString();
                permission.NavigationMenuId = c.id;
                permission.RoleId           = role.Id;

                switch (alpha)
                {
                case "a":
                    permission.Status = 1;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "b":
                    permission.Status = 2;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "c":
                    permission.Status = 3;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "d":
                    permission.Status = 4;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "ab":
                    permission.Status = 6;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "ac":
                    permission.Status = 7;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "ad":
                    permission.Status = 8;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "bc":
                    permission.Status = 9;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "bd":
                    permission.Status = 10;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "cd":
                    permission.Status = 11;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "abc":
                    permission.Status = 12;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "abd":
                    permission.Status = 13;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "acd":
                    permission.Status = 14;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "bcd":
                    permission.Status = 15;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                case "abcd":
                    permission.Status = 5;
                    _context.RoleMenuPermission.Add(permission);
                    await _context.SaveChangesAsync();

                    break;

                default:
                    Console.WriteLine("Other");
                    break;
                }
            }

            return(RedirectToAction("Index"));
        }