From a9840f9de43e654cc19806b2d35bf7981720a47a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 19 Jun 2021 00:44:56 +0200 Subject: [PATCH] Add searching --- src/Api.php | 21 ++++++++++ src/Authentication/NamiGuard.php | 5 +-- src/NamiUser.php | 16 ++++++-- tests/Unit/SearchTest.php | 68 ++++++++++++++++++++++++++++++++ tests/json/searchResponse.json | 50 +++++++++++++++++++++++ 5 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 tests/Unit/SearchTest.php create mode 100644 tests/json/searchResponse.json diff --git a/src/Api.php b/src/Api.php index 6b23d1a..911b332 100644 --- a/src/Api.php +++ b/src/Api.php @@ -32,6 +32,27 @@ class Api { 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 { return $this->loggedIn !== null; } diff --git a/src/Authentication/NamiGuard.php b/src/Authentication/NamiGuard.php index 6d7f28f..1a005c4 100644 --- a/src/Authentication/NamiGuard.php +++ b/src/Authentication/NamiGuard.php @@ -47,14 +47,13 @@ class NamiGuard { return; } - return NamiUser::fromCredentials($cache['credentials']); + return new NamiUser($cache); } public function attempt(array $credentials = [], $remember = false) { $api = Nami::login($credentials['mglnr'], $credentials['password']); $payload = [ - 'cookie' => $api->cookie->toArray(), 'credentials' => $credentials ]; @@ -77,7 +76,7 @@ class NamiGuard { } 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() { diff --git a/src/NamiUser.php b/src/NamiUser.php index 1315e4c..2f2eda8 100644 --- a/src/NamiUser.php +++ b/src/NamiUser.php @@ -3,8 +3,9 @@ namespace Zoomyboy\LaravelNami; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\Eloquent\Model; -class NamiUser implements Authenticatable { +class NamiUser extends Model implements Authenticatable { public $mglnr; public $password; @@ -12,11 +13,10 @@ class NamiUser implements Authenticatable { public function __construct($payload) { $this->mglnr = data_get($payload, 'credentials.mglnr'); $this->password = data_get($payload, 'credentials.password'); - $this->cookie = data_get($payload, 'cookie'); } - public function getNamiApi() { - return $this->attemptNamiLogin(cache('member.'.$this->mglnr)['credentials']['password']); + public function api() { + return Nami::login($this->mglnr, $this->password); } public function getNamiGroupId() { @@ -27,6 +27,14 @@ class NamiUser implements Authenticatable { 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() { return $this->{$this->getAuthIdentifierName()}.'-'.$this->groupid; } diff --git a/tests/Unit/SearchTest.php b/tests/Unit/SearchTest.php new file mode 100644 index 0000000..5c607fb --- /dev/null +++ b/tests/Unit/SearchTest.php @@ -0,0 +1,68 @@ + '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"; + } + +} diff --git a/tests/json/searchResponse.json b/tests/json/searchResponse.json new file mode 100644 index 0000000..744bd57 --- /dev/null +++ b/tests/json/searchResponse.json @@ -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 +}