I want to get the model information of a user who has logged in with AuthenticationPlugin

Hi.こんにちわ。

I’m using the AuthenticationPlugin for CakePHP4. I use a routing prefix to separate the admin screen and the normal screen, but the normal user can come and go between the admin screen and the normal screen freely.
So we want to get the model used and redirect to the 404 screen when the model is not used in the admin screen.

It’s a long story. Let’s get to the point.

This will give you the users email address you are logged in to.

$this->Authentication->getIdentity()->mail

Now you can get the users name you are logged in with.

$this->Authentication->getIdentity()->name

So how do I get information about the model I’m using as a logged in user?

???

.
.
.

This is when output by “dd($this->Authentication->getIdentity());”.

Authentication\Identity {#116 ▼
 ....
 #_registryAlias: "Users"
      id: 3
      name: "test_user_name"
      mail: "test@test.co.jp"
      password: "$2y$10$POv4UXil1mR/cWWBDlNkTu2mw2d8c7GTNdAOZRFYi03T8tk8m8O4S"
      sso: 0
      provider_cd: 0
      provider_id: null
      points: 0
      created: Cake\I18n\FrozenTime @1599105798 {#119 ▶}
      updated: Cake\I18n\FrozenTime @1599105798 {#120 ▶}
      [new]: false
      [accessible]: array:9 [▶]
      [dirty]: []
      [original]: []
      [virtual]: []
      [hasErrors]: false
      [errors]: []
      [invalid]: []
      [repository]: "Users"

How do I extract the value of [repository] again?

Plz Helpe Me!! :face_with_head_bandage: :face_with_head_bandage: :face_with_head_bandage:

I believe the Identity object has a getOriginalData() method that will return the entity that underlies the Identity. On that Entity your should be able to getSource() to get the alias of the model that made the entity.

1 Like

Thank you! Mr.dreamingmind.
dreamingmindさん、ありがとうございます。本当に助かりました。感謝してもしきれないです。僕はdreamingmindのことが大好きです :laughing:

This code gave me the results I wanted.
$this->Authentication->getIdentity()->getOriginalData()->getSource()
Thank you so much.