Пример #1
0
        public async Task <IActionResult> OnPostSaveAsync()
        {
            if (!ModelState.IsValid)
            {
                return(ModelState.ToJsonResult());
            }

            var entity = await _db.Permissions
                         .Where(x => x.Id == this.Id)
                         .SingleOrDefaultAsync();

            if (entity == null)
            {
                entity = new SystemPermission {
                    Id = this.Id ?? Guid.NewGuid(),

                    PermissionType = this.Data.PermissionType,

                    CreatedBy   = this.User.Identity.Name,
                    CreatedDate = DateTime.Now,
                };

                _db.Add(entity);
            }

            entity.Name = this.Data.Name;
            entity.Code = this.Data.Code;

            entity.UpdatedBy   = this.User.Identity.Name;
            entity.UpdatedDate = DateTime.Now;

            await _db.SaveChangesAsync();

            return(new SuccessResult());
        }
        public async Task <IActionResult> OnPostSaveAsync()
        {
            var rolesInUser = await _db.UsersInRoles
                              .Where(x => x.UserId == this.UserId)
                              .ToListAsync();

            // Add the new added role for the current user
            if (RoleInUserArray.Any())
            {
                foreach (var item in RoleInUserArray)
                {
                    if (!rolesInUser.Any(x => x.RoleId == item))
                    {
                        _db.Add(new UsersInRole {
                            RoleId = item,
                            UserId = this.UserId
                        });
                    }
                }
            }

            // Detach the removed role
            foreach (var item in rolesInUser)
            {
                if (!RoleInUserArray.Contains(item.RoleId))
                {
                    _db.Remove(item);
                }
            }

            await _db.SaveChangesAsync();

            return(new SuccessResult());
        }