laravel-nami-api/src/NamiUser.php

75 lines
1.7 KiB
PHP
Raw Normal View History

2020-06-27 23:45:49 +02:00
<?php
namespace Zoomyboy\LaravelNami;
use Illuminate\Contracts\Auth\Authenticatable;
2021-06-19 00:44:56 +02:00
use Illuminate\Database\Eloquent\Model;
2021-06-21 22:37:23 +02:00
use Cache;
2020-06-27 23:45:49 +02:00
2021-06-21 22:37:23 +02:00
class NamiUser implements Authenticatable {
2020-06-27 23:45:49 +02:00
2021-06-18 19:29:21 +02:00
public $mglnr;
2021-06-18 23:26:21 +02:00
public $password;
2020-06-27 23:45:49 +02:00
2021-06-21 22:37:23 +02:00
public function __construct($attributes) {
$this->mglnr = $attributes['mglnr'];
$this->password = $attributes['password'];
}
public static function fromPayload($payload) {
$user = new static([
'mglnr' => data_get($payload, 'credentials.mglnr'),
'password' => data_get($payload, 'credentials.password'),
]);
return $user;
2020-06-27 23:45:49 +02:00
}
2021-06-19 00:44:56 +02:00
public function api() {
return Nami::login($this->mglnr, $this->password);
2020-06-27 23:45:49 +02:00
}
public function getNamiGroupId() {
2021-06-21 23:50:28 +02:00
return $this->api()->findNr($this->mglnr)->group_id;
2020-06-27 23:45:49 +02:00
}
public function getAuthIdentifierName() {
return 'mglnr';
}
2021-06-21 22:37:23 +02:00
public function getMglnr() {
return $this->mglnr;
2021-06-19 00:44:56 +02:00
}
2021-06-21 22:37:23 +02:00
public function getFirstname() {
return Cache::remember('member-'.$this->mglnr.'-firstname', 3600, function() {
return $this->api()->findNr($this->mglnr)->firstname;
});
}
public function getLastname() {
return Cache::remember('member-'.$this->mglnr.'-lastname', 3600, function() {
return $this->api()->findNr($this->mglnr)->lastname;
});
2021-06-19 00:44:56 +02:00
}
2020-06-27 23:45:49 +02:00
public function getAuthIdentifier() {
return $this->{$this->getAuthIdentifierName()}.'-'.$this->groupid;
}
public function getAuthPassword() {
return null;
}
public function getRememberToken() {
return null;
}
public function setRememberToken($value) {}
public function getRememberTokenName() {
return null;
}
2021-06-21 22:37:23 +02:00
2020-06-27 23:45:49 +02:00
}