Esempio n. 1
0
        public async Task UpdateRolePermission(RoleViewModel roleViewModel)
        {
            if (roleViewModel != null && !string.IsNullOrEmpty(roleViewModel.Id) && roleViewModel.GroupViewModelList != null && roleViewModel.GroupViewModelList.Count() > 0)
            {
                var rolePermissionToRemove = _context.RolePermissions.Where(x => x.RoleId == roleViewModel.Id);
                _context.RolePermissions.RemoveRange(rolePermissionToRemove);


                foreach (var groupViewModel in roleViewModel.GroupViewModelList.Where(x => x.IsSelected))
                {
                    _context.RolePermissions.Add(new RolePermission()
                    {
                        GroupId = groupViewModel.Id, RoleId = roleViewModel.Id
                    });
                }

                var groupViewModelList = roleViewModel.GroupViewModelList.Where(x => !x.IsSelected).ToList();
                if (groupViewModelList != null && groupViewModelList.Count() > 0)
                {
                    foreach (var permissionViewModel in groupViewModelList.SelectMany(x => x.PermissionViewModelList).Where(x => x.IsSelected))
                    {
                        _context.RolePermissions.Add(new RolePermission()
                        {
                            PermissionId = permissionViewModel.Id, RoleId = roleViewModel.Id
                        });
                    }
                }

                await _context.SaveChangesAsync();
            }
        }
        public async Task UpdatePermissionGroup(GroupViewModel groupViewModel)
        {
            if (groupViewModel != null && groupViewModel.PermissionViewModelList != null && groupViewModel.PermissionViewModelList.Count() > 0)
            {
                var permissionIds = groupViewModel.PermissionViewModelList.Select(x => x.Id).ToList();

                _context.PermissionGroups.RemoveRange(_context.PermissionGroups.Where(x => x.GroupId == groupViewModel.Id));
                foreach (var permissionViewModel in groupViewModel.PermissionViewModelList.Where(x => x.IsSelected))
                {
                    _context.PermissionGroups.Add(new PermissionGroup()
                    {
                        GroupId = groupViewModel.Id, PermissionId = permissionViewModel.Id
                    });
                }
                await _context.SaveChangesAsync();
            }
        }
Esempio n. 3
0
        public async Task UpdateTrustModule(TrustViewModel trustViewModel)
        {
            if (trustViewModel != null && trustViewModel.TrustModuleViewModelList != null && trustViewModel.TrustModuleViewModelList.Count() > 0)
            {
                var moduleIds = trustViewModel.TrustModuleViewModelList.Select(x => x.ModuleId).ToList();

                _context.TrustModules.RemoveRange(_context.TrustModules.Where(x => x.TrustId == trustViewModel.Id && moduleIds.Contains(x.ModuleId)));
                _context.RolePermissions.RemoveRange(_context.RolePermissions.Where(x => x.Permission != null && moduleIds.Contains(x.Permission.ModuleId ?? 0)));

                foreach (var trustModule in trustViewModel.TrustModuleViewModelList.Where(x => x.IsHaveAccess))
                {
                    _context.TrustModules.Add(new TrustModule()
                    {
                        TrustId = trustViewModel.Id, ModuleId = trustModule.ModuleId
                    });
                }
                await _context.SaveChangesAsync();
            }
        }