public Role AddOrUpdateRole(Role role)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            var sourceEntry = role.ToDataModel();

            using (var repository = _platformRepository())
			using(var changeTracker = GetChangeTracker(repository))
            {
				AddOrUpdatePermissions(repository, role.Permissions);

                var targetEntry = repository.Roles.Include(r => r.RolePermissions)
											.FirstOrDefault(r => r.Id == role.Id);
				if (targetEntry == null)
				{
					repository.Add(sourceEntry);
				}
				else
				{
					changeTracker.Attach(targetEntry);
					sourceEntry.Patch(targetEntry);
				}
                

                CommitChanges(repository);
            }

            var result = GetRole(sourceEntry.Id);
            return result;
        }
예제 #2
0
       public static Role ToCoreModel(this dataModel.RoleEntity source, IPermissionScopeService scopeService)
       {
           var result = new Role();
           result.InjectFrom(source);
 
          result.Permissions = source.RolePermissions.Select(rp => rp.ToCoreModel(scopeService)).ToArray();
           return result;
       }
        public static Role ToCoreModel(this RoleEntity source, bool fillPermissions)
        {
            var result = new Role
            {
                Id = source.Id,
                Name = source.Name,
                Description = source.Description,
            };

            if (fillPermissions && source.RolePermissions != null)
            {
                result.Permissions = source.RolePermissions.Select(rp => rp.Permission.ToCoreModel()).ToArray();
            }

            return result;
        }
 public IHttpActionResult UpdateRole(Role role)
 {
     var result = _roleService.AddOrUpdateRole(role);
     return Ok(result);
 }
        public Role AddOrUpdateRole(Role role)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            var sourceEntry = role.ToDataModel();

            using (var repository = _platformRepository())
			using(var changeTracker = GetChangeTracker(repository))
            {
			    var targetEntry = repository.GetRoleById(role.Id);

                //Create not exist permissions
                if(role.Permissions != null)
                {
                    var permissionIds = role.Permissions.Select(x => x.Id).ToArray();
                    var alreadyExistPermissionIds = repository.Permissions.Where(x => permissionIds.Contains(x.Id))
                                                            .Select(x => x.Id)
                                                            .ToArray();
                    var notExistPermissionIds = permissionIds.Except(alreadyExistPermissionIds).ToArray();
                    foreach(var notExistPermissionId in notExistPermissionIds)
                    {
                        var permission = role.Permissions.First(x => x.Id == notExistPermissionId).ToDataModel();
                        repository.Add(permission);
                    }
                }
                if (targetEntry == null)
				{
					repository.Add(sourceEntry);
				}
				else
				{
					changeTracker.Attach(targetEntry);
					sourceEntry.Patch(targetEntry);
				}
                CommitChanges(repository);
            }

            var result = GetRole(sourceEntry.Id);
            return result;
        }