Add searching

This commit is contained in:
philipp lang 2021-06-19 00:44:56 +02:00
parent df10368f24
commit a9840f9de4
5 changed files with 153 additions and 7 deletions

View File

@ -32,6 +32,27 @@ class Api {
return $this; return $this;
} }
public function findNr($nr) {
return $this->search(['mitgliedsNummber' => $nr]);
}
public function search($payload) {
$url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload)).'&page=1&start=0&limit=10';
$response = $this->http()->get($url);
if ($response->json()['success'] === true) {
if (!count($response->json()['data'])) {
return null;
}
$data = collect($response->json()['data'][0])->mapWithKeys(function($value, $key) {
return [ str_replace('entries_', '', $key) => $value ];
});
return Member::fromNami($data);
}
}
protected function loggedInAlready(): bool { protected function loggedInAlready(): bool {
return $this->loggedIn !== null; return $this->loggedIn !== null;
} }

View File

@ -47,14 +47,13 @@ class NamiGuard {
return; return;
} }
return NamiUser::fromCredentials($cache['credentials']); return new NamiUser($cache);
} }
public function attempt(array $credentials = [], $remember = false) { public function attempt(array $credentials = [], $remember = false) {
$api = Nami::login($credentials['mglnr'], $credentials['password']); $api = Nami::login($credentials['mglnr'], $credentials['password']);
$payload = [ $payload = [
'cookie' => $api->cookie->toArray(),
'credentials' => $credentials 'credentials' => $credentials
]; ];
@ -77,7 +76,7 @@ class NamiGuard {
} }
private function resolveCache() { private function resolveCache() {
return $this->cache->get($this->session->get($this->getName())); return $this->cache->get('namiauth-'.$this->session->get($this->getName()));
} }
private function newCacheKey() { private function newCacheKey() {

View File

@ -3,8 +3,9 @@
namespace Zoomyboy\LaravelNami; namespace Zoomyboy\LaravelNami;
use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class NamiUser implements Authenticatable { class NamiUser extends Model implements Authenticatable {
public $mglnr; public $mglnr;
public $password; public $password;
@ -12,11 +13,10 @@ class NamiUser implements Authenticatable {
public function __construct($payload) { public function __construct($payload) {
$this->mglnr = data_get($payload, 'credentials.mglnr'); $this->mglnr = data_get($payload, 'credentials.mglnr');
$this->password = data_get($payload, 'credentials.password'); $this->password = data_get($payload, 'credentials.password');
$this->cookie = data_get($payload, 'cookie');
} }
public function getNamiApi() { public function api() {
return $this->attemptNamiLogin(cache('member.'.$this->mglnr)['credentials']['password']); return Nami::login($this->mglnr, $this->password);
} }
public function getNamiGroupId() { public function getNamiGroupId() {
@ -27,6 +27,14 @@ class NamiUser implements Authenticatable {
return 'mglnr'; return 'mglnr';
} }
public function getFirstnameAttribute() {
return $this->api()->findNr($this->mglnr)->vorname;
}
public function getLastnameAttribute() {
return $this->api()->findNr($this->mglnr)->nachname;
}
public function getAuthIdentifier() { public function getAuthIdentifier() {
return $this->{$this->getAuthIdentifierName()}.'-'.$this->groupid; return $this->{$this->getAuthIdentifierName()}.'-'.$this->groupid;
} }

68
tests/Unit/SearchTest.php Normal file
View File

@ -0,0 +1,68 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\Tests\TestCase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Config;
use Zoomyboy\LaravelNami\NamiServiceProvider;
use Zoomyboy\LaravelNami\LoginException;
use Zoomyboy\LaravelNami\Group;
use Zoomyboy\LaravelNami\Member;
use Illuminate\Database\Eloquent\Model;
class SearchTest extends TestCase
{
public $groupsResponse = '{"success":true,"data":[{"descriptor":"Group","name":"","representedClass":"de.iconcept.nami.entity.org.Gruppierung","id":103}],"responseType":"OK"}';
public $unauthorizedResponse = '{"success":false,"data":null,"responseType":"EXCEPTION","message":"Access denied - no right for requested operation","title":"Exception"}';
public $attributes = [
[
'firstname' => 'Max',
'lastname' => 'Nach1',
'group_id' => 103,
'nickname' => 'spitz1',
'gender_id' => 17,
'id' => 16,
], [
'firstname' => 'Jane',
'lastname' => 'Nach2',
'nickname' => null,
'group_id' => 103,
'gender_id' => null,
'id' => 17,
]
];
public function dataProvider() {
return [
'firstname' => ['vorname', ['Max', 'Jane']],
];
}
public function test_find_a_member_by_mglnr() {
Http::fake(array_merge($this->login(), [
$this->url(['mitgliedsNummber' => 150]) => Http::response($this->fakeJson('searchResponse.json'), 200),
]));
$this->setCredentials();
Nami::login();
$member = Nami::findNr(150);
$this->assertEquals('Philipp', $member->firstname);
Http::assertSent(function($request) {
return $request->url() == $this->url(['mitgliedsNummber' => 150])
&& $request->method() == 'GET';
});
Http::assertSentCount(3);
}
private function url($payload) {
$payload = rawurlencode(json_encode($payload));
return "https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues={$payload}&page=1&start=0&limit=10";
}
}

View File

@ -0,0 +1,50 @@
{
"success": true,
"data": [
{
"entries_ersteTaetigkeitId": null,
"entries_genericField1": "",
"entries_version": 78,
"entries_telefon3": "",
"entries_telefon2": "+49 176 70342420",
"entries_telefon1": "+49 212 1399418",
"descriptor": "Lang, Philipp",
"entries_id": 89418,
"entries_staatsangehoerigkeit": "deutsch",
"representedClass": "de.iconcept.nami.entity.mitglied.Mitglied",
"entries_rover": "",
"entries_pfadfinder": "",
"entries_mitgliedsNummer": 90166,
"entries_wiederverwendenFlag": false,
"entries_ersteUntergliederungId": null,
"entries_rowCssClass": "",
"entries_gruppierung": "Solingen-Wald, Silva 100105",
"entries_vorname": "Philipp",
"id": 89418,
"entries_woelfling": "",
"entries_beitragsarten": "",
"entries_stufe": "",
"entries_email": "pille@stamm-silva.de",
"entries_konfession": "",
"entries_gruppierungId": "100105",
"entries_fixBeitrag": "",
"entries_emailVertretungsberechtigter": "",
"entries_lastUpdated": "2021-04-11 19:40:56",
"entries_status": "Aktiv",
"entries_jungpfadfinder": "",
"entries_mglType": "Mitglied",
"entries_kontoverbindung": "",
"entries_geschlecht": "m\u00e4nnlich",
"entries_spitzname": "",
"entries_geburtsDatum": "1991-06-20 00:00:00",
"entries_staatangehoerigkeitText": "",
"entries_nachname": "Lang",
"entries_eintrittsdatum": "2005-05-01 00:00:00",
"entries_austrittsDatum": "",
"entries_genericField2": "",
"entries_telefax": ""
}
],
"responseType": "OK",
"totalEntries": 1
}