Skip to content

Commit ff3a495

Browse files
authored
Merge branch 'codeigniter4:develop' into patch-1
2 parents f112e1d + 7e90fda commit ff3a495

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/Authorization/Traits/Authorizable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CodeIgniter\I18n\Time;
88
use CodeIgniter\Shield\Authorization\AuthorizationException;
9+
use CodeIgniter\Shield\Exceptions\LogicException;
910
use CodeIgniter\Shield\Models\GroupModel;
1011
use CodeIgniter\Shield\Models\PermissionModel;
1112

@@ -226,9 +227,18 @@ public function hasPermission(string $permission): bool
226227
/**
227228
* Checks user permissions and their group permissions
228229
* to see if the user has a specific permission.
230+
*
231+
* @param string $permission string consisting of a scope and action, like `users.create`
229232
*/
230233
public function can(string $permission): bool
231234
{
235+
if (strpos($permission, '.') === false) {
236+
throw new LogicException(
237+
'A permission must be a string consisting of a scope and action, like `users.create`.'
238+
. ' Invalid permission: ' . $permission
239+
);
240+
}
241+
232242
$this->populatePermissions();
233243

234244
$permission = strtolower($permission);

src/Controllers/RegisterController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,8 @@ public function registerAction(): RedirectResponse
7676
}
7777

7878
// Save the user
79-
$allowedPostFields = array_merge(
80-
setting('Auth.validFields'),
81-
setting('Auth.personalFields'),
82-
array_keys($rules),
83-
);
84-
$user = $this->getUserEntity();
79+
$allowedPostFields = array_keys($rules);
80+
$user = $this->getUserEntity();
8581
$user->fill($this->request->getPost($allowedPostFields));
8682

8783
// Workaround for email only registration/login

src/Models/UserModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ public function update($id = null, $data = null): bool
253253
/** @throws DataException */
254254
$result = parent::update($id, $data);
255255
} catch (DataException $e) {
256+
// When $data is an array.
257+
if ($this->tempUser === null) {
258+
throw $e;
259+
}
260+
256261
$messages = [
257262
lang('Database.emptyDataset', ['update']),
258263
];

tests/Authorization/AuthorizableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CodeIgniter\I18n\Time;
88
use CodeIgniter\Shield\Authorization\AuthorizationException;
9+
use CodeIgniter\Shield\Exceptions\LogicException;
910
use CodeIgniter\Shield\Models\UserModel;
1011
use CodeIgniter\Test\DatabaseTestTrait;
1112
use Locale;
@@ -299,6 +300,16 @@ public function testCanCascadesToGroupsWithWildcards(): void
299300
$this->assertTrue($this->user->can('admin.access'));
300301
}
301302

303+
public function testCanGetsInvalidPermission(): void
304+
{
305+
$this->expectException(LogicException::class);
306+
$this->expectExceptionMessage('Invalid permission: developer');
307+
308+
$this->user->addGroup('superadmin');
309+
310+
$this->assertTrue($this->user->can('developer'));
311+
}
312+
302313
/**
303314
* @see https://github.com/codeigniter4/shield/pull/238
304315
*/

tests/Unit/UserModelTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Unit;
66

7+
use CodeIgniter\Database\Exceptions\DataException;
78
use CodeIgniter\Shield\Entities\User;
89
use CodeIgniter\Shield\Exceptions\LogicException;
910
use CodeIgniter\Shield\Models\UserModel;
@@ -239,4 +240,18 @@ public function testUpdateUserObjectWithoutUserDataToUpdate(): void
239240
'secret' => 'bar@bar.com',
240241
]);
241242
}
243+
244+
/**
245+
* @see https://github.com/codeigniter4/shield/issues/471
246+
*/
247+
public function testSaveArrayNoDataToUpdate(): void
248+
{
249+
$this->expectException(DataException::class);
250+
$this->expectExceptionMessage('There is no data to update.');
251+
252+
$users = $this->createUserModel();
253+
$user = fake(UserModel::class);
254+
255+
$users->save(['id' => $user->id]);
256+
}
242257
}

0 commit comments

Comments
 (0)