Skip to content

Commit 67530bb

Browse files
authored
[MixerApi/Core] - Adds support for Entity accessible and hidden to ModelProperty (#98)
* Adds support for Entity accessible and hidden to ModelProperty * Suggest jwt-auth and readme updates
1 parent 2a01ae8 commit 67530bb

7 files changed

Lines changed: 91 additions & 12 deletions

File tree

plugins/core/src/Model/Model.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ private function assignProperties(): void
5757
});
5858

5959
foreach ($columns as $columnName) {
60-
$modelProperty = (new ModelPropertyFactory($this->schema, $this->table, $columnName))->create();
60+
$modelProperty = (new ModelPropertyFactory($this->schema, $this->table, $columnName, $this->entity))
61+
->create();
6162

6263
$this->properties[$columnName] = $modelProperty;
6364
}

plugins/core/src/Model/ModelProperty.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,35 @@
1212
class ModelProperty
1313
{
1414
/**
15-
* @var string
15+
* @var string Column (field) name from the database definition
1616
*/
1717
private string $name = '';
1818

1919
/**
20-
* @var string
20+
* @var string Data type from the database definition
2121
*/
2222
private string $type = '';
2323

2424
/**
25-
* @var string
25+
* @var string Default value from the database definition
2626
*/
2727
private string $default = '';
2828

2929
/**
30-
* @var bool
30+
* @var bool Is this column a primary key (using db definition)
3131
*/
3232
private bool $isPrimaryKey = false;
3333

34+
/**
35+
* @var bool Should this field be hidden from responses (using CakePHP Entity $_hidden)
36+
*/
37+
private bool $isHidden = false;
38+
39+
/**
40+
* @var bool The mass assignment of this field (using CakePHP Entity $_accessible)
41+
*/
42+
private bool $isAccessible = false;
43+
3444
/**
3545
* @var \Cake\Validation\ValidationSet|null
3646
*/
@@ -112,6 +122,44 @@ public function setIsPrimaryKey(bool $isPrimaryKey)
112122
return $this;
113123
}
114124

125+
/**
126+
* @return bool
127+
*/
128+
public function isHidden(): bool
129+
{
130+
return $this->isHidden;
131+
}
132+
133+
/**
134+
* @param bool $isHidden Is this property readable in responses
135+
* @return $this
136+
*/
137+
public function setIsHidden(bool $isHidden)
138+
{
139+
$this->isHidden = $isHidden;
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* @return bool
146+
*/
147+
public function isAccessible(): bool
148+
{
149+
return $this->isAccessible;
150+
}
151+
152+
/**
153+
* @param bool $isAccessible Is this property writable in requests
154+
* @return $this
155+
*/
156+
public function setIsAccessible(bool $isAccessible)
157+
{
158+
$this->isAccessible = $isAccessible;
159+
160+
return $this;
161+
}
162+
115163
/**
116164
* @return \Cake\Validation\ValidationSet|null
117165
*/

plugins/core/src/Model/ModelPropertyFactory.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MixerApi\Core\Model;
55

66
use Cake\Database\Schema\TableSchema;
7+
use Cake\Datasource\EntityInterface;
78
use Cake\ORM\Table;
89
use Cake\Validation\Validator;
910

@@ -18,9 +19,14 @@ class ModelPropertyFactory
1819
* @param \Cake\Database\Schema\TableSchema $schema cake TableSchema instance
1920
* @param \Cake\ORM\Table $table cake Table instance
2021
* @param string $columnName the tables column name
22+
* @param \Cake\Datasource\EntityInterface $entity EntityInterface
2123
*/
22-
public function __construct(private TableSchema $schema, private Table $table, private string $columnName)
23-
{
24+
public function __construct(
25+
private TableSchema $schema,
26+
private Table $table,
27+
private string $columnName,
28+
private EntityInterface $entity
29+
) {
2430
}
2531

2632
/**
@@ -36,6 +42,8 @@ public function create()
3642
->setType($this->schema->getColumnType($this->columnName))
3743
->setDefault((string)$default)
3844
->setIsPrimaryKey($this->isPrimaryKey($vars, $this->columnName))
45+
->setIsHidden(in_array($this->columnName, $this->entity->getHidden()))
46+
->setIsAccessible($this->isAccessible())
3947
->setValidationSet($this->table->validationDefault(new Validator())->field($this->columnName));
4048
}
4149

@@ -55,4 +63,24 @@ private function isPrimaryKey(array $schemaDebugInfo, string $columnName): bool
5563

5664
return in_array($columnName, $schemaDebugInfo['constraints']['primary']['columns']);
5765
}
66+
67+
/**
68+
* Returns accessibility of the property.
69+
*
70+
* @link https://book.cakephp.org/4/en/orm/entities.html#mass-assignment
71+
* @return bool
72+
*/
73+
private function isAccessible(): bool
74+
{
75+
$accessible = $this->entity->getAccessible();
76+
if (isset($accessible[$this->columnName]) && is_bool($accessible[$this->columnName])) {
77+
return $accessible[$this->columnName];
78+
}
79+
80+
if (isset($accessible['*']) && is_bool($accessible['*'])) {
81+
return $accessible['*'];
82+
}
83+
84+
return false;
85+
}
5886
}

plugins/core/tests/TestCase/Model/ModelTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function test_model_property(): void
4545

4646
$this->assertTrue($model->getProperty('id')->isPrimaryKey());
4747
$this->assertEmpty($model->getProperty('first_name')->getDefault());
48+
$this->assertFalse($model->getProperty('first_name')->isHidden());
49+
$this->assertTrue($model->getProperty('first_name')->isAccessible());
4850
$this->assertEquals('string', $model->getProperty('first_name')->getType());
4951
$this->assertEquals('first_name', $model->getProperty('first_name')->getName());
5052
$this->assertInstanceOf(ValidationSet::class, $model->getProperty('first_name')->getValidationSet());

plugins/core/tests/test_app/src/Model/Entity/Actor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class Actor extends Entity
2020
protected $_accessible = [
2121
'first_name' => true,
2222
'last_name' => true,
23-
'modified' => true,
24-
'film_actors' => true,
23+
'*' => false
2524
];
2625

2726
/**

plugins/jwt-auth/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ The `secret` string is required when using HMAC. The secret should not be commit
5454
characters long. You can generate a strong secret using a tool like openssl or gpg:
5555

5656
```console
57-
openssl rand -base64 32
57+
openssl rand -base64 32 | cut -c1-32
5858
```
5959

6060
```console
61-
gpg --gen-random 1 32 | base64
61+
gpg --gen-random 1 32 | base64 | cut -c1-32
6262
```
6363

6464
#### keys

plugins/mixerapi/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
],
3333
"suggest": {
3434
"cnizzardini/cakephp-preloader": "An OpCache preloader for php >= 7.4 cakephp projects",
35-
"friendsofcake/search": "Search provides a simple interface to create paginate-able filters for your project"
35+
"friendsofcake/search": "Search provides a simple interface to create paginate-able filters for your project",
36+
"mixerapi/jwt-auth": "For JWT Authentication"
3637
},
3738
"prefer-stable": true,
3839
"minimum_stability": "dev"

0 commit comments

Comments
 (0)