|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ArieTimmerman\Laravel\SCIMServer\Attribute; |
| 4 | + |
| 5 | +use ArieTimmerman\Laravel\SCIMServer\Exceptions\SCIMException; |
| 6 | +use ArieTimmerman\Laravel\SCIMServer\Parser\Path; |
| 7 | +use Illuminate\Database\Eloquent\Builder; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | + |
| 11 | +class JSONCollection extends MutableCollection |
| 12 | +{ |
| 13 | + public function add($value, Model &$object) |
| 14 | + { |
| 15 | + foreach ($value as $v) { |
| 16 | + if (collect($object->{$this->attribute})->contains( |
| 17 | + fn ($item) => collect($item)->diffAssoc($v)->isEmpty() |
| 18 | + )) { |
| 19 | + throw new SCIMException('Value already exists', 400); |
| 20 | + } |
| 21 | + } |
| 22 | + $object->{$this->attribute} = collect($object->{$this->attribute})->merge($value); |
| 23 | + } |
| 24 | + |
| 25 | + public function replace($value, Model &$object, ?Path $path = null) |
| 26 | + { |
| 27 | + $object->{$this->attribute} = $value; |
| 28 | + } |
| 29 | + |
| 30 | + public function doRead(&$object, $attributes = []) |
| 31 | + { |
| 32 | + return $object->{$this->attribute}?->values()->all(); |
| 33 | + } |
| 34 | + |
| 35 | + public function remove($value, Model &$object, Path $path = null) |
| 36 | + { |
| 37 | + if ($path?->getValuePathFilter()?->getComparisonExpression() != null) { |
| 38 | + $attributes = $path?->getValuePathFilter()?->getComparisonExpression()?->attributePath?->attributeNames; |
| 39 | + $operator = $path?->getValuePathFilter()?->getComparisonExpression()?->operator; |
| 40 | + $compareValue = $path?->getValuePathFilter()?->getComparisonExpression()?->compareValue; |
| 41 | + |
| 42 | + if ($value != null) { |
| 43 | + throw new SCIMException('Non-null value is currently not supported for remove operation with filter'); |
| 44 | + } |
| 45 | + |
| 46 | + if (count($attributes) != 1) { |
| 47 | + throw new SCIMException('Only one attribute is currently supported for remove operation with filter'); |
| 48 | + } |
| 49 | + |
| 50 | + $object->{$this->attribute} = collect($object->{$this->attribute})->filter(function ($item) use ($attributes, $operator, $compareValue) { |
| 51 | + // check operator eq and ne |
| 52 | + if ($operator == 'eq') { |
| 53 | + return !(isset($item[$attributes[0]]) && $item[$attributes[0]] == $compareValue); |
| 54 | + } elseif ($operator == 'ne') { |
| 55 | + return !(!isset($item[$attributes[0]]) || $item[$attributes[0]] != $compareValue); |
| 56 | + } else { |
| 57 | + throw new SCIMException('Unsupported operator for remove operation with filter'); |
| 58 | + } |
| 59 | + })->values()->all(); |
| 60 | + } else { |
| 61 | + foreach ($value as $v) { |
| 62 | + $object->{$this->attribute} = collect($object->{$this->attribute})->filter(function ($item) use ($v) { |
| 63 | + return !collect($item)->diffAssoc($v)->isEmpty(); |
| 64 | + })->values()->all(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function applyComparison(Builder &$query, Path $path, Path $parentAttribute = null) |
| 70 | + { |
| 71 | + $fieldName = 'value'; |
| 72 | + |
| 73 | + if ($path != null && !empty($path->getAttributePathAttributes())) { |
| 74 | + $fieldName = $path->getAttributePathAttributes()[0]; |
| 75 | + } |
| 76 | + |
| 77 | + $operator = $path->node->operator; |
| 78 | + $value = $path->node->compareValue; |
| 79 | + |
| 80 | + $exists = false; |
| 81 | + |
| 82 | + foreach ($this->subAttributes as $subAttribute) { |
| 83 | + if ($subAttribute->name == $fieldName) { |
| 84 | + $exists = true; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!$exists) { |
| 90 | + throw new SCIMException('No attribute found with name ' . $path->getAttributePathAttributes()[0]); |
| 91 | + } |
| 92 | + |
| 93 | + // check if engine is postgres |
| 94 | + if (DB::getConfig("driver") == 'pgsql') { |
| 95 | + $baseQuery = sprintf("EXISTS ( |
| 96 | + SELECT 1 |
| 97 | + FROM json_array_elements(%s) elem |
| 98 | + WHERE elem ->> '%s' LIKE ? |
| 99 | + )", $this->attribute, $fieldName); |
| 100 | + } elseif (DB::getConfig("driver") == 'sqlite') { |
| 101 | + $baseQuery = sprintf("EXISTS ( |
| 102 | + SELECT 1 |
| 103 | + FROM json_each(%s) AS elem |
| 104 | + WHERE json_extract(elem.value, '$.%s') LIKE ? |
| 105 | + )", $this->attribute, $fieldName); |
| 106 | + } else { |
| 107 | + throw new SCIMException('Unsupported database engine'); |
| 108 | + } |
| 109 | + |
| 110 | + switch ($operator) { |
| 111 | + case "eq": |
| 112 | + $query->whereRaw($baseQuery, [addcslashes($value, '%_')]); |
| 113 | + break; |
| 114 | + case "ne": |
| 115 | + if (DB::getConfig("driver") == 'pgsql') { |
| 116 | + $baseQuery = sprintf("EXISTS ( |
| 117 | + SELECT 1 |
| 118 | + FROM json_array_elements(%s) elem |
| 119 | + WHERE elem ->> '%s' NOT LIKE ? |
| 120 | + )", $this->attribute, $fieldName); |
| 121 | + } elseif (DB::getConfig("driver") == 'sqlite') { |
| 122 | + $baseQuery = sprintf("EXISTS ( |
| 123 | + SELECT 1 |
| 124 | + FROM json_each(%s) AS elem |
| 125 | + WHERE json_extract(elem.value, '$.%s') NOT LIKE ? |
| 126 | + )", $this->attribute, $fieldName); |
| 127 | + } else { |
| 128 | + throw new SCIMException('Unsupported database engine'); |
| 129 | + } |
| 130 | + $query->whereRaw($baseQuery, [addcslashes($value, '%_')])->orWhereNull($this->attribute); |
| 131 | + break; |
| 132 | + case "co": |
| 133 | + $query->whereRaw($baseQuery, ['%' . addcslashes($value, '%_') . "%"]); |
| 134 | + break; |
| 135 | + case "sw": |
| 136 | + // $query->where($jsonAttribute, 'like', addcslashes($value, '%_') . '%'); |
| 137 | + $query->whereRaw($baseQuery, [addcslashes($value, '%_') . "%"]); |
| 138 | + break; |
| 139 | + case "ew": |
| 140 | + $query->whereRaw($baseQuery, ['%' . addcslashes($value, '%_')]); |
| 141 | + break; |
| 142 | + case "pr": |
| 143 | + $query->whereNotNull($this->attribute); |
| 144 | + break; |
| 145 | + case "gt": |
| 146 | + case "ge": |
| 147 | + case "lt": |
| 148 | + case "le": |
| 149 | + throw new SCIMException("This operator is not supported for this field: " . $operator); |
| 150 | + break; |
| 151 | + default: |
| 152 | + throw new SCIMException("Unknown operator " . $operator); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments