public async Task UpdatePermission(KorePermission permission)
        {
            var id       = Convert.ToInt32(permission.Id);
            var existing = await dbContext.Permissions.FirstOrDefaultAsync(x => x.Id == id);

            existing.Name        = permission.Name;
            existing.Category    = permission.Category;
            existing.Description = permission.Description;
            await dbContext.SaveChangesAsync();
        }
 public async Task InsertPermission(KorePermission permission)
 {
     dbContext.Permissions.Add(new Permission
     {
         TenantId    = permission.TenantId,
         Name        = permission.Name,
         Category    = permission.Category,
         Description = permission.Description
     });
     await dbContext.SaveChangesAsync();
 }
Exemple #3
0
        public MvcHtmlString PermissionsCheckBoxList(
            string name,
            IEnumerable <string> selectedPermissionIds,
            object labelHtmlAttributes    = null,
            object checkboxHtmlAttributes = null)
        {
            var membershipService   = EngineContext.Current.Resolve <IMembershipService>();
            var permissionProviders = EngineContext.Current.ResolveAll <IPermissionProvider>();
            var permissions         = permissionProviders.SelectMany(x => x.GetPermissions()).ToList();
            var workContext         = EngineContext.Current.Resolve <IWorkContext>();

            var allPermissions = AsyncHelper.RunSync(() => membershipService.GetAllPermissions(workContext.CurrentTenant.Id)).ToHashSet();
            var T = LocalizationUtilities.Resolve();

            #region First check if all permissions are in the DB

            foreach (var permission in permissions)
            {
                if (!allPermissions.Any(x => x.Name == permission.Name))
                {
                    var newPermission = new KorePermission
                    {
                        Name        = permission.Name,
                        Category    = string.IsNullOrEmpty(permission.Category) ? T(KoreWebLocalizableStrings.General.Miscellaneous) : permission.Category,
                        Description = permission.Description
                    };

                    newPermission.TenantId = workContext.CurrentTenant.Id;
                    membershipService.InsertPermission(newPermission);
                    allPermissions.Add(newPermission);
                }
            }

            #endregion First check if all permissions are in the DB

            var selectList = new List <ExtendedSelectListItem>();
            foreach (var categoryGroup in allPermissions.OrderBy(x => x.Category, new PermissionComparer(StringComparer.InvariantCultureIgnoreCase)).GroupBy(x => x.Category))
            {
                selectList.AddRange(categoryGroup.OrderBy(x => x.Description)
                                    .Select(permission => new ExtendedSelectListItem
                {
                    Category = permission.Category,
                    Text     = permission.Description,
                    Value    = permission.Id
                }));
            }

            return(html.CheckBoxList(
                       name,
                       selectList,
                       selectedPermissionIds,
                       labelHtmlAttributes: labelHtmlAttributes,
                       checkboxHtmlAttributes: checkboxHtmlAttributes));
        }
        public virtual async Task <IHttpActionResult> Post(KorePermission entity)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipPermissionsWrite))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            entity.TenantId = workContext.CurrentTenant.Id;
            await Service.InsertPermission(entity);

            return(Created(entity));
        }
        public virtual async Task <IHttpActionResult> Delete([FromODataUri] string key)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipPermissionsWrite))
            {
                return(Unauthorized());
            }

            KorePermission entity = await Service.GetPermissionById(key);

            if (entity == null)
            {
                return(NotFound());
            }

            await Service.DeletePermission(key);

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public virtual async Task <IHttpActionResult> Patch([FromODataUri] string key, Delta <KorePermission> patch)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipPermissionsWrite))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            KorePermission entity = await Service.GetPermissionById(key);

            if (entity == null)
            {
                return(NotFound());
            }

            patch.Patch(entity);

            try
            {
                await Service.UpdatePermission(entity);
            }
            catch (DbUpdateConcurrencyException x)
            {
                logger.Value.Error(x.Message, x);

                if (!EntityExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(entity));
        }