-
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 7 replies
-
Hello @iRedds, I would be happy to know your opinion on this matter. |
Beta Was this translation helpful? Give feedback.
-
@datamweb I have another issue with
So this function gave me a array to use for select in form to choose from users which has an access to provided item. public function getUsersFromGroupOptions(string $item, string $group): array
{
$options = [];
$cacheName = "{$item}_options_user_group_{$group}";
if (! ($options = cache($cacheName))) {
$options = array_reduce(
$this->select('users.id, users.first_name, users.last_name, users.username')
->join('auth_groups_users as agu', 'agu.user_id = users.id', 'left')
->where('agu.group', $group)
->findAll(),
static function ($result, $user) {
$name = implode(' ', [$user->first_name, $user->last_name]);
$result[$user->id] = (ctype_space($name) ? '' : $name) ?: $user->username;
return $result;
},
[],
);
cache()
->save($cacheName, $options, DECADE);
}
return $options;
} but I would like to delete cache if there is some update of the users groups or deletion. So I added /**
*
* @var string[]
*/
protected $beforeUpdate = ['clearCache'];
/**
* @var string[]
*/
protected $beforeDelete = ['clearCache']; and clearCache function /**
* @param mixed[] $data
*
* @return mixed[]
*/
public function clearCache(array $data): array
{
$user = $this->getUserById(is_array($data['id']) ? $data['id'][0] : $data['id']);
// delete cache for customer options
cache()->deleteMatching('*options_user_group*');
if ($user !== null) {
// delete model requests cache, name etc.
cache()->deleteMatching("user#{$user->id}*");
}
return $data;
} But because of the So now I'm thinking how to delete cache only if it's needed and not all the time when |
Beta Was this translation helpful? Give feedback.
-
There's no way to not do updated if we use the model's functions. We might be able to do a raw query to handle that. Unsure if that's a good idea or not. You can turn off the last active date recording, though, if you don't need that for your site. If public bool $recordActiveDate = false; |
Beta Was this translation helpful? Give feedback.
-
I agree with @jozefrebjak, the |
Beta Was this translation helpful? Give feedback.
-
After thinking a bit more I agree. Anyone want to submit a PR for that? |
Beta Was this translation helpful? Give feedback.
-
This issue was fixed by #392 |
Beta Was this translation helpful? Give feedback.
-
Create your own Line 341 in a3ea677 |
Beta Was this translation helpful? Give feedback.
Create your own
UserModel
and override theupdateActiveDate()
method:https://github.com/codeigniter4/shield/pull/392/files#diff-459f49e48ae4f20464cf08fd3256652536435610f4f7db89305fe7269e539ac9
and set your UserModel to
$userProvider
:shield/src/Config/Auth.php
Line 341 in a3ea677