IDP - User Management
* Added user registration process * Added user password reset process * Added user email verification proccess * update token id to return custom claims * update access token instrospection to return user custom claims * Migrated to Doctrine ORM ( from eloquent) * Added User CRUD * Added User Groups CRUD * Refactoring * Bug Fixing * added user registration oauth2 endpoint POST /api/v1/user-registration-requests payload * first_name ( required ) * last_name ( required) * email ( required ) * country ( optional ) scope user-registration ( private scope) Change-Id: I36e8cd4473ccad734565051442e2c6033b204f27
This commit is contained in:
parent
8abc01412f
commit
b52c932636
16
.env.example
16
.env.example
@ -11,12 +11,6 @@ DB_DATABASE=homestead
|
|||||||
DB_USERNAME=homestead
|
DB_USERNAME=homestead
|
||||||
DB_PASSWORD=secret
|
DB_PASSWORD=secret
|
||||||
|
|
||||||
SS_DB_DRIVER=mysql
|
|
||||||
SS_DB_HOST=localhost
|
|
||||||
SS_DB_DATABASE=homestead
|
|
||||||
SS_DB_USERNAME=homestead
|
|
||||||
SS_DB_PASSWORD=secret
|
|
||||||
|
|
||||||
DB_USE_SSL=false
|
DB_USE_SSL=false
|
||||||
DB_MYSQL_ATTR_SSL_CA=
|
DB_MYSQL_ATTR_SSL_CA=
|
||||||
DB_MYSQL_ATTR_SSL_KEY=
|
DB_MYSQL_ATTR_SSL_KEY=
|
||||||
@ -28,13 +22,15 @@ REDIS_PORT=port
|
|||||||
REDIS_DB=0
|
REDIS_DB=0
|
||||||
REDIS_PASSWORD=
|
REDIS_PASSWORD=
|
||||||
|
|
||||||
CACHE_DRIVER=file
|
CACHE_DRIVER=redis
|
||||||
|
|
||||||
SESSION_DRIVER=redis
|
SESSION_DRIVER=redis
|
||||||
SESSION_COOKIE_DOMAIN=
|
SESSION_COOKIE_DOMAIN=
|
||||||
SESSION_COOKIE_SECURE=false
|
SESSION_COOKIE_SECURE=false
|
||||||
|
|
||||||
QUEUE_DRIVER=sync
|
QUEUE_DRIVER=database
|
||||||
|
QUEUE_CONN=
|
||||||
|
QUEUE_DATABASE=
|
||||||
|
|
||||||
MAIL_DRIVER=sendgrid
|
MAIL_DRIVER=sendgrid
|
||||||
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
|
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
|
||||||
@ -65,4 +61,6 @@ RECAPTCHA_PUBLIC_KEY=
|
|||||||
RECAPTCHA_PRIVATE_KEY=
|
RECAPTCHA_PRIVATE_KEY=
|
||||||
|
|
||||||
BANNING_ENABLE=
|
BANNING_ENABLE=
|
||||||
SUPPORT_EMAIL=
|
SUPPORT_EMAIL=
|
||||||
|
MAIL_FROM_EMAIL="noreply@openstack.org"
|
||||||
|
MAIL_FROM_NAME="noreply@openstack.org"
|
7
.gitignore
vendored
7
.gitignore
vendored
@ -1,6 +1,5 @@
|
|||||||
/vendor
|
/vendor
|
||||||
composer.phar
|
composer.phar
|
||||||
composer.lock
|
|
||||||
.idea/*
|
.idea/*
|
||||||
.tox
|
.tox
|
||||||
AUTHORS
|
AUTHORS
|
||||||
@ -15,7 +14,7 @@ Homestead.yaml
|
|||||||
Homestead.json
|
Homestead.json
|
||||||
.env
|
.env
|
||||||
.env.testing
|
.env.testing
|
||||||
|
storage/proxies
|
||||||
/public/assets/jquery-cookie/
|
/public/assets/jquery-cookie/
|
||||||
/public/assets/crypto-js/
|
/public/assets/crypto-js/
|
||||||
/public/assets/bootstrap-tagsinput/
|
/public/assets/bootstrap-tagsinput/
|
||||||
@ -33,4 +32,6 @@ public/assets/css/index.css
|
|||||||
/public/assets/sweetalert2/
|
/public/assets/sweetalert2/
|
||||||
/public/assets/urijs
|
/public/assets/urijs
|
||||||
/public/assets/uri.js
|
/public/assets/uri.js
|
||||||
_intellij_phpdebug_validator.php
|
_intellij_phpdebug_validator.php
|
||||||
|
/public/assets/chosen-js
|
||||||
|
/public/assets/moment
|
88
app/Console/Commands/CreateSuperAdmin.php
Normal file
88
app/Console/Commands/CreateSuperAdmin.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php namespace App\Console\Commands;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
use Auth\Group;
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use LaravelDoctrine\ORM\Facades\EntityManager;
|
||||||
|
/**
|
||||||
|
* Class CreateSuperAdmin
|
||||||
|
* @package App\Console\Commands
|
||||||
|
*/
|
||||||
|
class CreateSuperAdmin extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'idp:create-super-admin {email} {password}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Create Super Admin User';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
$email = trim($this->argument('email'));
|
||||||
|
$password = trim($this->argument('password'));
|
||||||
|
|
||||||
|
$user = EntityManager::getRepository(User::class)->findOneBy(['email' =>$email]);
|
||||||
|
if(!is_null($user)) {
|
||||||
|
$this->error('email already exists on db !');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
$user->setEmail($email);
|
||||||
|
$user->verifyEmail();
|
||||||
|
$user->setPassword($password);
|
||||||
|
EntityManager::persist($user);
|
||||||
|
EntityManager::flush();
|
||||||
|
|
||||||
|
$group = EntityManager::getRepository(Group::class)->findOneBy(['name' => 'super admins']);
|
||||||
|
if(is_null($group)){
|
||||||
|
$group = new Group();
|
||||||
|
$group->setName('super admins');
|
||||||
|
$group->setSlug('super-admins');
|
||||||
|
$group->setDefault(false);
|
||||||
|
$group->setActive(true);
|
||||||
|
EntityManager::persist($group);
|
||||||
|
EntityManager::flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->addToGroup($group);
|
||||||
|
EntityManager::persist($user);
|
||||||
|
EntityManager::flush();
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,18 @@
|
|||||||
<?php namespace App\Console;
|
<?php namespace App\Console;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
use Illuminate\Console\Scheduling\Schedule;
|
use Illuminate\Console\Scheduling\Schedule;
|
||||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Kernel
|
* Class Kernel
|
||||||
* @package App\Console
|
* @package App\Console
|
||||||
@ -18,6 +28,7 @@ class Kernel extends ConsoleKernel
|
|||||||
// Commands\Inspire::class,
|
// Commands\Inspire::class,
|
||||||
Commands\CleanOAuth2StaleData::class,
|
Commands\CleanOAuth2StaleData::class,
|
||||||
Commands\CleanOpenIdStaleData::class,
|
Commands\CleanOpenIdStaleData::class,
|
||||||
|
Commands\CreateSuperAdmin::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
44
app/Events/OAuth2ClientLocked.php
Normal file
44
app/Events/OAuth2ClientLocked.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
/**
|
||||||
|
* Class OAuth2ClientLocked
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class OAuth2ClientLocked
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $client_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OAuth2ClientLocked constructor.
|
||||||
|
* @param string $client_id
|
||||||
|
*/
|
||||||
|
public function __construct(string $client_id)
|
||||||
|
{
|
||||||
|
$this->client_id = $client_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getClientId(): string
|
||||||
|
{
|
||||||
|
return $this->client_id;
|
||||||
|
}
|
||||||
|
}
|
51
app/Events/UserCreated.php
Normal file
51
app/Events/UserCreated.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||||
|
/**
|
||||||
|
* Class UserCreated
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class UserCreated
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $user_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LifecycleEventArgs
|
||||||
|
*/
|
||||||
|
protected $args;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerified constructor.
|
||||||
|
* @param int $user_id
|
||||||
|
*/
|
||||||
|
public function __construct(int $user_id, LifecycleEventArgs $args)
|
||||||
|
{
|
||||||
|
$this->user_id = $user_id;
|
||||||
|
$this->args = $args;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getUserId(): int
|
||||||
|
{
|
||||||
|
return $this->user_id;
|
||||||
|
}
|
||||||
|
}
|
45
app/Events/UserEmailVerified.php
Normal file
45
app/Events/UserEmailVerified.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
/**
|
||||||
|
* Class UserEmailVerified
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class UserEmailVerified
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $user_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerified constructor.
|
||||||
|
* @param int $user_id
|
||||||
|
*/
|
||||||
|
public function __construct(int $user_id)
|
||||||
|
{
|
||||||
|
$this->user_id = $user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getUserId(): int
|
||||||
|
{
|
||||||
|
return $this->user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
app/Events/UserLocked.php
Normal file
45
app/Events/UserLocked.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserLocked
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class UserLocked
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $user_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerified constructor.
|
||||||
|
* @param int $user_id
|
||||||
|
*/
|
||||||
|
public function __construct(int $user_id)
|
||||||
|
{
|
||||||
|
$this->user_id = $user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getUserId(): int
|
||||||
|
{
|
||||||
|
return $this->user_id;
|
||||||
|
}
|
||||||
|
}
|
44
app/Events/UserPasswordResetRequestCreated.php
Normal file
44
app/Events/UserPasswordResetRequestCreated.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
/**
|
||||||
|
* Class UserPasswordResetRequestCreated
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class UserPasswordResetRequestCreated
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerified constructor.
|
||||||
|
* @param int $user_id
|
||||||
|
*/
|
||||||
|
public function __construct(int $id)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getId(): int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
}
|
45
app/Events/UserPasswordResetSuccessful.php
Normal file
45
app/Events/UserPasswordResetSuccessful.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php namespace App\Events;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
/**
|
||||||
|
* Class UserPasswordResetSuccessful
|
||||||
|
* @package App\Events
|
||||||
|
*/
|
||||||
|
final class UserPasswordResetSuccessful
|
||||||
|
{
|
||||||
|
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $user_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerified constructor.
|
||||||
|
* @param int $user_id
|
||||||
|
*/
|
||||||
|
public function __construct(int $user_id)
|
||||||
|
{
|
||||||
|
$this->user_id = $user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getUserId(): int
|
||||||
|
{
|
||||||
|
return $this->user_id;
|
||||||
|
}
|
||||||
|
}
|
@ -1,69 +0,0 @@
|
|||||||
<?php namespace Factories;
|
|
||||||
/**
|
|
||||||
* Copyright 2015 OpenStack Foundation
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
**/
|
|
||||||
|
|
||||||
use OAuth2\Factories\IOAuth2ClientFactory;
|
|
||||||
use OAuth2\Models\IClient;
|
|
||||||
use Models\OAuth2\Client;
|
|
||||||
use OAuth2\OAuth2Protocol;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class OAuth2ClientFactory
|
|
||||||
* @package Factories
|
|
||||||
*/
|
|
||||||
final class OAuth2ClientFactory implements IOAuth2ClientFactory
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $app_name
|
|
||||||
* @param $owner
|
|
||||||
* @param string $application_type
|
|
||||||
* @return IClient
|
|
||||||
*/
|
|
||||||
public function build($app_name, $owner, $application_type)
|
|
||||||
{
|
|
||||||
$client = new Client
|
|
||||||
(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'max_auth_codes_issuance_basis' => 0,
|
|
||||||
'max_refresh_token_issuance_basis' => 0,
|
|
||||||
'max_access_token_issuance_qty' => 0,
|
|
||||||
'max_access_token_issuance_basis' => 0,
|
|
||||||
'max_refresh_token_issuance_qty' => 0,
|
|
||||||
'use_refresh_token' => false,
|
|
||||||
'rotate_refresh_token' => false,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$client->setOwner($owner);
|
|
||||||
|
|
||||||
$client->app_name = $app_name;
|
|
||||||
$client->active = true;
|
|
||||||
$client->use_refresh_token = false;
|
|
||||||
$client->rotate_refresh_token = false;
|
|
||||||
|
|
||||||
$client->application_type = $application_type;
|
|
||||||
|
|
||||||
if ($client->client_type === IClient::ClientType_Confidential)
|
|
||||||
{
|
|
||||||
$client->token_endpoint_auth_method = OAuth2Protocol::TokenEndpoint_AuthMethod_ClientSecretBasic;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$client->token_endpoint_auth_method = OAuth2Protocol::TokenEndpoint_AuthMethod_None;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,6 +11,8 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\libs\Auth\Repositories\IBannedIPRepository;
|
||||||
|
use App\libs\Auth\Repositories\IGroupRepository;
|
||||||
use Auth\Repositories\IUserRepository;
|
use Auth\Repositories\IUserRepository;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Illuminate\Support\Facades\Response;
|
use Illuminate\Support\Facades\Response;
|
||||||
@ -24,7 +26,6 @@ use OAuth2\Repositories\IApiScopeRepository;
|
|||||||
use OAuth2\Repositories\IClientRepository;
|
use OAuth2\Repositories\IClientRepository;
|
||||||
use OAuth2\Repositories\IRefreshTokenRepository;
|
use OAuth2\Repositories\IRefreshTokenRepository;
|
||||||
use OAuth2\Repositories\IResourceServerRepository;
|
use OAuth2\Repositories\IResourceServerRepository;
|
||||||
use OAuth2\Services\ITokenService;
|
|
||||||
use OAuth2\Repositories\IApiScopeGroupRepository;
|
use OAuth2\Repositories\IApiScopeGroupRepository;
|
||||||
use OAuth2\Repositories\IServerPrivateKeyRepository;
|
use OAuth2\Repositories\IServerPrivateKeyRepository;
|
||||||
use OAuth2\Services\IApiEndpointService;
|
use OAuth2\Services\IApiEndpointService;
|
||||||
@ -33,11 +34,14 @@ use OAuth2\Services\IApiService;
|
|||||||
use OAuth2\Services\IClientService;
|
use OAuth2\Services\IClientService;
|
||||||
use OAuth2\Services\IResourceServerService;
|
use OAuth2\Services\IResourceServerService;
|
||||||
use OpenId\Services\IUserService;
|
use OpenId\Services\IUserService;
|
||||||
|
use Sokil\IsoCodes\IsoCodesFactory;
|
||||||
|
use utils\Filter;
|
||||||
|
use utils\FilterElement;
|
||||||
|
use utils\PagingInfo;
|
||||||
use Utils\Services\IAuthService;
|
use Utils\Services\IAuthService;
|
||||||
use Utils\Services\IBannedIPService;
|
use Utils\Services\IBannedIPService;
|
||||||
use Utils\Services\IServerConfigurationService;
|
use Utils\Services\IServerConfigurationService;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AdminController
|
* Class AdminController
|
||||||
* @package App\Http\Controllers
|
* @package App\Http\Controllers
|
||||||
@ -100,7 +104,7 @@ class AdminController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* @var IApiScopeGroupRepository
|
* @var IApiScopeGroupRepository
|
||||||
*/
|
*/
|
||||||
private $group_repository;
|
private $api_group_repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var IClientRepository
|
* @var IClientRepository
|
||||||
@ -132,8 +136,42 @@ class AdminController extends Controller {
|
|||||||
*/
|
*/
|
||||||
private $resource_server_repository;
|
private $resource_server_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IGroupRepository
|
||||||
|
*/
|
||||||
|
private $group_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IBannedIPRepository
|
||||||
|
*/
|
||||||
|
private $banned_ips_repository;
|
||||||
|
|
||||||
const TokenPageSize = 25;
|
const TokenPageSize = 25;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AdminController constructor.
|
||||||
|
* @param IClientService $client_service
|
||||||
|
* @param IApiScopeService $scope_service
|
||||||
|
* @param IAccessTokenRepository $access_token_repository
|
||||||
|
* @param IRefreshTokenRepository $refresh_token_repository
|
||||||
|
* @param IResourceServerService $resource_server_service
|
||||||
|
* @param IApiService $api_service
|
||||||
|
* @param IApiEndpointService $endpoint_service
|
||||||
|
* @param IAuthService $auth_service
|
||||||
|
* @param IUserService $user_service
|
||||||
|
* @param IServerConfigurationService $configuration_service
|
||||||
|
* @param IBannedIPService $banned_ips_service
|
||||||
|
* @param IServerPrivateKeyRepository $private_keys_repository
|
||||||
|
* @param IApiScopeGroupRepository $api_group_repository
|
||||||
|
* @param IClientRepository $client_repository
|
||||||
|
* @param IUserRepository $user_repository
|
||||||
|
* @param IApiEndpointRepository $endpoint_repository
|
||||||
|
* @param IApiScopeRepository $scope_repository
|
||||||
|
* @param IApiRepository $api_repository
|
||||||
|
* @param IResourceServerRepository $resource_server_repository
|
||||||
|
* @param IBannedIPRepository $banned_ips_repository
|
||||||
|
* @param IGroupRepository $group_repository
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
IClientService $client_service,
|
IClientService $client_service,
|
||||||
IApiScopeService $scope_service,
|
IApiScopeService $scope_service,
|
||||||
@ -147,13 +185,15 @@ class AdminController extends Controller {
|
|||||||
IServerConfigurationService $configuration_service,
|
IServerConfigurationService $configuration_service,
|
||||||
IBannedIPService $banned_ips_service,
|
IBannedIPService $banned_ips_service,
|
||||||
IServerPrivateKeyRepository $private_keys_repository,
|
IServerPrivateKeyRepository $private_keys_repository,
|
||||||
IApiScopeGroupRepository $group_repository,
|
IApiScopeGroupRepository $api_group_repository,
|
||||||
IClientRepository $client_repository,
|
IClientRepository $client_repository,
|
||||||
IUserRepository $user_repository,
|
IUserRepository $user_repository,
|
||||||
IApiEndpointRepository $endpoint_repository,
|
IApiEndpointRepository $endpoint_repository,
|
||||||
IApiScopeRepository $scope_repository,
|
IApiScopeRepository $scope_repository,
|
||||||
IApiRepository $api_repository,
|
IApiRepository $api_repository,
|
||||||
IResourceServerRepository $resource_server_repository
|
IResourceServerRepository $resource_server_repository,
|
||||||
|
IBannedIPRepository $banned_ips_repository,
|
||||||
|
IGroupRepository $group_repository
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -169,15 +209,21 @@ class AdminController extends Controller {
|
|||||||
$this->configuration_service = $configuration_service;
|
$this->configuration_service = $configuration_service;
|
||||||
$this->banned_ips_service = $banned_ips_service;
|
$this->banned_ips_service = $banned_ips_service;
|
||||||
$this->private_keys_repository = $private_keys_repository;
|
$this->private_keys_repository = $private_keys_repository;
|
||||||
$this->group_repository = $group_repository;
|
$this->api_group_repository = $api_group_repository;
|
||||||
$this->client_repository = $client_repository;
|
$this->client_repository = $client_repository;
|
||||||
$this->user_repository = $user_repository;
|
$this->user_repository = $user_repository;
|
||||||
$this->endpoint_repository = $endpoint_repository;
|
$this->endpoint_repository = $endpoint_repository;
|
||||||
$this->scope_repository = $scope_repository;
|
$this->scope_repository = $scope_repository;
|
||||||
$this->api_repository = $api_repository;
|
$this->api_repository = $api_repository;
|
||||||
$this->resource_server_repository = $resource_server_repository;
|
$this->resource_server_repository = $resource_server_repository;
|
||||||
|
$this->banned_ips_repository = $banned_ips_repository;
|
||||||
|
$this->group_repository = $group_repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
public function editRegisteredClient($id)
|
public function editRegisteredClient($id)
|
||||||
{
|
{
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
@ -189,41 +235,41 @@ class AdminController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$selected_scopes = $client->getClientScopes();
|
$selected_scopes = $client->getClientScopes();
|
||||||
$aux_scopes = array();
|
$aux_scopes = [];
|
||||||
|
|
||||||
foreach ($selected_scopes as $scope) {
|
foreach ($selected_scopes as $scope) {
|
||||||
array_push($aux_scopes, $scope->id);
|
array_push($aux_scopes, $scope->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
// scope pre processing
|
// scope pre processing
|
||||||
$scopes = $this->scope_service->getAvailableScopes();
|
$scopes = $this->scope_repository->getAvailableScopes();
|
||||||
$group_scopes = $user->getGroupScopes();
|
$group_scopes = $user->getGroupScopes();
|
||||||
$merged_scopes = array_merge($scopes, $group_scopes);
|
$merged_scopes = array_merge($scopes, $group_scopes);
|
||||||
$final_scopes = [];
|
$final_scopes = [];
|
||||||
$processed_scopes = [];
|
$processed_scopes = [];
|
||||||
foreach($merged_scopes as $test_scope){
|
foreach($merged_scopes as $test_scope){
|
||||||
if(isset($processed_scopes[$test_scope->id])) continue;
|
if(isset($processed_scopes[$test_scope->getId()])) continue;
|
||||||
|
|
||||||
$processed_scopes[$test_scope->id] = $test_scope->id;
|
$processed_scopes[$test_scope->getId()] = $test_scope->getId();
|
||||||
$final_scopes[] = $test_scope;
|
$final_scopes[] = $test_scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
usort($final_scopes, function($elem1, $elem2){
|
usort($final_scopes, function($elem1, $elem2){
|
||||||
return $elem1->api_id > $elem2->api_id;
|
return $elem1->getApiId() > $elem2->getApiId() ;
|
||||||
});
|
});
|
||||||
// scope pre processing
|
// scope pre processing
|
||||||
|
|
||||||
$access_tokens = $this->access_token_repository->getAllValidByClientIdentifier($client->getId(), 1 , self::TokenPageSize);
|
$access_tokens = $this->access_token_repository->getAllValidByClientIdentifier($client->getId(), new PagingInfo(1 , self::TokenPageSize));
|
||||||
|
|
||||||
foreach ($access_tokens->items() as $token) {
|
foreach ($access_tokens->getItems() as $token) {
|
||||||
$friendly_scopes = $this->scope_service->getFriendlyScopesByName(explode(' ', $token->scope));
|
$friendly_scopes = $this->scope_repository->getFriendlyScopesByName(explode(' ', $token->scope));
|
||||||
$token->setFriendlyScopes(implode(',', $friendly_scopes));
|
$token->setFriendlyScopes(implode(',', $friendly_scopes));
|
||||||
}
|
}
|
||||||
|
|
||||||
$refresh_tokens = $this->refresh_token_repository->getAllValidByClientIdentifier($client->getId(), 1 , self::TokenPageSize);
|
$refresh_tokens = $this->refresh_token_repository->getAllValidByClientIdentifier($client->getId(), new PagingInfo(1 , self::TokenPageSize));
|
||||||
|
|
||||||
foreach ($refresh_tokens->items() as $token) {
|
foreach ($refresh_tokens->getItems() as $token) {
|
||||||
$friendly_scopes = $this->scope_service->getFriendlyScopesByName(explode(' ', $token->scope));
|
$friendly_scopes = $this->scope_repository->getFriendlyScopesByName(explode(' ', $token->scope));
|
||||||
$token->setFriendlyScopes(implode(',', $friendly_scopes));
|
$token->setFriendlyScopes(implode(',', $friendly_scopes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,13 +278,11 @@ class AdminController extends Controller {
|
|||||||
'client' => $client,
|
'client' => $client,
|
||||||
'selected_scopes' => $aux_scopes,
|
'selected_scopes' => $aux_scopes,
|
||||||
'scopes' => $final_scopes,
|
'scopes' => $final_scopes,
|
||||||
'access_tokens' => $access_tokens->items(),
|
'access_tokens' => $access_tokens->getItems(),
|
||||||
'access_tokens_pages' => $access_tokens->total() > 0 ? intval(ceil($access_tokens->total() / self::TokenPageSize)) : 0,
|
'access_tokens_pages' => $access_tokens->getTotal() > 0 ? intval(ceil($access_tokens->getTotal() / self::TokenPageSize)) : 0,
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
"use_system_scopes" => $user->canUseSystemScopes(),
|
"use_system_scopes" => $user->canUseSystemScopes(),
|
||||||
'refresh_tokens' => $refresh_tokens->items(),
|
'refresh_tokens' => $refresh_tokens->getItems(),
|
||||||
'refresh_tokens_pages' => $refresh_tokens->total() > 0 ? intval(ceil($refresh_tokens->total() / self::TokenPageSize)) : 0,
|
'refresh_tokens_pages' => $refresh_tokens->getTotal() > 0 ? intval(ceil($refresh_tokens->getTotal() / self::TokenPageSize)) : 0,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,29 +291,29 @@ class AdminController extends Controller {
|
|||||||
public function listApiScopeGroups()
|
public function listApiScopeGroups()
|
||||||
{
|
{
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$groups = $this->group_repository->getAll(1, PHP_INT_MAX);
|
$groups = $this->api_group_repository->getAllByPage(new PagingInfo(1, PHP_INT_MAX));
|
||||||
$non_selected_scopes = $this->scope_service->getAssignedByGroups();
|
$non_selected_scopes = $this->scope_repository->getAssignableByGroups();
|
||||||
return View::make("oauth2.profile.admin.api-scope-groups",array
|
return View::make("oauth2.profile.admin.api-scope-groups", [
|
||||||
(
|
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'groups' => $groups,
|
'groups' => $groups,
|
||||||
'non_selected_scopes' => $non_selected_scopes,
|
'non_selected_scopes' => $non_selected_scopes,
|
||||||
));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function editApiScopeGroup($id){
|
public function editApiScopeGroup($id){
|
||||||
$group = $this->group_repository->get($id);
|
$group = $this->api_group_repository->getById($id);
|
||||||
|
|
||||||
if(is_null($group))
|
if(is_null($group))
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
|
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$non_selected_scopes = $this->scope_service->getAssignedByGroups();
|
$non_selected_scopes = $this->scope_repository->getAssignableByGroups();
|
||||||
return View::make("oauth2.profile.admin.edit-api-scope-group",
|
return View::make("oauth2.profile.admin.edit-api-scope-group",
|
||||||
array
|
array
|
||||||
(
|
(
|
||||||
'is_oauth2_admin' => $user->isOAuth2ServerAdmin(),
|
|
||||||
'is_openstackid_admin' => $user->isOpenstackIdAdmin(),
|
|
||||||
'group' => $group,
|
'group' => $group,
|
||||||
'non_selected_scopes' => $non_selected_scopes,
|
'non_selected_scopes' => $non_selected_scopes,
|
||||||
)
|
)
|
||||||
@ -277,78 +321,93 @@ class AdminController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Resource servers
|
// Resource servers
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
public function listResourceServers() {
|
public function listResourceServers() {
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$resource_servers = $this->resource_server_repository->getAll(1, PHP_INT_MAX);
|
$resource_servers = $this->resource_server_repository->getAllByPage(new PagingInfo(1, PHP_INT_MAX));
|
||||||
return View::make("oauth2.profile.admin.resource-servers",array(
|
return View::make("oauth2.profile.admin.resource-servers",
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
[
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
'resource_servers' => $resource_servers
|
||||||
'resource_servers' => $resource_servers));
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function editResourceServer($id){
|
public function editResourceServer($id){
|
||||||
$resource_server = $this->resource_server_repository->get($id);
|
$resource_server = $this->resource_server_repository->getById($id);
|
||||||
if(is_null($resource_server))
|
if(is_null($resource_server))
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
return View::make("oauth2.profile.admin.edit-resource-server",array(
|
return View::make("oauth2.profile.admin.edit-resource-server",array(
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'resource_server'=>$resource_server
|
'resource_server'=>$resource_server
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function editApi($id){
|
public function editApi($id){
|
||||||
$api = $this->api_repository->get($id);
|
$api = $this->api_repository->getById($id);
|
||||||
if(is_null($api))
|
if(is_null($api))
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
return View::make("oauth2.profile.admin.edit-api",array(
|
return View::make("oauth2.profile.admin.edit-api",['api'=>$api]);
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'api'=>$api));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function editScope($id){
|
public function editScope($id){
|
||||||
$scope = $this->scope_repository->get($id);
|
$scope = $this->scope_repository->getById($id);
|
||||||
if(is_null($scope))
|
if(is_null($scope))
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
return View::make("oauth2.profile.admin.edit-scope",array(
|
return View::make("oauth2.profile.admin.edit-scope",array(
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'scope'=>$scope));
|
'scope'=>$scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function editEndpoint($id){
|
public function editEndpoint($id){
|
||||||
$endpoint = $this->endpoint_repository->get($id);
|
$endpoint = $this->endpoint_repository->getById($id);
|
||||||
if(is_null($endpoint)) return Response::view('errors.404', array(), 404);
|
if(is_null($endpoint)) return Response::view('errors.404', [], 404);
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$selected_scopes = array();
|
$selected_scopes = [];
|
||||||
$list = $endpoint->scopes()->get(array('id'));
|
$list = $endpoint->getScopes();
|
||||||
foreach($list as $selected_scope){
|
foreach($list as $selected_scope){
|
||||||
array_push($selected_scopes,$selected_scope->id);
|
$selected_scopes[] = $selected_scope->getId();
|
||||||
}
|
}
|
||||||
return View::make('oauth2.profile.admin.edit-endpoint',array(
|
return View::make('oauth2.profile.admin.edit-endpoint',array(
|
||||||
'is_oauth2_admin' => $user->isOAuth2ServerAdmin(),
|
|
||||||
'is_openstackid_admin' => $user->isOpenstackIdAdmin(),
|
|
||||||
'endpoint' => $endpoint ,
|
'endpoint' => $endpoint ,
|
||||||
'selected_scopes' => $selected_scopes));
|
'selected_scopes' => $selected_scopes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
public function editIssuedGrants(){
|
public function editIssuedGrants(){
|
||||||
|
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$access_tokens = $this->access_token_repository->getAllValidByUserId($user->getId(), 1, self::TokenPageSize);
|
$access_tokens = $this->access_token_repository->getAllValidByUserId($user->getId(), new PagingInfo(1, self::TokenPageSize));
|
||||||
$refresh_tokens = $this->refresh_token_repository->getAllValidByUserId($user->getId(), 1, self::TokenPageSize);
|
$refresh_tokens = $this->refresh_token_repository->getAllValidByUserId($user->getId(), new PagingInfo(1, self::TokenPageSize));
|
||||||
|
|
||||||
foreach($access_tokens->items() as $access_token){
|
foreach($access_tokens->getItems() as $access_token){
|
||||||
$friendly_scopes = $this->scope_service->getFriendlyScopesByName(explode(' ',$access_token->scope));
|
$friendly_scopes = $this->scope_repository->getFriendlyScopesByName(explode(' ',$access_token->getScope()));
|
||||||
$access_token->setFriendlyScopes(implode(', ',$friendly_scopes));
|
$access_token->setFriendlyScopes(implode(', ',$friendly_scopes));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($refresh_tokens->items() as $refresh_token){
|
foreach($refresh_tokens->getItems() as $refresh_token){
|
||||||
$friendly_scopes = $this->scope_service->getFriendlyScopesByName(explode(' ',$refresh_token->scope));
|
$friendly_scopes = $this->scope_repository->getFriendlyScopesByName(explode(' ',$refresh_token->getScope()));
|
||||||
$refresh_token->setFriendlyScopes(implode(', ',$friendly_scopes));
|
$refresh_token->setFriendlyScopes(implode(', ',$friendly_scopes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,72 +415,46 @@ class AdminController extends Controller {
|
|||||||
array
|
array
|
||||||
(
|
(
|
||||||
'user_id' => $user->getId(),
|
'user_id' => $user->getId(),
|
||||||
'access_tokens' => $access_tokens->items() ,
|
'access_tokens' => $access_tokens->getItems() ,
|
||||||
'access_tokens_pages' => $access_tokens->total() > 0 ? intval(ceil($access_tokens->total() / self::TokenPageSize)) : 0,
|
'access_tokens_pages' => $access_tokens->getTotal() > 0 ? intval(ceil($access_tokens->getTotal() / self::TokenPageSize)) : 0,
|
||||||
'refresh_tokens' => $refresh_tokens->items(),
|
'refresh_tokens' => $refresh_tokens->getItems(),
|
||||||
'refresh_tokens_pages' => $refresh_tokens->total() > 0 ? intval(ceil($refresh_tokens->total() / self::TokenPageSize)) : 0,
|
'refresh_tokens_pages' => $refresh_tokens->getTotal() > 0 ? intval(ceil($refresh_tokens->getTotal() / self::TokenPageSize)) : 0,
|
||||||
'is_oauth2_admin' => $user->isOAuth2ServerAdmin(),
|
|
||||||
'is_openstackid_admin' => $user->isOpenstackIdAdmin(),
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
public function listOAuth2Clients(){
|
public function listOAuth2Clients(){
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$clients = $user->getClients();
|
$clients = $user->getAvailableClients();
|
||||||
|
|
||||||
return View::make("oauth2.profile.clients", array(
|
return View::make("oauth2.profile.clients", [
|
||||||
"username" => $user->getFullName(),
|
"username" => $user->getFullName(),
|
||||||
"user_id" => $user->getId(),
|
"user_id" => $user->getId(),
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
"use_system_scopes" => $user->canUseSystemScopes(),
|
"use_system_scopes" => $user->canUseSystemScopes(),
|
||||||
'clients' => $clients,
|
'clients' => $clients,
|
||||||
));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
public function listLockedClients(){
|
public function listLockedClients(){
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$filter = new Filter();
|
||||||
$clients = $this->client_repository->getAll(1, PHP_INT_MAX,[
|
$filter->addFilterCondition(FilterElement::makeEqual('locked', true));
|
||||||
[
|
$clients = $this->client_repository->getAllByPage(new PagingInfo(1, PHP_INT_MAX), $filter);
|
||||||
'name'=>'locked',
|
|
||||||
'op' => '=',
|
|
||||||
'value'=> true
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
return View::make("oauth2.profile.admin.clients", array(
|
return View::make("oauth2.profile.admin.clients",[
|
||||||
"username" => $user->getFullName(),
|
'clients' => $clients
|
||||||
"user_id" => $user->getId(),
|
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'clients' => $clients,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function listLockedUsers(){
|
|
||||||
$user = $this->auth_service->getCurrentUser();
|
|
||||||
$users = $this->user_repository->getAll(1, PHP_INT_MAX,[
|
|
||||||
[
|
|
||||||
'name' => 'lock',
|
|
||||||
'op' => '=',
|
|
||||||
'value' => true
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
return View::make('admin.users', [
|
|
||||||
'username' => $user->getFullName(),
|
|
||||||
'user_id' => $user->getId(),
|
|
||||||
'is_oauth2_admin' => $user->isOAuth2ServerAdmin(),
|
|
||||||
'is_openstackid_admin' => $user->isOpenstackIdAdmin(),
|
|
||||||
'users' => $users,
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listServerConfig(){
|
public function listServerConfig(){
|
||||||
|
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$config_values = array();
|
$config_values = [];
|
||||||
$dictionary = array
|
$dictionary = array
|
||||||
(
|
(
|
||||||
'MaxFailed.Login.Attempts',
|
'MaxFailed.Login.Attempts',
|
||||||
@ -451,8 +484,6 @@ class AdminController extends Controller {
|
|||||||
(
|
(
|
||||||
"username" => $user->getFullName(),
|
"username" => $user->getFullName(),
|
||||||
"user_id" => $user->getId(),
|
"user_id" => $user->getId(),
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
'config_values' => $config_values,
|
'config_values' => $config_values,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -522,28 +553,104 @@ class AdminController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function listBannedIPs(){
|
public function listBannedIPs(){
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$page = $this->banned_ips_repository->getAllByPage(new PagingInfo(1, PHP_INT_MAX));
|
||||||
$ips = $this->banned_ips_service->getByPage(1, PHP_INT_MAX);
|
return View::make("admin.banned-ips",[
|
||||||
return View::make("admin.banned-ips",
|
"page" => $page
|
||||||
array
|
]
|
||||||
(
|
|
||||||
"username" => $user->getFullName(),
|
|
||||||
"user_id" => $user->getId(),
|
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
"ips" => $ips
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listServerPrivateKeys(){
|
public function listServerPrivateKeys(){
|
||||||
|
return View::make("oauth2.profile.admin.server-private-keys",
|
||||||
|
[
|
||||||
|
'private_keys' => $this->private_keys_repository->getAllByPage(new PagingInfo(1, PHP_INT_MAX)),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$user = $this->auth_service->getCurrentUser();
|
public function listUsers(){
|
||||||
|
// init database
|
||||||
|
$isoCodes = new IsoCodesFactory();
|
||||||
|
|
||||||
return View::make("oauth2.profile.admin.server-private-keys", array(
|
// get languages database
|
||||||
'private_keys' => $this->private_keys_repository->getAll(1, PHP_INT_MAX),
|
$languages = $isoCodes->getLanguages()->toArray();
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
$lang2Code = [];
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
foreach ($languages as $lang){
|
||||||
));
|
if(!empty($lang->getAlpha2()))
|
||||||
|
$lang2Code[] = $lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get countries database
|
||||||
|
$countries = $isoCodes->getCountries()->toArray();
|
||||||
|
return View::make("admin.users",
|
||||||
|
[
|
||||||
|
'page' => $this->user_repository->getAllByPage(new PagingInfo(1, 10)),
|
||||||
|
'countries' => $countries,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listGroups(){
|
||||||
|
return View::make("admin.groups",
|
||||||
|
[
|
||||||
|
'groups' => $this->group_repository->getAllByPage(new PagingInfo(1, 10)),
|
||||||
|
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $user_id
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
|
public function editUser($user_id){
|
||||||
|
|
||||||
|
$user = $this->user_repository->getById($user_id);
|
||||||
|
if (is_null($user)) {
|
||||||
|
Log::warning(sprintf("invalid user id %s", $user_id));
|
||||||
|
return View::make("errors.404");
|
||||||
|
}
|
||||||
|
|
||||||
|
// init database
|
||||||
|
$isoCodes = new IsoCodesFactory();
|
||||||
|
|
||||||
|
// get languages database
|
||||||
|
$languages = $isoCodes->getLanguages()->toArray();
|
||||||
|
$lang2Code = [];
|
||||||
|
foreach ($languages as $lang){
|
||||||
|
if(!empty($lang->getAlpha2()))
|
||||||
|
$lang2Code[] = $lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get countries database
|
||||||
|
$countries = $isoCodes->getCountries()->toArray();
|
||||||
|
|
||||||
|
return View::make("admin.edit-user",
|
||||||
|
[
|
||||||
|
'user' => $user,
|
||||||
|
'countries' => $countries,
|
||||||
|
'languages' => $lang2Code,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $group_id
|
||||||
|
* @return \Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
|
public function editGroup($group_id){
|
||||||
|
|
||||||
|
$group = $this->group_repository->getById($group_id);
|
||||||
|
|
||||||
|
if (is_null($group)) {
|
||||||
|
Log::warning(sprintf("invalid group id %s", $group_id));
|
||||||
|
return View::make("errors.404");
|
||||||
|
}
|
||||||
|
|
||||||
|
return View::make("admin.edit-group",
|
||||||
|
[
|
||||||
|
'group' => $group,
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
245
app/Http/Controllers/Api/APICRUDController.php
Normal file
245
app/Http/Controllers/Api/APICRUDController.php
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
<?php namespace App\Http\Controllers;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Api\JsonController;
|
||||||
|
use App\Http\Utils\PagingConstants;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use App\Services\IBaseService;
|
||||||
|
use Illuminate\Support\Facades\Input;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use models\utils\IBaseRepository;
|
||||||
|
use utils\Filter;
|
||||||
|
use utils\FilterParser;
|
||||||
|
use utils\OrderParser;
|
||||||
|
use utils\PagingInfo;
|
||||||
|
use Utils\Services\ILogService;
|
||||||
|
use Exception;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
/**
|
||||||
|
* Class APICRUDController
|
||||||
|
* @package App\Http\Controllers
|
||||||
|
*/
|
||||||
|
abstract class APICRUDController extends JsonController
|
||||||
|
{
|
||||||
|
use GetAllTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IBaseRepository
|
||||||
|
*/
|
||||||
|
protected $repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IBaseService
|
||||||
|
*/
|
||||||
|
protected $service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param IBaseRepository $repository
|
||||||
|
* @param IBaseService $service
|
||||||
|
* @param ILogService $log_service
|
||||||
|
*/
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IBaseRepository $repository,
|
||||||
|
IBaseService $service,
|
||||||
|
ILogService $log_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
parent::__construct($log_service);
|
||||||
|
$this->repository = $repository;
|
||||||
|
$this->service = $service;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getEntityNotFoundMessage($id):string {
|
||||||
|
return sprintf("entity %s not found", $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function get($id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$entity = $this->repository->getById($id);
|
||||||
|
if (is_null($entity)) {
|
||||||
|
throw new EntityNotFoundException($this->getEntityNotFoundMessage($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->ok(SerializerRegistry::getInstance()->getSerializer($entity, $this->serializerType())->serialize
|
||||||
|
(
|
||||||
|
Input::get("expand", '')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function serializerType():string{
|
||||||
|
return SerializerRegistry::SerializerType_Public;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected abstract function getUpdatePayloadValidationRules():array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayload():array{
|
||||||
|
return Input::All();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function update($id)
|
||||||
|
{
|
||||||
|
$payload = $this->getUpdatePayload();
|
||||||
|
return $this->_update($id, $payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function curateUpdatePayload(array $payload):array {
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function curateCreatePayload(array $payload):array {
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param array $payload
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
protected function _update($id, array $payload)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
$rules = $this->getUpdatePayloadValidationRules();
|
||||||
|
// Creates a Validator instance and validates the data.
|
||||||
|
$validation = Validator::make($payload, $rules);
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$ex = new ValidationException();
|
||||||
|
throw $ex->setMessages($validation->messages()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
$entity = $this->service->update($id, $this->curateUpdatePayload($payload));
|
||||||
|
|
||||||
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity, $this->serializerType())->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected abstract function getCreatePayloadValidationRules():array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayload():array{
|
||||||
|
return Input::All();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$payload = $this->getCreatePayload();
|
||||||
|
|
||||||
|
$rules = $this->getCreatePayloadValidationRules();
|
||||||
|
// Creates a Validator instance and validates the data.
|
||||||
|
$validation = Validator::make($payload, $rules);
|
||||||
|
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$ex = new ValidationException();
|
||||||
|
throw $ex->setMessages($validation->messages()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
$entity = $this->service->create($this->curateCreatePayload($payload));
|
||||||
|
|
||||||
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($entity, $this->serializerType())->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function delete($id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->service->delete($id);
|
||||||
|
return $this->deleted();
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,68 +0,0 @@
|
|||||||
<?php namespace App\Http\Controllers\Api;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copyright 2015 OpenStack Foundation
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
**/
|
|
||||||
use Utils\Services\ILogService;
|
|
||||||
/**
|
|
||||||
* Class AbstractRESTController
|
|
||||||
* @package App\Http\Controllers\Apis
|
|
||||||
*/
|
|
||||||
abstract class AbstractRESTController extends JsonController
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
protected $allowed_filter_fields;
|
|
||||||
protected $allowed_projection_fields;
|
|
||||||
|
|
||||||
protected $filter_delimiter;
|
|
||||||
protected $field_delimiter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AbstractRESTController constructor.
|
|
||||||
* @param ILogService $log_service
|
|
||||||
*/
|
|
||||||
public function __construct(ILogService $log_service)
|
|
||||||
{
|
|
||||||
parent::__construct($log_service);
|
|
||||||
$this->filter_delimiter = '+';
|
|
||||||
$this->field_delimiter = ',';
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getProjection($fields)
|
|
||||||
{
|
|
||||||
if(!is_string($fields)) return array('*');
|
|
||||||
if(empty($fields)) return array('*');
|
|
||||||
$fields_args = explode($this->field_delimiter,$fields);
|
|
||||||
$res = array();
|
|
||||||
foreach($fields_args as $exp){
|
|
||||||
if(in_array($exp,$this->allowed_projection_fields)){
|
|
||||||
array_push($res,$exp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!count($res))
|
|
||||||
$res = array('*');
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getFilters($filters)
|
|
||||||
{
|
|
||||||
if(!is_array($filters)) return array();
|
|
||||||
$res = array();
|
|
||||||
foreach($filters as $fieldname=>$value){
|
|
||||||
if(in_array($fieldname,$this->allowed_filter_fields)){
|
|
||||||
array_push($res,['name' => $fieldname, 'op' => '=','value' => $value]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,107 +11,123 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
|
use App\libs\Auth\Repositories\IBannedIPRepository;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
use Utils\Services\IBannedIPService;
|
use Utils\Services\IBannedIPService;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use App\Http\Controllers\ICRUDController;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ApiBannedIPController
|
* Class ApiBannedIPController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
*/
|
*/
|
||||||
class ApiBannedIPController extends AbstractRESTController implements ICRUDController
|
final class ApiBannedIPController extends APICRUDController
|
||||||
{
|
{
|
||||||
|
|
||||||
private $banned_ip_service;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* ApiBannedIPController constructor.
|
||||||
|
* @param IBannedIPRepository $banned_ip_repository
|
||||||
* @param IBannedIPService $banned_ip_service
|
* @param IBannedIPService $banned_ip_service
|
||||||
* @param ILogService $log_service
|
* @param ILogService $log_service
|
||||||
*/
|
*/
|
||||||
public function __construct(IBannedIPService $banned_ip_service, ILogService $log_service)
|
public function __construct
|
||||||
|
(
|
||||||
|
IBannedIPRepository $banned_ip_repository,
|
||||||
|
IBannedIPService $banned_ip_service,
|
||||||
|
ILogService $log_service
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
parent::__construct($log_service);
|
parent::__construct($banned_ip_repository, $banned_ip_service, $log_service);
|
||||||
|
|
||||||
$this->banned_ip_service = $banned_ip_service;
|
|
||||||
$this->allowed_filter_fields = array();
|
|
||||||
$this->allowed_projection_fields = array('*');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
public function get($id)
|
public function get($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$ip = Input::get("ip", null);
|
$ip = Input::get("ip", null);
|
||||||
if (!is_null($ip)) {
|
if (!is_null($ip)) {
|
||||||
$banned_ip = $this->banned_ip_service->getByIP($ip);
|
$banned_ip = $this->repository->getByIp(strval($ip));
|
||||||
} else {
|
} else {
|
||||||
$banned_ip = $this->banned_ip_service->get($id);
|
$banned_ip = $this->repository->getById(intval($id));
|
||||||
}
|
}
|
||||||
if (is_null($banned_ip)) {
|
if (is_null($banned_ip)) {
|
||||||
return $this->error404(array('error' => 'banned ip not found'));
|
throw new EntityNotFoundException();
|
||||||
}
|
}
|
||||||
|
return $this->ok(SerializerRegistry::getInstance()->getSerializer($banned_ip)->serialize());
|
||||||
$data = $banned_ip->toArray();
|
}
|
||||||
return $this->ok($data);
|
catch (ValidationException $ex1)
|
||||||
} catch (Exception $ex) {
|
{
|
||||||
$this->log_service->error($ex);
|
Log::warning($ex1);
|
||||||
return $this->error500($ex);
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
}
|
}
|
||||||
}
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
public function create()
|
Log::warning($ex2);
|
||||||
{
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
// TODO: Implement create() method.
|
}
|
||||||
}
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
//check for optional filters param on querystring
|
|
||||||
$fields = $this->getProjection(Input::get('fields', null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields', 'limit', 'offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset', 1));
|
|
||||||
$page_size = intval(Input::get('limit', 10));
|
|
||||||
|
|
||||||
$list = $this->banned_ip_service->getByPage($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = array();
|
|
||||||
foreach ($list->getItems() as $ip) {
|
|
||||||
array_push($items, $ip->toArray());
|
|
||||||
}
|
|
||||||
return $this->ok(array(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->getTotal()
|
|
||||||
));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
public function delete($id = null)
|
public function delete($id = null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (is_null($id)) {
|
if (is_null($id)) {
|
||||||
$ip = Input::get("ip", null);
|
$ip = Input::get("ip", null);
|
||||||
} else {
|
} else {
|
||||||
$banned_ip = $this->banned_ip_service->get($id);
|
$banned_ip = $this->repository->getById($id);
|
||||||
$ip = $banned_ip->ip;
|
$ip = $banned_ip->getIp();
|
||||||
}
|
}
|
||||||
if (is_null($ip))
|
if (is_null($ip))
|
||||||
return $this->error400('invalid request');
|
return $this->error400('invalid request');
|
||||||
$res = $this->banned_ip_service->delete($ip);
|
$this->service->deleteByIP($ip);
|
||||||
return $res ? $this->deleted() : $this->error404(array('error' => 'operation failed'));
|
return $this->deleted();
|
||||||
} catch (Exception $ex) {
|
}
|
||||||
$this->log_service->error($ex);
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update()
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
{
|
{
|
||||||
// TODO: Implement update() method.
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,31 +11,21 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use OAuth2\Repositories\IApiRepository;
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
use Utils\Services\ILogService;
|
|
||||||
use OAuth2\Services\IApiService;
|
|
||||||
use OAuth2\Exceptions\InvalidApi;
|
|
||||||
use App\Http\Controllers\ICRUDController;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use OAuth2\Repositories\IApiRepository;
|
||||||
|
use OAuth2\Services\IApiService;
|
||||||
|
use Utils\Services\ILogService;
|
||||||
/**
|
/**
|
||||||
* Class ApiController
|
* Class ApiController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
*/
|
*/
|
||||||
class ApiController extends AbstractRESTController implements ICRUDController
|
final class ApiController extends APICRUDController
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var IApiService
|
|
||||||
*/
|
|
||||||
private $api_service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiRepository
|
|
||||||
*/
|
|
||||||
private $api_repository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ApiController constructor.
|
* ApiController constructor.
|
||||||
@ -50,176 +40,94 @@ class ApiController extends AbstractRESTController implements ICRUDController
|
|||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($log_service);
|
parent::__construct($api_repository, $api_service, $log_service);
|
||||||
$this->api_repository = $api_repository;
|
|
||||||
$this->api_service = $api_service;
|
|
||||||
//set filters allowed values
|
|
||||||
$this->allowed_filter_fields = ['resource_server_id'];
|
|
||||||
$this->allowed_projection_fields = ['*'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($id)
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function activate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$api = $this->api_repository->get($id);
|
$api = $this->service->update($id, ['active' => true]);
|
||||||
if(is_null($api)){
|
|
||||||
return $this->error404(array('error' => 'api not found'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($api)->serialize());
|
||||||
}
|
|
||||||
$scopes = $api->scopes()->get(array('id','name'));
|
} catch (ValidationException $ex1) {
|
||||||
$endpoints = $api->endpoints()->get(array('id','name'));
|
Log::warning($ex1);
|
||||||
$data = $api->toArray();
|
return $this->error412(array($ex1->getMessage()));
|
||||||
$data['scopes'] = $scopes->toArray();
|
} catch (EntityNotFoundException $ex2) {
|
||||||
$data['endpoints'] = $endpoints->toArray();
|
Log::warning($ex2);
|
||||||
return $this->ok($data);
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getByPage()
|
/**
|
||||||
{
|
* @return array
|
||||||
try {
|
*/
|
||||||
//check for optional filters param on querystring
|
protected function getFilterRules():array{
|
||||||
$fields = $this->getProjection(Input::get('fields',null));
|
return [
|
||||||
$filters = $this->getFilters(Input::except('fields','limit','offset'));
|
'resource_server_id' => ['==']
|
||||||
$page_nbr = intval(Input::get('offset',1));
|
];
|
||||||
$page_size = intval(Input::get('limit',10));
|
|
||||||
$list = $this->api_repository->getAll($page_nbr,$page_size, $filters,$fields);
|
|
||||||
$items = array();
|
|
||||||
foreach ($list->items() as $api)
|
|
||||||
{
|
|
||||||
array_push($items, $api->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok
|
|
||||||
(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (Exception $ex)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
/**
|
||||||
{
|
* @return array
|
||||||
try {
|
*/
|
||||||
$new_api = Input::all();
|
protected function getFilterValidatorRules():array{
|
||||||
|
return [
|
||||||
$rules = array(
|
'resource_server_id' => 'sometimes|required|integer',
|
||||||
'name' => 'required|alpha_dash|max:255',
|
];
|
||||||
'description' => 'required|text',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
'resource_server_id' => 'required|integer',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($new_api, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_api_model = $this->api_service->add(
|
|
||||||
$new_api['name'],
|
|
||||||
$new_api['description'],
|
|
||||||
$new_api['active'],
|
|
||||||
$new_api['resource_server_id']
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->created(array('api_id' => $new_api_model->id));
|
|
||||||
}
|
|
||||||
catch (InvalidApi $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error400(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($id)
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function deactivate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$res = $this->api_service->delete($id);
|
$api = $this->service->update($id, ['active' => false]);
|
||||||
return $res ? $this->deleted() : $this->error404(array('error'=>'operation failed'));
|
|
||||||
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($api)->serialize());
|
||||||
|
} catch (ValidationException $ex1) {
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array($ex1->getMessage()));
|
||||||
|
} catch (EntityNotFoundException $ex2) {
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(){
|
/**
|
||||||
try {
|
* @return array
|
||||||
|
*/
|
||||||
$values = Input::all();
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
$rules = array(
|
return [
|
||||||
'id' => 'required|integer',
|
'name' => 'sometimes|required|alpha_dash|max:255',
|
||||||
'name' => 'sometimes|required|alpha_dash|max:255',
|
'description' => 'sometimes|required|text',
|
||||||
'description' => 'sometimes|required|text',
|
'active' => 'sometimes|required|boolean',
|
||||||
'active' => 'sometimes|required|boolean',
|
];
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->api_service->update(intval($values['id']),$values);
|
|
||||||
|
|
||||||
return $this->ok();
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(InvalidApi $ex1){
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function activate($id){
|
/**
|
||||||
try {
|
* @return array
|
||||||
$res = $this->api_service->setStatus($id,true);
|
*/
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
protected function getCreatePayloadValidationRules(): array
|
||||||
}
|
{
|
||||||
catch(InvalidApi $ex1){
|
return [
|
||||||
$this->log_service->error($ex1);
|
'name' => 'required|alpha_dash|max:255',
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
'description' => 'required|text',
|
||||||
}
|
'active' => 'required|boolean',
|
||||||
catch (Exception $ex) {
|
'resource_server_id' => 'required|integer',
|
||||||
$this->log_service->error($ex);
|
];
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deactivate($id){
|
|
||||||
try {
|
|
||||||
$res = $this->api_service->setStatus($id,false);
|
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
|
||||||
}
|
|
||||||
catch(InvalidApi $ex1){
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -11,35 +11,28 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use App\Http\Controllers\ICRUDController;
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use models\exceptions\EntityNotFoundException;
|
||||||
use OAuth2\Exceptions\InvalidApiEndpoint;
|
use models\exceptions\ValidationException;
|
||||||
use OAuth2\Exceptions\InvalidApiScope;
|
|
||||||
use OAuth2\Repositories\IApiEndpointRepository;
|
use OAuth2\Repositories\IApiEndpointRepository;
|
||||||
use OAuth2\Services\IApiEndpointService;
|
use OAuth2\Services\IApiEndpointService;
|
||||||
use Utils\Exceptions\EntityNotFoundException;
|
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ApiEndpointController
|
* Class ApiEndpointController
|
||||||
* REST Controller for Api endpoint entity CRUD ops
|
* REST Controller for Api endpoint entity CRUD ops
|
||||||
*/
|
*/
|
||||||
class ApiEndpointController extends AbstractRESTController implements ICRUDController {
|
final class ApiEndpointController extends APICRUDController {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var IApiEndpointService
|
* ApiEndpointController constructor.
|
||||||
|
* @param IApiEndpointService $api_endpoint_service
|
||||||
|
* @param IApiEndpointRepository $endpoint_repository
|
||||||
|
* @param ILogService $log_service
|
||||||
*/
|
*/
|
||||||
private $api_endpoint_service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiEndpointRepository
|
|
||||||
*/
|
|
||||||
private $endpoint_repository;
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct
|
public function __construct
|
||||||
(
|
(
|
||||||
IApiEndpointService $api_endpoint_service,
|
IApiEndpointService $api_endpoint_service,
|
||||||
@ -47,231 +40,146 @@ class ApiEndpointController extends AbstractRESTController implements ICRUDContr
|
|||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($log_service);
|
parent::__construct($endpoint_repository, $api_endpoint_service, $log_service);
|
||||||
$this->api_endpoint_service = $api_endpoint_service;
|
|
||||||
$this->endpoint_repository = $endpoint_repository;
|
|
||||||
//set filters allowed values
|
|
||||||
$this->allowed_filter_fields = array('api_id');
|
|
||||||
$this->allowed_projection_fields = array('*');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$api_endpoint = $this->api_endpoint_service->get($id);
|
|
||||||
if(is_null($api_endpoint)){
|
|
||||||
return $this->error404(array('error' => 'api endpoint not found'));
|
|
||||||
}
|
|
||||||
$scopes = $api_endpoint->scopes()->get(array('id','name'));
|
|
||||||
$data = $api_endpoint->toArray();
|
|
||||||
$data['scopes'] = $scopes->toArray();
|
|
||||||
return $this->ok($data);
|
|
||||||
}
|
|
||||||
catch (EntityNotFoundException $ex1) {
|
|
||||||
$this->log_service->warning($ex1);
|
|
||||||
return $this->error404($ex1);
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
//check for optional filters param on querystring
|
|
||||||
$fields = $this->getProjection(Input::get('fields',null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields','limit','offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset',1));
|
|
||||||
$page_size = intval(Input::get('limit',10));
|
|
||||||
$list = $this->endpoint_repository->getAll($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = array();
|
|
||||||
|
|
||||||
foreach ($list->items() as $api_endpoint) {
|
|
||||||
array_push($items, $api_endpoint->toArray());
|
|
||||||
}
|
|
||||||
return $this->ok
|
|
||||||
(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (Exception $ex)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$new_api_endpoint = Input::all();
|
|
||||||
|
|
||||||
$rules = array(
|
|
||||||
'name' => 'required|alpha_dash|max:255',
|
|
||||||
'description' => 'required|freetext',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
'allow_cors' => 'required|boolean',
|
|
||||||
'route' => 'required|route',
|
|
||||||
'http_method' => 'required|httpmethod',
|
|
||||||
'api_id' => 'required|integer',
|
|
||||||
'rate_limit' => 'required|integer',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($new_api_endpoint, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_api_endpoint_model = $this->api_endpoint_service->add(
|
|
||||||
$new_api_endpoint['name'],
|
|
||||||
$new_api_endpoint['description'],
|
|
||||||
$new_api_endpoint['active'],
|
|
||||||
$new_api_endpoint['allow_cors'],
|
|
||||||
$new_api_endpoint['route'],
|
|
||||||
$new_api_endpoint['http_method'],
|
|
||||||
$new_api_endpoint['api_id'],
|
|
||||||
$new_api_endpoint['rate_limit']
|
|
||||||
);
|
|
||||||
return $this->created(array('api_endpoint_id' => $new_api_endpoint_model->id));
|
|
||||||
}
|
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error400(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$res = $this->api_endpoint_service->delete($id);
|
|
||||||
return $res?$this->deleted():$this->error404(array('error'=>'operation failed'));
|
|
||||||
}
|
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = array(
|
|
||||||
'id' => 'required|integer',
|
|
||||||
'name' => 'sometimes|required|alpha_dash|max:255',
|
|
||||||
'description' => 'sometimes|required|freetext',
|
|
||||||
'active' => 'sometimes|required|boolean',
|
|
||||||
'allow_cors' => 'sometimes|required|boolean',
|
|
||||||
'route' => 'sometimes|required|route',
|
|
||||||
'http_method' => 'sometimes|required|httpmethod',
|
|
||||||
'rate_limit' => 'sometimes|integer',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = $this->api_endpoint_service->update(intval($values['id']),$values);
|
|
||||||
|
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
|
||||||
}
|
|
||||||
catch(InvalidApiEndpoint $ex1){
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error400(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function activate($id){
|
public function activate($id){
|
||||||
try {
|
try {
|
||||||
$res = $this->api_endpoint_service->setStatus($id,true);
|
$endpoint = $this->service->update($id,['active'=>false]);
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($endpoint)->serialize());
|
||||||
}
|
}
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->error($ex1);
|
{
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deactivate($id){
|
public function deactivate($id){
|
||||||
try {
|
try {
|
||||||
$res = $this->api_endpoint_service->setStatus($id,false);
|
$endpoint = $this->service->update($id,['active'=>false]);
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($endpoint)->serialize());
|
||||||
}
|
}
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->error($ex1);
|
{
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addRequiredScope($id, $scope_id){
|
public function addRequiredScope($id, $scope_id){
|
||||||
try {
|
try {
|
||||||
$res = $this->api_endpoint_service->addRequiredScope($id,$scope_id);
|
$endpoint = $this->service->addRequiredScope($id, $scope_id);
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($endpoint)->serialize());
|
||||||
}
|
}
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->error($ex1);
|
{
|
||||||
return $this->error400(array('error'=>$ex1->getMessage()));
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
}
|
}
|
||||||
catch (InvalidApiScope $ex2) {
|
catch (EntityNotFoundException $ex2)
|
||||||
$this->log_service->error($ex2);
|
{
|
||||||
return $this->error400(array('error'=>$ex2->getMessage()));
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeRequiredScope($id, $scope_id){
|
public function removeRequiredScope($id, $scope_id){
|
||||||
try {
|
try {
|
||||||
$res = $this->api_endpoint_service->removeRequiredScope($id,$scope_id);
|
$endpoint = $this->service->removeRequiredScope($id,$scope_id);
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($endpoint)->serialize());
|
||||||
}
|
}
|
||||||
catch (InvalidApiEndpoint $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->error($ex1);
|
{
|
||||||
return $this->error400(array('error'=>$ex1->getMessage()));
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
}
|
}
|
||||||
catch (InvalidApiScope $ex2) {
|
catch (EntityNotFoundException $ex2)
|
||||||
$this->log_service->error($ex2);
|
{
|
||||||
return $this->error400(array('error'=>$ex2->getMessage()));
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getFilterRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['=@', '=='],
|
||||||
|
'http_method' => ['=@', '=='],
|
||||||
|
'route' => ['=@', '=='],
|
||||||
|
'active' => [ '=='],
|
||||||
|
'api_id' => ['=='],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterValidatorRules():array{
|
||||||
|
return [
|
||||||
|
'name' => 'sometimes|required|string',
|
||||||
|
'http_method'=> 'sometimes|required|string',
|
||||||
|
'route' => 'sometimes|required|string',
|
||||||
|
'active' => 'sometimes|required|boolean',
|
||||||
|
'api_id' => 'sometimes|required|integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'required|alpha_dash|max:255',
|
||||||
|
'description' => 'required|freetext',
|
||||||
|
'active' => 'required|boolean',
|
||||||
|
'allow_cors' => 'required|boolean',
|
||||||
|
'route' => 'required|route',
|
||||||
|
'http_method' => 'required|httpmethod',
|
||||||
|
'api_id' => 'required|integer',
|
||||||
|
'rate_limit' => 'required|integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'sometimes|required|alpha_dash|max:255',
|
||||||
|
'description' => 'sometimes|required|freetext',
|
||||||
|
'active' => 'sometimes|required|boolean',
|
||||||
|
'allow_cors' => 'sometimes|required|boolean',
|
||||||
|
'route' => 'sometimes|required|route',
|
||||||
|
'http_method' => 'sometimes|required|httpmethod',
|
||||||
|
'rate_limit' => 'sometimes|integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,30 +11,21 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
use OAuth2\Exceptions\InvalidResourceServer;
|
use App\Http\Controllers\APICRUDController;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
use OAuth2\Repositories\IResourceServerRepository;
|
use OAuth2\Repositories\IResourceServerRepository;
|
||||||
use OAuth2\Services\IResourceServerService;
|
use OAuth2\Services\IResourceServerService;
|
||||||
use Utils\Exceptions\EntityNotFoundException;
|
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use App\Http\Controllers\ICRUDController;
|
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
/**
|
/**
|
||||||
* Class ApiResourceServerController
|
* Class ApiResourceServerController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
*/
|
*/
|
||||||
class ApiResourceServerController extends AbstractRESTController implements ICRUDController
|
final class ApiResourceServerController extends APICRUDController
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var IResourceServerService $resource_service
|
|
||||||
*/
|
|
||||||
private $resource_server_service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IResourceServerRepository
|
|
||||||
*/
|
|
||||||
private $repository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ApiResourceServerController constructor.
|
* ApiResourceServerController constructor.
|
||||||
@ -49,185 +40,50 @@ class ApiResourceServerController extends AbstractRESTController implements ICRU
|
|||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($log_service);
|
parent::__construct($repository, $resource_server_service, $log_service);
|
||||||
$this->repository = $repository;
|
|
||||||
$this->resource_server_service = $resource_server_service;
|
|
||||||
$this->allowed_filter_fields = [''];
|
|
||||||
$this->allowed_projection_fields = ['*'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$resource_server = $this->repository->get($id);
|
|
||||||
if (is_null($resource_server)) {
|
|
||||||
return $this->error404(array('error' => 'resource server not found'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = $resource_server->toArray();
|
|
||||||
$apis = $resource_server->apis()->get(array('id', 'name'));
|
|
||||||
$data['apis'] = $apis->toArray();
|
|
||||||
$client = $resource_server->getClient();
|
|
||||||
|
|
||||||
if (!is_null($client)) {
|
|
||||||
$data['client_id'] = $client->getClientId();
|
|
||||||
$data['client_secret'] = $client->getClientSecret();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok($data);
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$fields = $this->getProjection(Input::get('fields', null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields', 'limit', 'offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset', 1));
|
|
||||||
$page_size = intval(Input::get('limit', 10));
|
|
||||||
|
|
||||||
$paginator = $this->repository->getAll($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = [];
|
|
||||||
|
|
||||||
foreach ($paginator->items() as $rs) {
|
|
||||||
$items[] = $rs->toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok([
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $paginator->total()
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = array(
|
|
||||||
'host' => 'required|host|max:255',
|
|
||||||
'ips' => 'required',
|
|
||||||
'friendly_name' => 'required|text|max:512',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
);
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_resource_server_model = $this->resource_server_service->add(
|
|
||||||
$values['host'],
|
|
||||||
$values['ips'],
|
|
||||||
$values['friendly_name'],
|
|
||||||
$values['active']);
|
|
||||||
|
|
||||||
return $this->created(array('resource_server_id' => $new_resource_server_model->id));
|
|
||||||
} catch (InvalidResourceServer $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
|
|
||||||
return $this->error400(array('error' => $ex1->getMessage()));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$this->resource_server_service->delete($id);
|
|
||||||
return $this->deleted();
|
|
||||||
}
|
|
||||||
catch (EntityNotFoundException $ex1) {
|
|
||||||
$this->log_service->warning($ex1);
|
|
||||||
return $this->error404(['message' => $ex1->getMessage()]);
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function regenerateClientSecret($id)
|
public function regenerateClientSecret($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$res = $this->resource_server_service->regenerateClientSecret($id);
|
$resource_server = $this->service->regenerateClientSecret($id);
|
||||||
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($resource_server->getClient())->serialize());
|
||||||
return !is_null($res) ? $this->ok(array('new_secret' => $res)) : $this->error404(array('error' => 'operation failed'));
|
|
||||||
}
|
}
|
||||||
catch (EntityNotFoundException $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->warning($ex1);
|
{
|
||||||
return $this->error404(['message' => $ex1->getMessage()]);
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = array(
|
|
||||||
'id' => 'required|integer',
|
|
||||||
'host' => 'sometimes|required|host|max:255',
|
|
||||||
'ips' => 'required',
|
|
||||||
'friendly_name' => 'sometimes|required|text|max:512',
|
|
||||||
);
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
$res = $this->resource_server_service->update(intval($values['id']), $values);
|
|
||||||
|
|
||||||
return $this->ok();
|
|
||||||
}
|
|
||||||
catch (EntityNotFoundException $ex1) {
|
|
||||||
$this->log_service->warning($ex1);
|
|
||||||
return $this->error404(['message' => $ex1->getMessage()]);
|
|
||||||
}
|
|
||||||
catch (InvalidResourceServer $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('message' => $ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function activate($id)
|
public function activate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->resource_server_service->setStatus($id, true);
|
$entity = $this->service->update($id, ['active' => true]);
|
||||||
return $this->ok();
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
}
|
}
|
||||||
catch (EntityNotFoundException $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->warning($ex1);
|
{
|
||||||
return $this->error404(['message' => $ex1->getMessage()]);
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,17 +91,48 @@ class ApiResourceServerController extends AbstractRESTController implements ICRU
|
|||||||
public function deactivate($id)
|
public function deactivate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->resource_server_service->setStatus($id, false);
|
|
||||||
|
|
||||||
return $this->ok();
|
$entity = $this->service->update($id, ['active' => false]);
|
||||||
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
}
|
}
|
||||||
catch (EntityNotFoundException $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->warning($ex1);
|
{
|
||||||
return $this->error404(['message' => $ex1->getMessage()]);
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'host' => 'sometimes|required|host|max:255',
|
||||||
|
'ips' => 'required',
|
||||||
|
'friendly_name' => 'sometimes|required|text|max:512',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'host' => 'required|host|max:255',
|
||||||
|
'ips' => 'required',
|
||||||
|
'friendly_name' => 'required|text|max:512',
|
||||||
|
'active' => 'required|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,31 +11,20 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use OAuth2\Repositories\IApiScopeRepository;
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
use Utils\Services\ILogService;
|
|
||||||
use OAuth2\Services\IApiScopeService;
|
|
||||||
use OAuth2\Exceptions\InvalidApi;
|
|
||||||
use OAuth2\Exceptions\InvalidApiScope;
|
|
||||||
use App\Http\Controllers\ICRUDController;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use OAuth2\Repositories\IApiScopeRepository;
|
||||||
|
use OAuth2\Services\IApiScopeService;
|
||||||
|
use Utils\Services\ILogService;
|
||||||
/**
|
/**
|
||||||
* Class ApiScopeController
|
* Class ApiScopeController
|
||||||
*/
|
*/
|
||||||
class ApiScopeController extends AbstractRESTController implements ICRUDController {
|
final class ApiScopeController extends APICRUDController
|
||||||
|
{
|
||||||
/**
|
|
||||||
* @var IApiScopeService
|
|
||||||
*/
|
|
||||||
private $api_scope_service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiScopeRepository
|
|
||||||
*/
|
|
||||||
private $scope_repository;
|
|
||||||
|
|
||||||
public function __construct
|
public function __construct
|
||||||
(
|
(
|
||||||
@ -44,187 +33,108 @@ class ApiScopeController extends AbstractRESTController implements ICRUDControll
|
|||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($log_service);
|
parent::__construct($scope_repository, $api_scope_service, $log_service);
|
||||||
$this->scope_repository = $scope_repository;
|
|
||||||
$this->api_scope_service = $api_scope_service;
|
|
||||||
//set filters allowed values
|
|
||||||
$this->allowed_filter_fields = array('api_id');
|
|
||||||
$this->allowed_projection_fields = array('*');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($id)
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function activate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$scope = $this->scope_repository->get($id);
|
$scope = $this->service->update($id, ['active' => true]);
|
||||||
if(is_null($scope)){
|
|
||||||
return $this->error404(array('error' => 'scope not found'));
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($scope)->serialize());
|
||||||
}
|
} catch (ValidationException $ex1) {
|
||||||
$data = $scope->toArray();
|
Log::warning($ex1);
|
||||||
return $this->ok($data);
|
return $this->error412(array($ex1->getMessage()));
|
||||||
|
} catch (EntityNotFoundException $ex2) {
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getByPage()
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function deactivate($id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
//check for optional filters param on querystring
|
$scope = $this->service->update($id, ['active' => false]);
|
||||||
$fields = $this->getProjection(Input::get('fields',null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields','limit','offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset',1));
|
|
||||||
$page_size = intval(Input::get('limit',10));
|
|
||||||
|
|
||||||
$list = $this->scope_repository->getAll($page_nbr, $page_size, $filters,$fields);
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($scope)->serialize());
|
||||||
$items = array();
|
} catch (ValidationException $ex1) {
|
||||||
|
Log::warning($ex1);
|
||||||
foreach ($list->items() as $scope)
|
return $this->error412(array($ex1->getMessage()));
|
||||||
{
|
} catch (EntityNotFoundException $ex2) {
|
||||||
array_push($items, $scope->toArray());
|
Log::warning($ex2);
|
||||||
}
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
|
|
||||||
return $this->ok
|
|
||||||
(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
{
|
{
|
||||||
try {
|
return [
|
||||||
|
'id' => 'required|integer',
|
||||||
$values = Input::all();
|
'name' => 'sometimes|required|scopename|max:512',
|
||||||
|
'description' => 'sometimes|required|freetext',
|
||||||
$rules = array(
|
'short_description' => 'sometimes|required|freetext|max:512',
|
||||||
'name' => 'required|scopename|max:512',
|
'active' => 'sometimes|required|boolean',
|
||||||
'short_description' => 'required|freetext|max:512',
|
'system' => 'sometimes|required|boolean',
|
||||||
'description' => 'required|freetext',
|
'default' => 'sometimes|required|boolean',
|
||||||
'active' => 'required|boolean',
|
'assigned_by_groups' => 'sometimes|boolean',
|
||||||
'default' => 'required|boolean',
|
];
|
||||||
'system' => 'required|boolean',
|
|
||||||
'api_id' => 'required|integer',
|
|
||||||
'assigned_by_groups' => 'required|boolean',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_scope = $this->api_scope_service->add(
|
|
||||||
$values['name'],
|
|
||||||
$values['short_description'],
|
|
||||||
$values['description'],
|
|
||||||
$values['active'],
|
|
||||||
$values['default'],
|
|
||||||
$values['system'],
|
|
||||||
$values['api_id'],
|
|
||||||
$values['assigned_by_groups']
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->created(array('scope_id' => $new_scope->id));
|
|
||||||
}
|
|
||||||
catch(InvalidApi $ex1){
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error' => $ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch(InvalidApiScope $ex2){
|
|
||||||
$this->log_service->error($ex2);
|
|
||||||
return $this->error400(array('error' => $ex2->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($id)
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
{
|
{
|
||||||
try {
|
return [
|
||||||
$res = $this->api_scope_service->delete($id);
|
'name' => 'required|scopename|max:512',
|
||||||
return $res?$this->deleted():$this->error404(array('error'=>'operation failed'));
|
'short_description' => 'required|freetext|max:512',
|
||||||
}
|
'description' => 'required|freetext',
|
||||||
catch(InvalidApiScope $ex1){
|
'active' => 'required|boolean',
|
||||||
$this->log_service->error($ex1);
|
'default' => 'required|boolean',
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
'system' => 'required|boolean',
|
||||||
}
|
'api_id' => 'required|integer',
|
||||||
catch (Exception $ex) {
|
'assigned_by_groups' => 'required|boolean',
|
||||||
$this->log_service->error($ex);
|
];
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update()
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterRules():array
|
||||||
{
|
{
|
||||||
try {
|
return [
|
||||||
|
'name' => ['=@', '=='],
|
||||||
$values = Input::all();
|
'is_assigned_by_groups' => ['=='],
|
||||||
|
'api_id' => ['=='],
|
||||||
$rules = array(
|
];
|
||||||
'id' => 'required|integer',
|
|
||||||
'name' => 'sometimes|required|scopename|max:512',
|
|
||||||
'description' => 'sometimes|required|freetext',
|
|
||||||
'short_description' => 'sometimes|required|freetext|max:512',
|
|
||||||
'active' => 'sometimes|required|boolean',
|
|
||||||
'system' => 'sometimes|required|boolean',
|
|
||||||
'default' => 'sometimes|required|boolean',
|
|
||||||
'assigned_by_groups' => 'sometimes|boolean',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error'=>'validation','messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = $this->api_scope_service->update(intval($values['id']),$values);
|
|
||||||
|
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(InvalidApiScope $ex1){
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error'=>$ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
public function activate($id){
|
* @return array
|
||||||
try {
|
*/
|
||||||
$res = $this->api_scope_service->setStatus($id,true);
|
protected function getFilterValidatorRules():array{
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
return [
|
||||||
} catch (Exception $ex) {
|
'name' => 'sometimes|required|string',
|
||||||
$this->log_service->error($ex);
|
'is_assigned_by_groups' => 'sometimes|required|boolean',
|
||||||
return $this->error500($ex);
|
'api_id' => 'sometimes|required|integer',
|
||||||
}
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deactivate($id){
|
|
||||||
try {
|
|
||||||
$res = $this->api_scope_service->setStatus($id,false);
|
|
||||||
return $res?$this->ok():$this->error400(array('error'=>'operation failed'));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,5 +1,4 @@
|
|||||||
<?php namespace App\Http\Controllers\Api;
|
<?php namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2015 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -12,248 +11,113 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use App\Http\Controllers\ICRUDController;
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
use Auth\Repositories\IUserRepository;
|
use Exception;
|
||||||
use OAuth2\Exceptions\InvalidApiScopeGroup;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
use OAuth2\Repositories\IApiScopeGroupRepository;
|
use OAuth2\Repositories\IApiScopeGroupRepository;
|
||||||
use OAuth2\Services\IApiScopeGroupService;
|
use OAuth2\Services\IApiScopeGroupService;
|
||||||
use OAuth2\Services\IApiScopeService;
|
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ApiScopeGroupController
|
* Class ApiScopeGroupController
|
||||||
* @package App\Http\Controllers
|
* @package App\Http\Controllers
|
||||||
*/
|
*/
|
||||||
final class ApiScopeGroupController extends AbstractRESTController implements ICRUDController
|
final class ApiScopeGroupController extends APICRUDController
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiScopeGroupRepository
|
|
||||||
*/
|
|
||||||
private $repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiScopeGroupService
|
|
||||||
*/
|
|
||||||
private $service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IUserRepository
|
|
||||||
*/
|
|
||||||
private $user_repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IApiScopeService
|
|
||||||
*/
|
|
||||||
private $scope_service;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ApiScopeGroupController constructor.
|
* ApiScopeGroupController constructor.
|
||||||
* @param IApiScopeGroupService $service
|
* @param IApiScopeGroupService $service
|
||||||
* @param IApiScopeGroupRepository $repository
|
* @param IApiScopeGroupRepository $repository
|
||||||
* @param IUserRepository $user_repository
|
|
||||||
* @param IApiScopeService $scope_service
|
|
||||||
* @param ILogService $log_service
|
* @param ILogService $log_service
|
||||||
*/
|
*/
|
||||||
public function __construct
|
public function __construct
|
||||||
(
|
(
|
||||||
IApiScopeGroupService $service,
|
IApiScopeGroupService $service,
|
||||||
IApiScopeGroupRepository $repository,
|
IApiScopeGroupRepository $repository,
|
||||||
IUserRepository $user_repository,
|
|
||||||
IApiScopeService $scope_service,
|
|
||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($log_service);
|
parent::__construct($repository, $service, $log_service);
|
||||||
|
|
||||||
$this->repository = $repository;
|
|
||||||
$this->user_repository = $user_repository;
|
|
||||||
$this->scope_service = $scope_service;
|
|
||||||
$this->service = $service;
|
|
||||||
$this->allowed_filter_fields = array('');
|
|
||||||
$this->allowed_projection_fields = array('*');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return mixed
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
*/
|
*/
|
||||||
public function get($id)
|
|
||||||
{
|
|
||||||
// TODO: Implement get() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = array
|
|
||||||
(
|
|
||||||
'name' => 'required|text|max:512',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
'scopes' => 'required',
|
|
||||||
'users' => 'required|user_ids',
|
|
||||||
);
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_group = $this->service->register
|
|
||||||
(
|
|
||||||
$values['name'],
|
|
||||||
$values['active'],
|
|
||||||
$values['scopes'],
|
|
||||||
$values['users']
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->created(array('group_id' => $new_group->id));
|
|
||||||
} catch (InvalidApiScopeGroup $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
|
|
||||||
return $this->error400(array('error' => $ex1->getMessage()));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$fields = $this->getProjection(Input::get('fields', null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields', 'limit', 'offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset', 1));
|
|
||||||
$page_size = intval(Input::get('limit', 10));
|
|
||||||
|
|
||||||
$list = $this->repository->getAll($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = array();
|
|
||||||
|
|
||||||
foreach ($list->items() as $g)
|
|
||||||
{
|
|
||||||
array_push($items, $g->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$group = $this->repository->get(intval($id));
|
|
||||||
if(is_null($group)) return $this->error404();
|
|
||||||
foreach($group->users()->get() as $user)
|
|
||||||
{
|
|
||||||
foreach($user->clients()->get() as $client)
|
|
||||||
{
|
|
||||||
foreach($group->scopes()->get() as $scope)
|
|
||||||
$client->scopes()->detach(intval($scope->id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->repository->delete($group);
|
|
||||||
return $this->deleted();
|
|
||||||
}
|
|
||||||
catch (Exception $ex)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function update()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = [
|
|
||||||
'id' => 'required|integer',
|
|
||||||
'name' => 'required|text|max:512',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
'scopes' => 'required',
|
|
||||||
'users' => 'required|user_ids',
|
|
||||||
];
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
|
|
||||||
return $this->error400(['error' => 'validation', 'messages' => $messages]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->service->update(intval($values['id']), $values);
|
|
||||||
|
|
||||||
return $this->ok();
|
|
||||||
}
|
|
||||||
catch (InvalidApiScopeGroup $ex1)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
return $this->error404(array('error' => $ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function activate($id){
|
public function activate($id){
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$this->service->setStatus($id, true);
|
$entity = $this->service->update($id, ['active' => true]);
|
||||||
return $this->ok();
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
public function deactivate($id){
|
public function deactivate($id){
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$this->service->setStatus($id, false);
|
$entity = $this->service->update($id, ['active' => false]);
|
||||||
return $this->ok();
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'required|text|max:512',
|
||||||
|
'active' => 'required|boolean',
|
||||||
|
'scopes' => 'required',
|
||||||
|
'users' => 'required|user_ids',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'required|text|max:512',
|
||||||
|
'active' => 'required|boolean',
|
||||||
|
'scopes' => 'required',
|
||||||
|
'users' => 'required|user_ids',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,15 +11,20 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use OAuth2\Services\IAsymmetricKeyService;
|
use OAuth2\Services\IAsymmetricKeyService;
|
||||||
use Utils\Exceptions\EntityNotFoundException;
|
use models\exceptions\EntityNotFoundException;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use OAuth2\Repositories\IAsymmetricKeyRepository;
|
use OAuth2\Repositories\IAsymmetricKeyRepository;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
/**
|
||||||
class AsymmetricKeyApiController extends AbstractRESTController
|
* Class AsymmetricKeyApiController
|
||||||
|
* @package App\Http\Controllers\Api
|
||||||
|
*/
|
||||||
|
abstract class AsymmetricKeyApiController extends APICRUDController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var IAsymmetricKeyService
|
* @var IAsymmetricKeyService
|
||||||
@ -41,98 +46,20 @@ class AsymmetricKeyApiController extends AbstractRESTController
|
|||||||
IAsymmetricKeyService $service,
|
IAsymmetricKeyService $service,
|
||||||
ILogService $log_service
|
ILogService $log_service
|
||||||
) {
|
) {
|
||||||
parent::__construct($log_service);
|
parent::__construct($repository, $service, $log_service);
|
||||||
$this->repository = $repository;
|
|
||||||
$this->service = $service;
|
|
||||||
//set filters allowed values
|
|
||||||
$this->allowed_filter_fields = array('*');
|
|
||||||
$this->allowed_projection_fields = array('*');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @return array
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
protected function _delete($id)
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
{
|
{
|
||||||
try {
|
return [
|
||||||
$res = $this->service->delete($id);
|
'id' => 'required|integer',
|
||||||
|
'active' => 'required|boolean',
|
||||||
return $res ? $this->deleted() : $this->error404(array('error' => 'operation failed'));
|
];
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function _update($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
|
|
||||||
$values = Input::all();
|
|
||||||
|
|
||||||
$rules = array(
|
|
||||||
'id' => 'required|integer',
|
|
||||||
'active' => 'required|boolean',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Creates a Validator instance and validates the data.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails()) {
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->service->update(intval($id), $values);
|
|
||||||
|
|
||||||
return $this->ok();
|
|
||||||
|
|
||||||
} catch (EntityNotFoundException $ex1) {
|
|
||||||
$this->log_service->error($ex1);
|
|
||||||
|
|
||||||
return $this->error404(array('error' => $ex1->getMessage()));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected function _getByPage()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
//check for optional filters param on querystring
|
|
||||||
$fields = $this->getProjection(Input::get('fields', null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields', 'limit', 'offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset', 1));
|
|
||||||
$page_size = intval(Input::get('limit', 10));
|
|
||||||
|
|
||||||
$list = $this->repository->getAll($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = array();
|
|
||||||
foreach ($list->items() as $private_key) {
|
|
||||||
$data = $private_key->toArray();
|
|
||||||
$data['sha_256'] = $private_key->getSHA_256_Thumbprint();
|
|
||||||
array_push($items, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok
|
|
||||||
(
|
|
||||||
array
|
|
||||||
(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
@ -12,14 +12,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use OAuth2\Services\IClientPublicKeyService;
|
use OAuth2\Services\IClientPublicKeyService;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use OAuth2\Repositories\IClientPublicKeyRepository;
|
use OAuth2\Repositories\IClientPublicKeyRepository;
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
use Services\Exceptions\ValidationException;
|
|
||||||
/**
|
/**
|
||||||
* Class ClientPublicKeyApiController
|
* Class ClientPublicKeyApiController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
@ -41,29 +37,50 @@ final class ClientPublicKeyApiController extends AsymmetricKeyApiController
|
|||||||
parent::__construct($repository, $service, $log_service);
|
parent::__construct($repository, $service, $log_service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @return array
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public function get($id)
|
protected function getCreatePayload():array{
|
||||||
{
|
$payload = Input::All();
|
||||||
return $this->error404();
|
return array_merge($payload, $this->extra_create_payload_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private $extra_create_payload_params = [];
|
||||||
/**
|
/**
|
||||||
* @param int $client_id
|
* @param int $client_id
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function create($client_id)
|
public function _create($client_id)
|
||||||
{
|
{
|
||||||
try
|
$this->extra_create_payload_params['client_id'] = $client_id;
|
||||||
{
|
return $this->create();
|
||||||
|
}
|
||||||
|
|
||||||
$values = Input::All();
|
/**
|
||||||
$values['client_id'] = $client_id;
|
* @param int $client_id
|
||||||
// Build the validation constraint set.
|
* @param int $public_key_id
|
||||||
$rules = array(
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function _update($client_id, $public_key_id)
|
||||||
|
{
|
||||||
|
return $this->update($public_key_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $client_id
|
||||||
|
* @param int $public_key_id
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function _delete($client_id, $public_key_id){
|
||||||
|
return $this->delete($public_key_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
'client_id' => 'required|integer',
|
'client_id' => 'required|integer',
|
||||||
'kid' => 'required|text|max:255',
|
'kid' => 'required|text|max:255',
|
||||||
'active' => 'required|boolean',
|
'active' => 'required|boolean',
|
||||||
@ -73,89 +90,6 @@ final class ClientPublicKeyApiController extends AsymmetricKeyApiController
|
|||||||
'usage' => 'required|public_key_usage',
|
'usage' => 'required|public_key_usage',
|
||||||
'type' => 'required|public_key_type',
|
'type' => 'required|public_key_type',
|
||||||
'alg' => 'required|key_alg:usage',
|
'alg' => 'required|key_alg:usage',
|
||||||
);
|
];
|
||||||
|
|
||||||
// Create a new validator instance.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails())
|
|
||||||
{
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$public_key = $this->service->register($values);
|
|
||||||
|
|
||||||
return $this->created(array('id' => $public_key->getId()));
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(ValidationException $ex1)
|
|
||||||
{
|
|
||||||
return $this->error400(array('error' => $ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getByPage($client_id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
//check for optional filters param on querystring
|
|
||||||
$fields = $this->getProjection(Input::get('fields', null));
|
|
||||||
$filters = $this->getFilters(Input::except('fields', 'limit', 'offset'));
|
|
||||||
$page_nbr = intval(Input::get('offset', 1));
|
|
||||||
$page_size = intval(Input::get('limit', 10));
|
|
||||||
array_push($filters, array
|
|
||||||
(
|
|
||||||
'name' => 'oauth2_client_id',
|
|
||||||
'op' => '=',
|
|
||||||
'value' => $client_id
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$list = $this->repository->getAll($page_nbr, $page_size, $filters, $fields);
|
|
||||||
$items = array();
|
|
||||||
foreach ($list->items() as $private_key) {
|
|
||||||
$data = $private_key->toArray();
|
|
||||||
$data['sha_256'] = $private_key->getSHA_256_Thumbprint();
|
|
||||||
array_push($items, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->ok(array(
|
|
||||||
'page' => $items,
|
|
||||||
'total_items' => $list->total()
|
|
||||||
));
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $client_id
|
|
||||||
* @param int $public_key_id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function update($client_id, $public_key_id)
|
|
||||||
{
|
|
||||||
return $this->_update($public_key_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $client_id
|
|
||||||
* @param int $public_key_id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function delete($client_id, $public_key_id){
|
|
||||||
return $this->_delete($public_key_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
258
app/Http/Controllers/Api/GroupApiController.php
Normal file
258
app/Http/Controllers/Api/GroupApiController.php
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Api;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
|
use App\Http\Utils\PagingConstants;
|
||||||
|
use App\libs\Auth\Repositories\IGroupRepository;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use App\Services\Auth\IGroupService;
|
||||||
|
use Auth\Repositories\IUserRepository;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Support\Facades\Input;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use utils\Filter;
|
||||||
|
use utils\FilterElement;
|
||||||
|
use utils\FilterParser;
|
||||||
|
use utils\OrderParser;
|
||||||
|
use Utils\Services\ILogService;
|
||||||
|
use utils\PagingInfo;
|
||||||
|
use Exception;
|
||||||
|
/**
|
||||||
|
* Class GroupApiController
|
||||||
|
* @package App\Http\Controllers\Api
|
||||||
|
*/
|
||||||
|
final class GroupApiController extends APICRUDController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IUserRepository
|
||||||
|
*/
|
||||||
|
private $user_repository;
|
||||||
|
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IGroupRepository $repository,
|
||||||
|
IUserRepository $user_repository,
|
||||||
|
IGroupService $service,
|
||||||
|
ILogService $log_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
parent::__construct($repository, $service, $log_service);
|
||||||
|
$this->user_repository = $user_repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['=@', '=='],
|
||||||
|
'slug' => ['=@', '=='],
|
||||||
|
'active' => [ '=='],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterValidatorRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'sometimes|required|string',
|
||||||
|
'slug' => 'sometimes|required|string',
|
||||||
|
'active' => 'sometimes|required|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'sometimes|required|string|max:512',
|
||||||
|
'slug' => 'sometimes|alpha_dash|string|max:254',
|
||||||
|
'active' => 'sometimes|required|boolean',
|
||||||
|
'default' => 'sometimes|required|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => 'required|string|max:512',
|
||||||
|
'slug' => 'required|alpha_dash|max:254',
|
||||||
|
'active' => 'required|boolean',
|
||||||
|
'default' => 'required|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $group_id
|
||||||
|
* @param $user_id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function addUserToGroup($group_id, $user_id){
|
||||||
|
try {
|
||||||
|
$group = $this->repository->getById($group_id);
|
||||||
|
if(is_null($group))
|
||||||
|
return $this->error404();
|
||||||
|
$this->service->addUser2Group($group, $user_id);
|
||||||
|
return $this->updated();
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $group_id
|
||||||
|
* @param $user_id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function removeUserFromGroup($group_id, $user_id){
|
||||||
|
try {
|
||||||
|
$group = $this->repository->getById($group_id);
|
||||||
|
if(is_null($group))
|
||||||
|
return $this->error404();
|
||||||
|
$this->service->removeUserFromGroup($group, $user_id);
|
||||||
|
return $this->deleted();
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $group_id
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function getUsersFromGroup($group_id)
|
||||||
|
{
|
||||||
|
$values = Input::all();
|
||||||
|
$rules = [
|
||||||
|
|
||||||
|
'page' => 'integer|min:1',
|
||||||
|
'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize),
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$validation = Validator::make($values, $rules);
|
||||||
|
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$ex = new ValidationException();
|
||||||
|
throw $ex->setMessages($validation->messages()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
// default values
|
||||||
|
$page = 1;
|
||||||
|
$per_page = PagingConstants::DefaultPageSize;;
|
||||||
|
|
||||||
|
if (Input::has('page')) {
|
||||||
|
$page = intval(Input::get('page'));
|
||||||
|
$per_page = intval(Input::get('per_page'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$filter = null;
|
||||||
|
|
||||||
|
if (Input::has('filter')) {
|
||||||
|
$filter = FilterParser::parse(Input::get('filter'), [
|
||||||
|
'first_name' => ['=@', '=='],
|
||||||
|
'last_name' => ['=@', '=='],
|
||||||
|
'email' => ['=@', '=='],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_null($filter)) $filter = new Filter();
|
||||||
|
|
||||||
|
$filter_validator_rules = [
|
||||||
|
'first_name' => 'nullable|string',
|
||||||
|
'last_name' => 'nullable|string',
|
||||||
|
'email' => 'nullable|string',
|
||||||
|
];
|
||||||
|
|
||||||
|
if(count($filter_validator_rules)) {
|
||||||
|
$filter->validate($filter_validator_rules);
|
||||||
|
}
|
||||||
|
|
||||||
|
$order = null;
|
||||||
|
|
||||||
|
if (Input::has('order'))
|
||||||
|
{
|
||||||
|
$order = OrderParser::parse(Input::get('order'), [
|
||||||
|
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$filter->addFilterCondition(FilterElement::makeEqual("group_id", $group_id));
|
||||||
|
|
||||||
|
$data = $this->user_repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
|
||||||
|
|
||||||
|
return $this->ok
|
||||||
|
(
|
||||||
|
$data->toArray
|
||||||
|
(
|
||||||
|
Input::get('expand', ''),
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
SerializerRegistry::SerializerType_Private
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,35 +0,0 @@
|
|||||||
<?php namespace App\Http\Controllers;
|
|
||||||
/**
|
|
||||||
* Interface ICRUDController
|
|
||||||
* @package App\Http\Controllers
|
|
||||||
*/
|
|
||||||
interface ICRUDController {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get($id);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function create();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getByPage();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function delete($id);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function update();
|
|
||||||
|
|
||||||
}
|
|
@ -43,12 +43,13 @@ abstract class JsonController extends Controller {
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updated($data='ok')
|
protected function updated($data = 'ok', $has_content = true)
|
||||||
{
|
{
|
||||||
$res = Response::json($data, 204);
|
$res = Response::json($data, $has_content ? 201 : 204);
|
||||||
//jsonp
|
//jsonp
|
||||||
if(Input::has('callback'))
|
if (Input::has('callback')) {
|
||||||
$res->setCallback(Input::get('callback'));
|
$res->setCallback(Input::get('callback'));
|
||||||
|
}
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ abstract class JsonController extends Controller {
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function error400($data){
|
protected function error400($data = ['message' => 'Bad Request']){
|
||||||
return Response::json($data, 400);
|
return Response::json($data, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,11 +11,9 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use OAuth2\IResourceServerContext;
|
use OAuth2\IResourceServerContext;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
use App\Http\Controllers\Api\JsonController;
|
use App\Http\Controllers\Api\JsonController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OAuth2ProtectedController
|
* Class OAuth2ProtectedController
|
||||||
* @package App\Http\Controllers\Api\OAuth2
|
* @package App\Http\Controllers\Api\OAuth2
|
||||||
|
@ -11,21 +11,58 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
use App\Http\Controllers\GetAllTrait;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use Auth\Repositories\IUserRepository;
|
||||||
use Illuminate\Support\Facades\Response;
|
use Illuminate\Support\Facades\Response;
|
||||||
use OAuth2\Builders\IdTokenBuilder;
|
use OAuth2\Builders\IdTokenBuilder;
|
||||||
use OAuth2\IResourceServerContext;
|
use OAuth2\IResourceServerContext;
|
||||||
use OAuth2\Repositories\IClientRepository;
|
use OAuth2\Repositories\IClientRepository;
|
||||||
use OAuth2\ResourceServer\IUserService;
|
use OAuth2\ResourceServer\IUserService;
|
||||||
use OAuth2\Services\IClientService;
|
|
||||||
use Utils\Http\HttpContentType;
|
use Utils\Http\HttpContentType;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
|
use Exception;
|
||||||
/**
|
/**
|
||||||
* Class OAuth2UserApiController
|
* Class OAuth2UserApiController
|
||||||
* @package App\Http\Controllers\Api\OAuth2
|
* @package App\Http\Controllers\Api\OAuth2
|
||||||
*/
|
*/
|
||||||
class OAuth2UserApiController extends OAuth2ProtectedController
|
final class OAuth2UserApiController extends OAuth2ProtectedController
|
||||||
{
|
{
|
||||||
|
use GetAllTrait;
|
||||||
|
|
||||||
|
protected function getAllSerializerType():string{
|
||||||
|
return SerializerRegistry::SerializerType_Private;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => ['=@', '=='],
|
||||||
|
'last_name' => ['=@', '=='],
|
||||||
|
'email' => ['=@', '=='],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOrderRules():array{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterValidatorRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => 'sometimes|required|string',
|
||||||
|
'last_name' => 'sometimes|required|string',
|
||||||
|
'email' => 'sometimes|required|string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var IUserService
|
* @var IUserService
|
||||||
*/
|
*/
|
||||||
@ -42,6 +79,7 @@ class OAuth2UserApiController extends OAuth2ProtectedController
|
|||||||
private $id_token_builder;
|
private $id_token_builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param IUserRepository $repository
|
||||||
* @param IUserService $user_service
|
* @param IUserService $user_service
|
||||||
* @param IResourceServerContext $resource_server_context
|
* @param IResourceServerContext $resource_server_context
|
||||||
* @param ILogService $log_service
|
* @param ILogService $log_service
|
||||||
@ -50,6 +88,7 @@ class OAuth2UserApiController extends OAuth2ProtectedController
|
|||||||
*/
|
*/
|
||||||
public function __construct
|
public function __construct
|
||||||
(
|
(
|
||||||
|
IUserRepository $repository,
|
||||||
IUserService $user_service,
|
IUserService $user_service,
|
||||||
IResourceServerContext $resource_server_context,
|
IResourceServerContext $resource_server_context,
|
||||||
ILogService $log_service,
|
ILogService $log_service,
|
||||||
@ -58,7 +97,7 @@ class OAuth2UserApiController extends OAuth2ProtectedController
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($resource_server_context, $log_service);
|
parent::__construct($resource_server_context, $log_service);
|
||||||
|
$this->repository = $repository;
|
||||||
$this->user_service = $user_service;
|
$this->user_service = $user_service;
|
||||||
$this->client_repository = $client_repository;
|
$this->client_repository = $client_repository;
|
||||||
$this->id_token_builder = $id_token_builder;
|
$this->id_token_builder = $id_token_builder;
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Api\OAuth2;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Illuminate\Support\Facades\Input;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Request;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use OAuth2\IResourceServerContext;
|
||||||
|
use Utils\Services\ILogService;
|
||||||
|
/**
|
||||||
|
* Class OAuth2UserRegistrationRequestApiController
|
||||||
|
* @package App\Http\Controllers\Api\OAuth2
|
||||||
|
*/
|
||||||
|
final class OAuth2UserRegistrationRequestApiController extends OAuth2ProtectedController
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param IUserService $user_service
|
||||||
|
* @param IResourceServerContext $resource_server_context
|
||||||
|
* @param ILogService $log_service
|
||||||
|
*/
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IUserService $user_service,
|
||||||
|
IResourceServerContext $resource_server_context,
|
||||||
|
ILogService $log_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
parent::__construct($resource_server_context, $log_service);
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(){
|
||||||
|
try {
|
||||||
|
|
||||||
|
if(!Request::isJson()) return $this->error400();
|
||||||
|
$payload = Input::json()->all();
|
||||||
|
|
||||||
|
// Creates a Validator instance and validates the data.
|
||||||
|
$validation = Validator::make($payload, [
|
||||||
|
'first_name' => 'required|string|max:255',
|
||||||
|
'last_name' => 'required|string|max:255',
|
||||||
|
'email' => 'required|string|email|max:255',
|
||||||
|
'country' => 'sometimes|required|string|country_iso_alpha2_code',
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$messages = $validation->messages()->toArray();
|
||||||
|
|
||||||
|
return $this->error412
|
||||||
|
(
|
||||||
|
$messages
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$registration_request = $this->user_service->createRegistrationRequest
|
||||||
|
(
|
||||||
|
$this->resource_server_context->getCurrentClientId(),
|
||||||
|
$payload
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($registration_request)->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1) {
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412([$ex1->getMessage()]);
|
||||||
|
}
|
||||||
|
catch(EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message'=> $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (\Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,15 +11,9 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
use OAuth2\Repositories\IServerPrivateKeyRepository;
|
use OAuth2\Repositories\IServerPrivateKeyRepository;
|
||||||
use OAuth2\Services\IServerPrivateKeyService;
|
use OAuth2\Services\IServerPrivateKeyService;
|
||||||
use Services\Exceptions\ValidationException;
|
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ServerPrivateKeyApiController
|
* Class ServerPrivateKeyApiController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
@ -42,74 +36,20 @@ final class ServerPrivateKeyApiController extends AsymmetricKeyApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function create()
|
protected function getCreatePayloadValidationRules(): array
|
||||||
{
|
{
|
||||||
try
|
return [
|
||||||
{
|
'kid' => 'required|text|min:5|max:255',
|
||||||
|
'active' => 'required|boolean',
|
||||||
$values = Input::All();
|
'valid_from' => 'date_format:m/d/Y',
|
||||||
// Build the validation constraint set.
|
'valid_to' => 'date_format:m/d/Y|after:valid_from',
|
||||||
$rules = array(
|
'pem_content' => 'sometimes|required|private_key_pem:password|private_key_pem_length:password',
|
||||||
'kid' => 'required|text|min:5|max:255',
|
'usage' => 'required|public_key_usage',
|
||||||
'active' => 'required|boolean',
|
'type' => 'required|public_key_type',
|
||||||
'valid_from' => 'date_format:m/d/Y',
|
'alg' => 'required|key_alg:usage',
|
||||||
'valid_to' => 'date_format:m/d/Y|after:valid_from',
|
'password' => 'min:5|max:255|private_key_password:pem_content',
|
||||||
'pem_content' => 'sometimes|required|private_key_pem:password|private_key_pem_length:password',
|
];
|
||||||
'usage' => 'required|public_key_usage',
|
|
||||||
'type' => 'required|public_key_type',
|
|
||||||
'alg' => 'required|key_alg:usage',
|
|
||||||
'password' => 'min:5|max:255|private_key_password:pem_content',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create a new validator instance.
|
|
||||||
$validation = Validator::make($values, $rules);
|
|
||||||
|
|
||||||
if ($validation->fails())
|
|
||||||
{
|
|
||||||
$messages = $validation->messages()->toArray();
|
|
||||||
return $this->error400(array('error' => 'validation', 'messages' => $messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
$private_key = $this->service->register($values);
|
|
||||||
|
|
||||||
return $this->created(array('id' => $private_key->getId()));
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(ValidationException $ex1)
|
|
||||||
{
|
|
||||||
return $this->error400(array('error' => $ex1->getMessage()));
|
|
||||||
}
|
|
||||||
catch (Exception $ex)
|
|
||||||
{
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
return $this->_getByPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function update($id)
|
|
||||||
{
|
|
||||||
return $this->_update($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $id
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
return $this->_delete($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -11,37 +11,30 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
use App\Http\Controllers\APICRUDController;
|
||||||
use App\Http\Controllers\ICRUDController;
|
use App\Http\Utils\HTMLCleaner;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
use Auth\Repositories\IUserRepository;
|
use Auth\Repositories\IUserRepository;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use OAuth2\Exceptions\ExpiredAccessTokenException;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
use OAuth2\Services\ITokenService;
|
use OAuth2\Services\ITokenService;
|
||||||
use OpenId\Services\IUserService;
|
use OpenId\Services\IUserService;
|
||||||
use Utils\Exceptions\EntityNotFoundException;
|
use models\exceptions\EntityNotFoundException;
|
||||||
use Utils\Services\ILogService;
|
use Utils\Services\ILogService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class UserApiController
|
* Class UserApiController
|
||||||
* @package App\Http\Controllers\Api
|
* @package App\Http\Controllers\Api
|
||||||
*/
|
*/
|
||||||
class UserApiController extends AbstractRESTController implements ICRUDController {
|
final class UserApiController extends APICRUDController {
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IUserService
|
|
||||||
*/
|
|
||||||
private $user_service;
|
|
||||||
/**
|
/**
|
||||||
* @var ITokenService
|
* @var ITokenService
|
||||||
*/
|
*/
|
||||||
private $token_service;
|
private $token_service;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IUserRepository
|
|
||||||
*/
|
|
||||||
private $user_repository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UserApiController constructor.
|
* UserApiController constructor.
|
||||||
* @param IUserRepository $user_repository
|
* @param IUserRepository $user_repository
|
||||||
@ -56,135 +49,214 @@ class UserApiController extends AbstractRESTController implements ICRUDControlle
|
|||||||
IUserService $user_service,
|
IUserService $user_service,
|
||||||
ITokenService $token_service
|
ITokenService $token_service
|
||||||
){
|
){
|
||||||
parent::__construct($log_service);
|
parent::__construct($user_repository, $user_service, $log_service);
|
||||||
|
$this->token_service = $token_service;
|
||||||
$this->user_service = $user_service;
|
|
||||||
$this->token_service = $token_service;
|
|
||||||
$this->user_repository = $user_repository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => ['=@', '=='],
|
||||||
|
'last_name' => ['=@', '=='],
|
||||||
|
'email' => ['=@', '=='],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterValidatorRules():array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => 'nullable|string',
|
||||||
|
'last_name' => 'nullable|string',
|
||||||
|
'email' => 'nullable|string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function unlock($id){
|
public function unlock($id){
|
||||||
try {
|
try {
|
||||||
$this->user_service->unlockUser($id);
|
$entity = $this->service->unlockUser($id);
|
||||||
return $this->updated();
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
}
|
}
|
||||||
catch (EntityNotFoundException $ex1) {
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->error($ex1);
|
{
|
||||||
return $this->error404(array('error' => $ex1->getMessage()));
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array($ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
}
|
}
|
||||||
catch (Exception $ex) {
|
catch (Exception $ex) {
|
||||||
$this->log_service->error($ex);
|
Log::error($ex);
|
||||||
return $this->error500($ex);
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function lock($id){
|
||||||
|
try {
|
||||||
|
$entity = $this->service->lockUser($id);
|
||||||
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412(array($ex1->getMessage()));
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAllSerializerType():string{
|
||||||
|
return SerializerRegistry::SerializerType_Private;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function revokeToken($id,$value){
|
public function revokeMyToken($value){
|
||||||
|
|
||||||
try{
|
try{
|
||||||
$hint = Input::get('hint','none');
|
$hint = Input::get('hint','none');
|
||||||
|
|
||||||
switch($hint){
|
switch($hint){
|
||||||
case 'access-token':{
|
case 'access-token':{
|
||||||
$token = $this->token_service->getAccessToken($value,true);
|
|
||||||
if(is_null($token))
|
|
||||||
throw new Exception(sprintf("access token %s expired!.",$value));
|
|
||||||
if(is_null($token->getUserId()) || intval($token->getUserId())!=intval($id))
|
|
||||||
throw new Exception(sprintf("access token %s does not belongs to user id %s!.",$value,$id));
|
|
||||||
$this->token_service->revokeAccessToken($value,true);
|
$this->token_service->revokeAccessToken($value,true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'refresh-token':
|
case 'refresh-token':
|
||||||
$token = $this->token_service->getRefreshToken($value,true);
|
|
||||||
if(is_null($token))
|
|
||||||
throw new Exception(sprintf("access token %s expired!.",$value));
|
|
||||||
if(is_null($token->getUserId()) || intval($token->getUserId())!=intval($id))
|
|
||||||
throw new Exception(sprintf("refresh token %s does not belongs to user id %s!.",$value,$id));
|
|
||||||
$this->token_service->revokeRefreshToken($value,true);
|
$this->token_service->revokeRefreshToken($value,true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Exception(sprintf("hint %s not allowed",$hint));
|
throw new Exception(sprintf("hint %s not allowed",$hint));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return $this->ok();
|
return $this->deleted();
|
||||||
}
|
}
|
||||||
catch(ExpiredAccessTokenException $ex1){
|
catch (ValidationException $ex1)
|
||||||
$this->log_service->warning($ex1);
|
|
||||||
return $this->error404();
|
|
||||||
}
|
|
||||||
catch(Exception $ex){
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function get($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$user = $this->user_repository->get($id);
|
|
||||||
if(is_null($user)){
|
|
||||||
return $this->error404(array('error' => 'user not found'));
|
|
||||||
}
|
|
||||||
$data = $user->toArray();
|
|
||||||
return $this->ok($data);
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
$this->log_service->error($ex);
|
|
||||||
return $this->error500($ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
// TODO: Implement create() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getByPage()
|
|
||||||
{
|
|
||||||
// TODO: Implement getByPage() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
// TODO: Implement delete() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update()
|
|
||||||
{
|
|
||||||
// TODO: Implement update() method.
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fetch()
|
|
||||||
{
|
|
||||||
$values = Input::all();
|
|
||||||
if(!isset($values['t'])) return $this->error404();
|
|
||||||
|
|
||||||
$term = $values['t'];
|
|
||||||
$users = $this->user_repository->getByEmailOrName($term);
|
|
||||||
$list = array();
|
|
||||||
|
|
||||||
if(count($users) > 0)
|
|
||||||
{
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
foreach($users as $u)
|
return $this->error412(array( $ex1->getMessage()));
|
||||||
{
|
}
|
||||||
array_push($list, array
|
catch (EntityNotFoundException $ex2)
|
||||||
(
|
{
|
||||||
'id' => $u->id,
|
Log::warning($ex2);
|
||||||
'value' => sprintf('%s', $u->getFullName())
|
return $this->error404(array('message' => $ex2->getMessage()));
|
||||||
)
|
}
|
||||||
);
|
catch (Exception $ex) {
|
||||||
}
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
}
|
}
|
||||||
return $this->ok($list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUpdatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => 'required|string',
|
||||||
|
'last_name' => 'required|string',
|
||||||
|
'email' => 'required|email',
|
||||||
|
'identifier' => 'sometimes|string',
|
||||||
|
'bio' => 'nullable|string',
|
||||||
|
'address1' => 'nullable|string',
|
||||||
|
'address2' => 'nullable|string',
|
||||||
|
'city' => 'nullable|string',
|
||||||
|
'state' => 'nullable|string',
|
||||||
|
'post_code' => 'nullable|string',
|
||||||
|
'country_iso_code' => 'nullable|country_iso_alpha2_code',
|
||||||
|
'second_email' => 'nullable|email',
|
||||||
|
'third_email' => 'nullable|email',
|
||||||
|
'gender' => 'nullable|string',
|
||||||
|
'gender_specify' => 'nullable|string',
|
||||||
|
'statement_of_interest' => 'nullable|string',
|
||||||
|
'irc' => 'nullable|string',
|
||||||
|
'linked_in_profile' => 'nullable|string',
|
||||||
|
'github_user' => 'nullable|string',
|
||||||
|
'wechat_user' => 'nullable|string',
|
||||||
|
'twitter_name' => 'nullable|string',
|
||||||
|
'language' => 'nullable|string',
|
||||||
|
'birthday' => 'nullable|date_format:U',
|
||||||
|
'password' => 'sometimes|string|min:8|confirmed',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function curateUpdatePayload(array $payload):array {
|
||||||
|
return HTMLCleaner::cleanData($payload, [
|
||||||
|
'bio', 'statement_of_interest'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function curateCreatePayload(array $payload):array {
|
||||||
|
return HTMLCleaner::cleanData($payload, [
|
||||||
|
'bio', 'statement_of_interest'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getCreatePayloadValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'first_name' => 'required|string',
|
||||||
|
'last_name' => 'required|string',
|
||||||
|
'email' => 'required|email',
|
||||||
|
'identifier' => 'sometimes|string',
|
||||||
|
'bio' => 'nullable|string',
|
||||||
|
'address1' => 'nullable|string',
|
||||||
|
'address2' => 'nullable|string',
|
||||||
|
'city' => 'nullable|string',
|
||||||
|
'state' => 'nullable|string',
|
||||||
|
'post_code' => 'nullable|string',
|
||||||
|
'country_iso_code' => 'nullable|country_iso_alpha2_code',
|
||||||
|
'second_email' => 'nullable|email',
|
||||||
|
'third_email' => 'nullable|email',
|
||||||
|
'gender' => 'nullable|string',
|
||||||
|
'statement_of_interest' => 'nullable|string',
|
||||||
|
'irc' => 'nullable|string',
|
||||||
|
'linked_in_profile' => 'nullable|string',
|
||||||
|
'github_user' => 'nullable|string',
|
||||||
|
'wechat_user' => 'nullable|string',
|
||||||
|
'twitter_name' => 'nullable|string',
|
||||||
|
'language' => 'nullable|string',
|
||||||
|
'birthday' => 'nullable|date_format:U',
|
||||||
|
'password' => 'sometimes|string|min:8|confirmed',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Http\JsonResponse|mixed
|
||||||
|
*/
|
||||||
|
public function updateMe(){
|
||||||
|
if(!Auth::check())
|
||||||
|
return $this->error403();
|
||||||
|
$myId = Auth::user()->getId();
|
||||||
|
return $this->update($myId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
104
app/Http/Controllers/Auth/EmailVerificationController.php
Normal file
104
app/Http/Controllers/Auth/EmailVerificationController.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Redirect;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Http\Request as LaravelRequest;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
/**
|
||||||
|
* Class EmailVerificationController
|
||||||
|
* @package App\Http\Controllers\Auth
|
||||||
|
*/
|
||||||
|
final class EmailVerificationController extends Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EmailVerificationController constructor.
|
||||||
|
* @param IUserService $user_service
|
||||||
|
*/
|
||||||
|
public function __construct(IUserService $user_service)
|
||||||
|
{
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showVerificationForm()
|
||||||
|
{
|
||||||
|
return view('auth.email_verification');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $token
|
||||||
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||||
|
*/
|
||||||
|
public function verify($token)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$user = $this->user_service->verifyEmail($token);
|
||||||
|
return view('auth.email_verification_success', ['user' => $user]);
|
||||||
|
}
|
||||||
|
catch (\Exception $ex){
|
||||||
|
Log::error($ex);
|
||||||
|
}
|
||||||
|
return view('auth.email_verification_error');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validator for an incoming registration request.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected function validator(array $data)
|
||||||
|
{
|
||||||
|
return Validator::make($data, [
|
||||||
|
'email' => 'required|string|email|max:255',
|
||||||
|
'g-recaptcha-response' => 'required|recaptcha',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resend(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$payload = $request->all();
|
||||||
|
$validator = $this->validator($payload);
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
return Redirect::action('Auth\EmailVerificationController@showVerificationForm')->withErrors($validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $this->user_service->resendVerificationEmail($payload);
|
||||||
|
|
||||||
|
return view("auth.email_verification_resend_success", ['user' => $user]);
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
foreach ($ex->getMessages() as $message){
|
||||||
|
$validator->getMessageBag()->add('validation', $message);
|
||||||
|
}
|
||||||
|
return Redirect::action('Auth\EmailVerificationController@showVerificationForm')->withErrors($validator);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::error($ex);
|
||||||
|
}
|
||||||
|
return view("auth.email_verification_error");
|
||||||
|
}
|
||||||
|
}
|
126
app/Http/Controllers/Auth/ForgotPasswordController.php
Normal file
126
app/Http/Controllers/Auth/ForgotPasswordController.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Redirect;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Http\Request as LaravelRequest;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
/**
|
||||||
|
* Class ForgotPasswordController
|
||||||
|
* @package App\Http\Controllers\Auth
|
||||||
|
*/
|
||||||
|
final class ForgotPasswordController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ForgotPasswordController constructor.
|
||||||
|
* @param IUserService $user_service
|
||||||
|
*/
|
||||||
|
public function __construct(IUserService $user_service)
|
||||||
|
{
|
||||||
|
$this->middleware('guest');
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the form to request a password reset link.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function showLinkRequestForm()
|
||||||
|
{
|
||||||
|
return view('auth.passwords.email');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a reset link to the given user.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function sendResetLinkEmail(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$payload = $request->all();
|
||||||
|
$validator = $this->validator($payload);
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
return back()
|
||||||
|
->withInput($request->only('email'))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->user_service->requestPasswordReset($payload);
|
||||||
|
|
||||||
|
return $this->sendResetLinkResponse("Reset link sent");
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
foreach ($ex->getMessages() as $message){
|
||||||
|
$validator->getMessageBag()->add('validation', $message);
|
||||||
|
}
|
||||||
|
return back()
|
||||||
|
->withInput($request->only('email'))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
return view("auth.passwords.email_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validator for an incoming registration request.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected function validator(array $data)
|
||||||
|
{
|
||||||
|
return Validator::make($data, [
|
||||||
|
'email' => 'required|string|email|max:255',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the response for a successful password reset link.
|
||||||
|
*
|
||||||
|
* @param string $response
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
protected function sendResetLinkResponse($response)
|
||||||
|
{
|
||||||
|
return back()->with('status', trans($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the response for a failed password reset link.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param string $response
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
protected function sendResetLinkFailedResponse(LaravelRequest $request, $response)
|
||||||
|
{
|
||||||
|
return back()
|
||||||
|
->withInput($request->only('email'))
|
||||||
|
->withErrors(['email' => trans($response)]);
|
||||||
|
}
|
||||||
|
}
|
188
app/Http/Controllers/Auth/PasswordSetController.php
Normal file
188
app/Http/Controllers/Auth/PasswordSetController.php
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\libs\Auth\Repositories\IUserRegistrationRequestRepository;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Http\Request as LaravelRequest;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use OAuth2\Repositories\IClientRepository;
|
||||||
|
/**
|
||||||
|
* Class PasswordSetController
|
||||||
|
* @package App\Http\Controllers\Auth
|
||||||
|
*/
|
||||||
|
final class PasswordSetController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IUserRegistrationRequestRepository
|
||||||
|
*/
|
||||||
|
private $user_registration_request_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IClientRepository
|
||||||
|
*/
|
||||||
|
private $client_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PasswordSetController constructor.
|
||||||
|
* @param IUserRegistrationRequestRepository $user_registration_request_repository
|
||||||
|
* @param IClientRepository $client_repository
|
||||||
|
* @param IUserService $user_service
|
||||||
|
*/
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IUserRegistrationRequestRepository $user_registration_request_repository,
|
||||||
|
IClientRepository $client_repository,
|
||||||
|
IUserService $user_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->middleware('guest');
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
$this->user_registration_request_repository = $user_registration_request_repository;
|
||||||
|
$this->client_repository = $client_repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $token
|
||||||
|
* @param LaravelRequest $request
|
||||||
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||||
|
*/
|
||||||
|
public function showPasswordSetForm($token, LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
$user_registration_request = $this->user_registration_request_repository->getByHash($token);
|
||||||
|
|
||||||
|
if(is_null($user_registration_request))
|
||||||
|
throw new EntityNotFoundException("request not found");
|
||||||
|
|
||||||
|
if($user_registration_request->isRedeem())
|
||||||
|
throw new ValidationException("request already redeem!");
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
"email" => $user_registration_request->getEmail(),
|
||||||
|
"token" => $token,
|
||||||
|
"redirect_uri" => '',
|
||||||
|
"client_id" => '',
|
||||||
|
];
|
||||||
|
|
||||||
|
if($request->has("redirect_uri") && $request->has("client_id")){
|
||||||
|
$redirect_uri = $request->get("redirect_uri");
|
||||||
|
$client_id = $request->get("client_id");
|
||||||
|
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
if(is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if(!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$params['redirect_uri'] = $redirect_uri;
|
||||||
|
$params['client_id'] = $client_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('auth.passwords.set', $params);
|
||||||
|
}
|
||||||
|
catch (\Exception $ex){
|
||||||
|
Log::error($ex);
|
||||||
|
}
|
||||||
|
return view('auth.passwords.set_error');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validator for an incoming registration request.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected function validator(array $data)
|
||||||
|
{
|
||||||
|
return Validator::make($data, [
|
||||||
|
'token' => 'required',
|
||||||
|
'password' => 'required|string|min:8|confirmed',
|
||||||
|
'g-recaptcha-response' => 'required|recaptcha',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the given user's password.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function setPassword(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$payload = $request->all();
|
||||||
|
$validator = $this->validator($payload);
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['token','client_id', 'redirect_uri', 'email']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user_registration_request = $this->user_service->setPassword($payload['token'], $payload['password']);
|
||||||
|
$params = [
|
||||||
|
'client_id' => '',
|
||||||
|
'redirect_uri' => '',
|
||||||
|
'email' => '',
|
||||||
|
];
|
||||||
|
|
||||||
|
// check redirect uri with associated client
|
||||||
|
if($request->has("redirect_uri") && $request->has("client_id")){
|
||||||
|
$redirect_uri = $request->get("redirect_uri");
|
||||||
|
$client_id = $request->get("client_id");
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
|
||||||
|
if(is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if(!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$params['client_id'] = $client_id;
|
||||||
|
$params['redirect_uri'] = $redirect_uri;
|
||||||
|
$params['email'] = $user_registration_request->getEmail();
|
||||||
|
}
|
||||||
|
|
||||||
|
Auth::login($user_registration_request->getOwner(), true);
|
||||||
|
|
||||||
|
return view("auth.passwords.set_success", $params);
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
foreach ($ex->getMessages() as $message){
|
||||||
|
$validator->getMessageBag()->add('validation', $message);
|
||||||
|
}
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['token','client_id', 'redirect_uri', 'email']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return view("auth.passwords.reset_error");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
261
app/Http/Controllers/Auth/RegisterController.php
Normal file
261
app/Http/Controllers/Auth/RegisterController.php
Normal file
@ -0,0 +1,261 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\URL;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Http\Request as LaravelRequest;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use OAuth2\Factories\OAuth2AuthorizationRequestFactory;
|
||||||
|
use OAuth2\OAuth2Message;
|
||||||
|
use OAuth2\Repositories\IClientRepository;
|
||||||
|
use OAuth2\Services\IMementoOAuth2SerializerService;
|
||||||
|
use Sokil\IsoCodes\IsoCodesFactory;
|
||||||
|
use Exception;
|
||||||
|
/**
|
||||||
|
* Class RegisterController
|
||||||
|
* @package App\Http\Controllers\Auth
|
||||||
|
*/
|
||||||
|
final class RegisterController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IClientRepository
|
||||||
|
*/
|
||||||
|
private $client_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IMementoOAuth2SerializerService
|
||||||
|
*/
|
||||||
|
private $memento_service;
|
||||||
|
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IClientRepository $client_repository,
|
||||||
|
IUserService $user_service,
|
||||||
|
IMementoOAuth2SerializerService $memento_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->middleware('guest');
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
$this->client_repository = $client_repository;
|
||||||
|
$this->memento_service = $memento_service;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param LaravelRequest $request
|
||||||
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||||
|
* @throws ValidationException
|
||||||
|
*/
|
||||||
|
public function showRegistrationForm(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// init database
|
||||||
|
$isoCodes = new IsoCodesFactory();
|
||||||
|
// get countries database
|
||||||
|
$countries = $isoCodes->getCountries()->toArray();
|
||||||
|
$params = [
|
||||||
|
"redirect_uri" => '',
|
||||||
|
"email" => '',
|
||||||
|
"first_name" => '',
|
||||||
|
"last_name" => '',
|
||||||
|
"client_id" => '',
|
||||||
|
'countries' => $countries
|
||||||
|
];
|
||||||
|
|
||||||
|
// check if we have a former oauth2 request
|
||||||
|
if ($this->memento_service->exists()) {
|
||||||
|
|
||||||
|
Log::debug("RegisterController::showRegistrationForm exist a oauth auth request on session");
|
||||||
|
|
||||||
|
$oauth_auth_request = OAuth2AuthorizationRequestFactory::getInstance()->build
|
||||||
|
(
|
||||||
|
OAuth2Message::buildFromMemento($this->memento_service->load())
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($oauth_auth_request->isValid()) {
|
||||||
|
|
||||||
|
|
||||||
|
$redirect_uri = $oauth_auth_request->getRedirectUri();
|
||||||
|
$client_id = $oauth_auth_request->getClientId();
|
||||||
|
|
||||||
|
Log::debug(sprintf( "RegisterController::showRegistrationForm exist a oauth auth request is valid for client id %s", $client_id));
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
if (is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if (!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$this->memento_service->serialize($oauth_auth_request->getMessage()->createMemento());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if we have explicit params at query string
|
||||||
|
if ($request->has("redirect_uri") && $request->has("client_id")) {
|
||||||
|
$redirect_uri = $request->get("redirect_uri");
|
||||||
|
$client_id = $request->get("client_id");
|
||||||
|
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
if (is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if (!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$params['redirect_uri'] = $redirect_uri;
|
||||||
|
$params['client_id'] = $client_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($request->has('email')){
|
||||||
|
$params['email'] = $request->get("email");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($request->has('first_name')){
|
||||||
|
$params['first_name'] = $request->get("first_name");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($request->has('last_name')){
|
||||||
|
$params['last_name'] = $request->get("last_name");
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('auth.register', $params);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
return view("auth.register_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validator for an incoming registration request.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected function validator(array $data)
|
||||||
|
{
|
||||||
|
return Validator::make($data, [
|
||||||
|
'first_name' => 'required|string|max:255',
|
||||||
|
'last_name' => 'required|string|max:255',
|
||||||
|
'country_iso_code' => 'required|string|country_iso_alpha2_code',
|
||||||
|
'email' => 'required|string|email|max:255',
|
||||||
|
'password' => 'required|string|min:8|confirmed',
|
||||||
|
'g-recaptcha-response' => 'required|recaptcha',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a registration request for the application.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function register(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
$validator = null;
|
||||||
|
try {
|
||||||
|
$payload = $request->all();
|
||||||
|
$validator = $this->validator($payload);
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['first_name', 'last_name', 'country_iso_code','email','client_id', 'redirect_uri']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $this->user_service->registerUser($payload);
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
'client_id' => '',
|
||||||
|
'redirect_uri' => '',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// check if we have a former oauth2 request
|
||||||
|
if ($this->memento_service->exists()) {
|
||||||
|
|
||||||
|
Log::debug("RegisterController::register exist a oauth auth request on session");
|
||||||
|
$oauth_auth_request = OAuth2AuthorizationRequestFactory::getInstance()->build
|
||||||
|
(
|
||||||
|
OAuth2Message::buildFromMemento($this->memento_service->load())
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($oauth_auth_request->isValid()) {
|
||||||
|
$redirect_uri = $oauth_auth_request->getRedirectUri();
|
||||||
|
$client_id = $oauth_auth_request->getClientId();
|
||||||
|
Log::debug(sprintf( "RegisterController::register exist a oauth auth request is valid for client id %s", $client_id));
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
if (is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if (!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$this->memento_service->serialize($oauth_auth_request->getMessage()->createMemento());
|
||||||
|
|
||||||
|
$params['redirect_uri'] = action('OAuth2\OAuth2ProviderController@auth');
|
||||||
|
|
||||||
|
Auth::login($user, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check redirect uri with associated client
|
||||||
|
if($request->has("redirect_uri") && $request->has("client_id")){
|
||||||
|
$redirect_uri = $request->get("redirect_uri");
|
||||||
|
$client_id = $request->get("client_id");
|
||||||
|
$client = $this->client_repository->getClientById($client_id);
|
||||||
|
|
||||||
|
if(is_null($client))
|
||||||
|
throw new ValidationException("client does not exists");
|
||||||
|
|
||||||
|
if(!$client->isUriAllowed($redirect_uri))
|
||||||
|
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
|
||||||
|
|
||||||
|
$params['client_id'] = $client_id;
|
||||||
|
$params['redirect_uri'] = $redirect_uri;
|
||||||
|
Auth::login($user, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return view("auth.register_success", $params);
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
|
||||||
|
if(!is_null($validator)) {
|
||||||
|
$validator->getMessageBag()->add('validation', sprintf
|
||||||
|
(
|
||||||
|
"It looks like a user with this email address already exists." .
|
||||||
|
"You can either <a href='%s'>sign in</a> or <a href='%s'>reset your password</a> if you've forgotten it.",
|
||||||
|
URL::action("UserController@getLogin"),
|
||||||
|
URL::action("Auth\ForgotPasswordController@showLinkRequestForm")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['first_name', 'last_name', 'country_iso_code','email']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
catch(Exception $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
return view("auth.register_error");
|
||||||
|
}
|
||||||
|
}
|
143
app/Http/Controllers/Auth/ResetPasswordController.php
Normal file
143
app/Http/Controllers/Auth/ResetPasswordController.php
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
<?php namespace App\Http\Controllers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\libs\Auth\Repositories\IUserPasswordResetRequestRepository;
|
||||||
|
use App\Services\Auth\IUserService;
|
||||||
|
use Auth\Repositories\IUserRepository;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Redirect;
|
||||||
|
use Illuminate\Support\Facades\URL;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Http\Request as LaravelRequest;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
/**
|
||||||
|
* Class ResetPasswordController
|
||||||
|
* @package App\Http\Controllers\Auth
|
||||||
|
*/
|
||||||
|
final class ResetPasswordController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IUserService
|
||||||
|
*/
|
||||||
|
private $user_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IUserPasswordResetRequestRepository
|
||||||
|
*/
|
||||||
|
private $user_password_reset_request_repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ResetPasswordController constructor.
|
||||||
|
* @param IUserPasswordResetRequestRepository $user_password_reset_request_repository
|
||||||
|
* @param IUserService $user_service
|
||||||
|
*/
|
||||||
|
public function __construct
|
||||||
|
(
|
||||||
|
IUserPasswordResetRequestRepository $user_password_reset_request_repository,
|
||||||
|
IUserService $user_service
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->middleware('guest');
|
||||||
|
$this->user_service = $user_service;
|
||||||
|
$this->user_password_reset_request_repository = $user_password_reset_request_repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $token
|
||||||
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||||
|
*/
|
||||||
|
public function showResetForm($token)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$request = $this->user_password_reset_request_repository->getByToken($token);
|
||||||
|
if(is_null($request))
|
||||||
|
throw new EntityNotFoundException(sprint("request not found for token %s", $token));
|
||||||
|
|
||||||
|
if(!$request->isValid())
|
||||||
|
throw new ValidationException("request is void");
|
||||||
|
|
||||||
|
if($request->isRedeem()){
|
||||||
|
throw new ValidationException("request is already redeem");
|
||||||
|
}
|
||||||
|
return view('auth.passwords.reset')->with(
|
||||||
|
[
|
||||||
|
'token' => $token,
|
||||||
|
'email' => $request->getOwner()->getEmail()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::error($ex);
|
||||||
|
}
|
||||||
|
return view("auth.passwords.reset_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validator for an incoming registration request.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected function validator(array $data)
|
||||||
|
{
|
||||||
|
return Validator::make($data, [
|
||||||
|
'token' => 'required',
|
||||||
|
'password' => 'required|string|min:8|confirmed',
|
||||||
|
'g-recaptcha-response' => 'required|recaptcha',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the given user's password.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function reset(LaravelRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$payload = $request->all();
|
||||||
|
$validator = $this->validator($payload);
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['token', 'email']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->user_service->resetPassword($payload['token'], $payload['password']);
|
||||||
|
|
||||||
|
return view("auth.passwords.reset_success");
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
foreach ($ex->getMessages() as $message){
|
||||||
|
$validator->getMessageBag()->add('validation', $message);
|
||||||
|
}
|
||||||
|
return back()
|
||||||
|
->withInput($request->only(['token', 'email']))
|
||||||
|
->withErrors($validator);
|
||||||
|
}
|
||||||
|
catch(\Exception $ex){
|
||||||
|
Log::warning($ex);
|
||||||
|
}
|
||||||
|
return view("auth.passwords.reset_error");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -17,7 +17,6 @@ use Illuminate\Support\Facades\View;
|
|||||||
use Illuminate\Support\Facades\Redirect;
|
use Illuminate\Support\Facades\Redirect;
|
||||||
use App\Http\Controllers\OpenId\OpenIdController;
|
use App\Http\Controllers\OpenId\OpenIdController;
|
||||||
use App\Http\Controllers\OpenId\DiscoveryController;
|
use App\Http\Controllers\OpenId\DiscoveryController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HomeController
|
* Class HomeController
|
||||||
* @package App\Http\Controllers
|
* @package App\Http\Controllers
|
||||||
|
@ -255,7 +255,7 @@ final class OAuth2ProviderController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function checkSessionIFrame()
|
public function checkSessionIFrame()
|
||||||
{
|
{
|
||||||
$data = array();
|
$data = [];
|
||||||
return View::make("oauth2.session.check-session", $data);
|
return View::make("oauth2.session.check-session", $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,13 +11,11 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use OpenId\IOpenIdProtocol;
|
use OpenId\IOpenIdProtocol;
|
||||||
use OpenId\Services\IServerConfigurationService;
|
use OpenId\Services\IServerConfigurationService;
|
||||||
use Utils\Services\IAuthService;
|
use Utils\Services\IAuthService;
|
||||||
use Illuminate\Support\Facades\View;
|
use Illuminate\Support\Facades\View;
|
||||||
use Illuminate\Support\Facades\Response;
|
use Illuminate\Support\Facades\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DiscoveryController
|
* Class DiscoveryController
|
||||||
* @package App\Http\Controllers\OpenId
|
* @package App\Http\Controllers\OpenId
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
use OpenId\Xrds\XRDSDocumentBuilder;
|
use OpenId\Xrds\XRDSDocumentBuilder;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OpenIdController
|
* Class OpenIdController
|
||||||
* @package App\Http\Controllers\OpenId
|
* @package App\Http\Controllers\OpenId
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
<?php namespace App\Http\Controllers\OpenId;
|
<?php namespace App\Http\Controllers\OpenId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2015 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -25,7 +24,6 @@ use OpenId\OpenIdMessage;
|
|||||||
use OpenId\Responses\OpenIdResponse;
|
use OpenId\Responses\OpenIdResponse;
|
||||||
use OpenId\Services\IMementoOpenIdSerializerService;
|
use OpenId\Services\IMementoOpenIdSerializerService;
|
||||||
use OpenId\Strategies\OpenIdResponseStrategyFactoryMethod;
|
use OpenId\Strategies\OpenIdResponseStrategyFactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OpenIdProviderController
|
* Class OpenIdProviderController
|
||||||
* @package App\Http\Controllers\OpenId
|
* @package App\Http\Controllers\OpenId
|
||||||
|
140
app/Http/Controllers/Traits/GetAllTrait.php
Normal file
140
app/Http/Controllers/Traits/GetAllTrait.php
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
<?php namespace App\Http\Controllers;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Http\Utils\PagingConstants;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
use Illuminate\Support\Facades\Input;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use utils\Filter;
|
||||||
|
use utils\FilterParser;
|
||||||
|
use utils\OrderParser;
|
||||||
|
use utils\PagingInfo;
|
||||||
|
use Exception;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
use models\exceptions\EntityNotFoundException;
|
||||||
|
/**
|
||||||
|
* Trait GetAllTrait
|
||||||
|
* @package App\Http\Controllers
|
||||||
|
*/
|
||||||
|
trait GetAllTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterRules():array{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilterValidatorRules():array{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getOrderRules():array{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function applyExtraFilters(Filter $filter):Filter{
|
||||||
|
return $filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAllSerializerType():string{
|
||||||
|
return SerializerRegistry::SerializerType_Public;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getAll()
|
||||||
|
{
|
||||||
|
$values = Input::all();
|
||||||
|
$rules = [
|
||||||
|
|
||||||
|
'page' => 'integer|min:1',
|
||||||
|
'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize),
|
||||||
|
];
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$validation = Validator::make($values, $rules);
|
||||||
|
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$ex = new ValidationException();
|
||||||
|
throw $ex->setMessages($validation->messages()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
// default values
|
||||||
|
$page = 1;
|
||||||
|
$per_page = PagingConstants::DefaultPageSize;;
|
||||||
|
|
||||||
|
if (Input::has('page')) {
|
||||||
|
$page = intval(Input::get('page'));
|
||||||
|
$per_page = intval(Input::get('per_page'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$filter = null;
|
||||||
|
|
||||||
|
if (Input::has('filter')) {
|
||||||
|
$filter = FilterParser::parse(Input::get('filter'), $this->getFilterRules());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_null($filter)) $filter = new Filter();
|
||||||
|
|
||||||
|
$filter_validator_rules = $this->getFilterValidatorRules();
|
||||||
|
if(count($filter_validator_rules)) {
|
||||||
|
$filter->validate($filter_validator_rules);
|
||||||
|
}
|
||||||
|
|
||||||
|
$order = null;
|
||||||
|
|
||||||
|
if (Input::has('order'))
|
||||||
|
{
|
||||||
|
$order = OrderParser::parse(Input::get('order'), $this->getOrderRules());
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->repository->getAllByPage(new PagingInfo($page, $per_page), $this->applyExtraFilters($filter), $order);
|
||||||
|
|
||||||
|
return $this->ok
|
||||||
|
(
|
||||||
|
$data->toArray
|
||||||
|
(
|
||||||
|
Input::get('expand', ''),
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
$this->getAllSerializerType()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (ValidationException $ex1)
|
||||||
|
{
|
||||||
|
Log::warning($ex1);
|
||||||
|
return $this->error412($ex1->getMessages());
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException $ex2)
|
||||||
|
{
|
||||||
|
Log::warning($ex2);
|
||||||
|
return $this->error404(['message' => $ex2->getMessage()]);
|
||||||
|
}
|
||||||
|
catch (Exception $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
return $this->error500($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
<?php namespace App\Http\Controllers;
|
<?php namespace App\Http\Controllers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2015 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -12,7 +11,6 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use App\Http\Controllers\OpenId\DiscoveryController;
|
use App\Http\Controllers\OpenId\DiscoveryController;
|
||||||
use App\Http\Controllers\OpenId\OpenIdController;
|
use App\Http\Controllers\OpenId\OpenIdController;
|
||||||
use Auth\Exceptions\AuthenticationException;
|
use Auth\Exceptions\AuthenticationException;
|
||||||
@ -35,8 +33,9 @@ use OAuth2\Services\ISecurityContextService;
|
|||||||
use OAuth2\Services\ITokenService;
|
use OAuth2\Services\ITokenService;
|
||||||
use OpenId\Services\IMementoOpenIdSerializerService;
|
use OpenId\Services\IMementoOpenIdSerializerService;
|
||||||
use OpenId\Services\ITrustedSitesService;
|
use OpenId\Services\ITrustedSitesService;
|
||||||
use Services\Exceptions\ValidationException;
|
use models\exceptions\ValidationException;
|
||||||
use Services\IUserActionService;
|
use Services\IUserActionService;
|
||||||
|
use Sokil\IsoCodes\IsoCodesFactory;
|
||||||
use Strategies\DefaultLoginStrategy;
|
use Strategies\DefaultLoginStrategy;
|
||||||
use Strategies\IConsentStrategy;
|
use Strategies\IConsentStrategy;
|
||||||
use Strategies\OAuth2ConsentStrategy;
|
use Strategies\OAuth2ConsentStrategy;
|
||||||
@ -47,7 +46,6 @@ use Utils\IPHelper;
|
|||||||
use Utils\Services\IAuthService;
|
use Utils\Services\IAuthService;
|
||||||
use Utils\Services\IServerConfigurationService;
|
use Utils\Services\IServerConfigurationService;
|
||||||
use Utils\Services\IServerConfigurationService as IUtilsServerConfigurationService;
|
use Utils\Services\IServerConfigurationService as IUtilsServerConfigurationService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class UserController
|
* Class UserController
|
||||||
* @package App\Http\Controllers
|
* @package App\Http\Controllers
|
||||||
@ -271,9 +269,9 @@ final class UserController extends OpenIdController
|
|||||||
//failed login attempt...
|
//failed login attempt...
|
||||||
$user = $this->auth_service->getUserByUsername($username);
|
$user = $this->auth_service->getUserByUsername($username);
|
||||||
|
|
||||||
if ($user)
|
if (!is_null($user))
|
||||||
{
|
{
|
||||||
$login_attempts = $user->login_failed_attempt;
|
$login_attempts = $user->getLoginFailedAttempt();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->login_strategy->errorLogin
|
return $this->login_strategy->errorLogin
|
||||||
@ -383,6 +381,14 @@ final class UserController extends OpenIdController
|
|||||||
*/
|
*/
|
||||||
return $this->discovery->user($identifier);
|
return $this->discovery->user($identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$redirect = Session::get('backurl');
|
||||||
|
if (!empty($redirect)) {
|
||||||
|
Session::forget('backurl');
|
||||||
|
Session::save();
|
||||||
|
return Redirect::to($redirect);
|
||||||
|
}
|
||||||
|
|
||||||
$current_user = $this->auth_service->getCurrentUser();
|
$current_user = $this->auth_service->getCurrentUser();
|
||||||
$another_user = false;
|
$another_user = false;
|
||||||
if ($current_user && $current_user->getIdentifier() != $user->getIdentifier())
|
if ($current_user && $current_user->getIdentifier() != $user->getIdentifier())
|
||||||
@ -394,8 +400,8 @@ final class UserController extends OpenIdController
|
|||||||
$pic_url = $user->getPic();
|
$pic_url = $user->getPic();
|
||||||
$pic_url = str_contains($pic_url, 'http') ? $pic_url : $assets_url . $pic_url;
|
$pic_url = str_contains($pic_url, 'http') ? $pic_url : $assets_url . $pic_url;
|
||||||
|
|
||||||
$params = array
|
$params = [
|
||||||
(
|
|
||||||
'show_fullname' => $user->getShowProfileFullName(),
|
'show_fullname' => $user->getShowProfileFullName(),
|
||||||
'username' => $user->getFullName(),
|
'username' => $user->getFullName(),
|
||||||
'show_email' => $user->getShowProfileEmail(),
|
'show_email' => $user->getShowProfileEmail(),
|
||||||
@ -404,7 +410,7 @@ final class UserController extends OpenIdController
|
|||||||
'show_pic' => $user->getShowProfilePic(),
|
'show_pic' => $user->getShowProfilePic(),
|
||||||
'pic' => $pic_url,
|
'pic' => $pic_url,
|
||||||
'another_user' => $another_user,
|
'another_user' => $another_user,
|
||||||
);
|
];
|
||||||
|
|
||||||
return View::make("identity", $params);
|
return View::make("identity", $params);
|
||||||
}
|
}
|
||||||
@ -433,55 +439,35 @@ final class UserController extends OpenIdController
|
|||||||
{
|
{
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
$sites = $user->getTrustedSites();
|
$sites = $user->getTrustedSites();
|
||||||
$actions = $user->getActions();
|
$actions = $user->getLatestNActions(10);
|
||||||
|
|
||||||
return View::make("profile", array
|
// init database
|
||||||
(
|
$isoCodes = new IsoCodesFactory();
|
||||||
"username" => $user->getFullName(),
|
|
||||||
"user_id" => $user->getId(),
|
|
||||||
"is_oauth2_admin" => $user->isOAuth2ServerAdmin(),
|
|
||||||
"is_openstackid_admin" => $user->isOpenstackIdAdmin(),
|
|
||||||
"use_system_scopes" => $user->canUseSystemScopes(),
|
|
||||||
"openid_url" => $this->server_configuration_service->getUserIdentityEndpointURL($user->getIdentifier()),
|
|
||||||
"identifier " => $user->getIdentifier(),
|
|
||||||
"sites" => $sites,
|
|
||||||
'identifier' => $user->getIdentifier(),
|
|
||||||
"show_pic" => $user->getShowProfilePic(),
|
|
||||||
"show_full_name" => $user->getShowProfileFullName(),
|
|
||||||
"show_email" => $user->getShowProfileEmail(),
|
|
||||||
'actions' => $actions,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function postUserProfileOptions()
|
// get languages database
|
||||||
{
|
$languages = $isoCodes->getLanguages()->toArray();
|
||||||
$values = Input::all();
|
$lang2Code = [];
|
||||||
$show_full_name = intval(Input::get("show_full_name", 0));
|
foreach ($languages as $lang){
|
||||||
$show_email = intval(Input::get("show_email", 0));
|
if(!empty($lang->getAlpha2()))
|
||||||
$show_pic = intval(Input::get("show_pic", 0));
|
$lang2Code[] = $lang;
|
||||||
$identifier = Input::get("identifier", null);
|
|
||||||
|
|
||||||
$validator = Validator::make($values, ['identifier' => 'required|openid.identifier']);
|
|
||||||
|
|
||||||
if ($validator->fails()) {
|
|
||||||
return Redirect::back()->withErrors($validator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// get countries database
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$countries = $isoCodes->getCountries()->toArray();
|
||||||
$this->user_service->saveProfileInfo($user->getId(), $show_pic, $show_full_name, $show_email, $identifier);
|
|
||||||
|
|
||||||
return Redirect::action("UserController@getProfile");
|
return View::make("profile", [
|
||||||
}
|
'user' => $user,
|
||||||
catch(ValidationException $ex1){
|
"openid_url" => $this->server_configuration_service->getUserIdentityEndpointURL($user->getIdentifier()),
|
||||||
$validator->errors()->add('identifier', $ex1->getMessage());
|
"sites" => $sites,
|
||||||
return Redirect::back()->withErrors($validator);
|
'actions' => $actions,
|
||||||
}
|
'countries' => $countries,
|
||||||
|
'languages' => $lang2Code,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteTrustedSite($id)
|
public function deleteTrustedSite($id)
|
||||||
{
|
{
|
||||||
$this->trusted_sites_service->delTrustedSite($id);
|
$this->trusted_sites_service->delete($id);
|
||||||
return Redirect::action("UserController@getProfile");
|
return Redirect::action("UserController@getProfile");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,13 +33,13 @@ class Authenticate
|
|||||||
public function handle($request, Closure $next, $guard = null)
|
public function handle($request, Closure $next, $guard = null)
|
||||||
{
|
{
|
||||||
if (Auth::guard($guard)->guest()) {
|
if (Auth::guard($guard)->guest()) {
|
||||||
Session::put('url.intended', URL::full());
|
Session::put('backurl', URL::full());
|
||||||
Session::save();
|
Session::save();
|
||||||
return Redirect::action('HomeController@index');
|
return Redirect::action('UserController@getLogin');
|
||||||
}
|
}
|
||||||
$redirect = Session::get('url.intended');
|
$redirect = Session::get('backurl');
|
||||||
if (!empty($redirect)) {
|
if (!empty($redirect)) {
|
||||||
Session::forget('url.intended');
|
Session::forget('backurl');
|
||||||
Session::save();
|
Session::save();
|
||||||
return Redirect::to($redirect);
|
return Redirect::to($redirect);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ final class CORSMiddleware
|
|||||||
|
|
||||||
const CORS_IP_BLACKLIST_PREFIX = 'CORS_IP_BLACKLIST_PREFIX:';
|
const CORS_IP_BLACKLIST_PREFIX = 'CORS_IP_BLACKLIST_PREFIX:';
|
||||||
|
|
||||||
private $headers = array();
|
private $headers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A header is said to be a simple header if the header field name is an ASCII case-insensitive match for Accept,
|
* A header is said to be a simple header if the header field name is an ASCII case-insensitive match for Accept,
|
||||||
@ -439,7 +439,7 @@ final class CORSMiddleware
|
|||||||
|
|
||||||
private static function getCustomHeaders(Request $request)
|
private static function getCustomHeaders(Request $request)
|
||||||
{
|
{
|
||||||
$custom_headers = array();
|
$custom_headers = [];
|
||||||
foreach ($request->headers->all() as $k => $h) {
|
foreach ($request->headers->all() as $k => $h) {
|
||||||
if (starts_with('X-', strtoupper(trim($k)))) {
|
if (starts_with('X-', strtoupper(trim($k)))) {
|
||||||
array_push($custom_headers, strtoupper(trim($k)));
|
array_push($custom_headers, strtoupper(trim($k)));
|
||||||
|
@ -28,7 +28,7 @@ class CORSRequestPreflightData
|
|||||||
/** Final HTTP request expected method */
|
/** Final HTTP request expected method */
|
||||||
private $expected_method = null;
|
private $expected_method = null;
|
||||||
/** Final HTTP request expected custom headers */
|
/** Final HTTP request expected custom headers */
|
||||||
private $expected_custom_headers = array();
|
private $expected_custom_headers = [];
|
||||||
/** Current HTTP request uri */
|
/** Current HTTP request uri */
|
||||||
private $uri = null;
|
private $uri = null;
|
||||||
/** Current HTTP request origin header */
|
/** Current HTTP request origin header */
|
||||||
@ -69,7 +69,7 @@ class CORSRequestPreflightData
|
|||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$res = array();
|
$res = [];
|
||||||
$res['sender'] = $this->sender;
|
$res['sender'] = $this->sender;
|
||||||
$res['uri'] = $this->uri;
|
$res['uri'] = $this->uri;
|
||||||
$res['origin'] = $this->origin;
|
$res['origin'] = $this->origin;
|
||||||
|
@ -69,7 +69,7 @@ final class CurrentUserCanEditOAuth2Client
|
|||||||
$client = $this->client_repository->getClientByIdentifier($client_id);
|
$client = $this->client_repository->getClientByIdentifier($client_id);
|
||||||
$user = $this->auth_service->getCurrentUser();
|
$user = $this->auth_service->getCurrentUser();
|
||||||
|
|
||||||
if (is_null($client) || !$client->candEdit($user))
|
if (is_null($client) || !$client->canEdit($user))
|
||||||
throw new Exception('invalid client id for current user');
|
throw new Exception('invalid client id for current user');
|
||||||
|
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
<?php namespace App\Http\Middleware;
|
|
||||||
/**
|
|
||||||
* Copyright 2016 OpenStack Foundation
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
**/
|
|
||||||
use Closure;
|
|
||||||
use Illuminate\Support\Facades\Response;
|
|
||||||
use Utils\Services\ServiceLocator;
|
|
||||||
use Utils\Services\UtilsServiceCatalog;
|
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use Illuminate\Support\Facades\Input;
|
|
||||||
use Illuminate\Support\Facades\Route;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class CurrentUserCheckRouteParams
|
|
||||||
* @package App\Http\Middleware
|
|
||||||
*/
|
|
||||||
class CurrentUserCheckRouteParams
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Handle an incoming request.
|
|
||||||
*
|
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @param \Closure $next
|
|
||||||
* @param string|null $guard
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function handle($request, Closure $next, $guard = null)
|
|
||||||
{
|
|
||||||
try{
|
|
||||||
$route = Route::getCurrentRoute();
|
|
||||||
$authentication_service = ServiceLocator::getInstance()->getService(UtilsServiceCatalog::AuthenticationService);
|
|
||||||
$used_id = Input::get('user_id',null);
|
|
||||||
|
|
||||||
if(is_null($used_id))
|
|
||||||
$used_id = Input::get('id',null);
|
|
||||||
|
|
||||||
if(is_null($used_id))
|
|
||||||
$used_id = $route->parameter('user_id');
|
|
||||||
|
|
||||||
if(is_null($used_id))
|
|
||||||
$used_id = $route->parameter('id');
|
|
||||||
|
|
||||||
$user = $authentication_service->getCurrentUser();
|
|
||||||
if (is_null($used_id) || intval($used_id) !== intval($user->getId()))
|
|
||||||
throw new Exception(sprintf('user id %s does not match with current user id %s',$used_id,$user->getId()));
|
|
||||||
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
Log::error($ex);
|
|
||||||
return Response::json(array('error' => 'operation not allowed.'), 400);
|
|
||||||
}
|
|
||||||
return $next($request);
|
|
||||||
}
|
|
||||||
}
|
|
@ -33,11 +33,11 @@ final class CurrentUserIsOAuth2ServerAdmin
|
|||||||
{
|
{
|
||||||
if (Auth::guard($guard)->guest())
|
if (Auth::guard($guard)->guest())
|
||||||
{
|
{
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
if(!Auth::user()->isOAuth2ServerAdmin())
|
if(!Auth::user()->isOAuth2ServerAdmin())
|
||||||
{
|
{
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,11 @@ final class CurrentUserIsOpenIdServerAdmin
|
|||||||
{
|
{
|
||||||
if (Auth::guard($guard)->guest())
|
if (Auth::guard($guard)->guest())
|
||||||
{
|
{
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
if(!Auth::user()->isOpenstackIdAdmin())
|
if(!Auth::user()->isOpenIdServerAdmin())
|
||||||
{
|
{
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,11 @@ class CurrentUserIsOpenIdServerAdminJson
|
|||||||
{
|
{
|
||||||
if (Auth::guard($guard)->guest())
|
if (Auth::guard($guard)->guest())
|
||||||
{
|
{
|
||||||
return Response::json(array('error' => 'you are not allowed to perform this operation'));
|
return Response::json(['error' => 'you are not allowed to perform this operation']);
|
||||||
}
|
}
|
||||||
if(!Auth::user()->isOpenstackIdAdmin())
|
if(!Auth::user()->isOpenIdServerAdmin())
|
||||||
{
|
{
|
||||||
return Response::json(array('error' => 'you are not allowed to perform this operation'));
|
return Response::json(['error' => 'you are not allowed to perform this operation']);
|
||||||
}
|
}
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
@ -238,7 +238,6 @@ final class OAuth2BearerAccessTokenRequestValidator
|
|||||||
if (!is_null($access_token->getUserId()))
|
if (!is_null($access_token->getUserId()))
|
||||||
{
|
{
|
||||||
$context['user_id'] = $access_token->getUserId();
|
$context['user_id'] = $access_token->getUserId();
|
||||||
//$context['user_external_id'] = $access_token->getUserExternalId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->context->setAuthorizationContext($context);
|
$this->context->setAuthorizationContext($context);
|
||||||
@ -324,12 +323,13 @@ final class OAuth2BearerAccessTokenRequestValidator
|
|||||||
*/
|
*/
|
||||||
protected function getHeaders()
|
protected function getHeaders()
|
||||||
{
|
{
|
||||||
$headers = array();
|
$headers = [];
|
||||||
if (function_exists('getallheaders')) {
|
if (function_exists('getallheaders')) {
|
||||||
foreach (getallheaders() as $name => $value) {
|
foreach (getallheaders() as $name => $value) {
|
||||||
$headers[strtolower($name)] = $value;
|
$headers[strtolower($name)] = $value;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
if(empty($headers)){
|
||||||
// @codeCoverageIgnoreEnd
|
// @codeCoverageIgnoreEnd
|
||||||
foreach ($_SERVER as $name => $value) {
|
foreach ($_SERVER as $name => $value) {
|
||||||
if (substr($name, 0, 5) == 'HTTP_') {
|
if (substr($name, 0, 5) == 'HTTP_') {
|
||||||
|
@ -30,7 +30,7 @@ class RedirectIfAuthenticated
|
|||||||
public function handle($request, Closure $next, $guard = null)
|
public function handle($request, Closure $next, $guard = null)
|
||||||
{
|
{
|
||||||
if (Auth::guard($guard)->check()) {
|
if (Auth::guard($guard)->check()) {
|
||||||
return redirect('/home');
|
return redirect('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
@ -34,11 +34,11 @@ final class SingleAccessPoint
|
|||||||
//checkpoint security pattern entry point
|
//checkpoint security pattern entry point
|
||||||
$checkpoint_service = ServiceLocator::getInstance()->getService(UtilsServiceCatalog::CheckPointService);
|
$checkpoint_service = ServiceLocator::getInstance()->getService(UtilsServiceCatalog::CheckPointService);
|
||||||
if ($checkpoint_service instanceof ICheckPointService && !$checkpoint_service->check()) {
|
if ($checkpoint_service instanceof ICheckPointService && !$checkpoint_service->check()) {
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
Log::error($ex);
|
Log::error($ex);
|
||||||
return Response::view('errors.404', array(), 404);
|
return Response::view('errors.404', [], 404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
32
app/Http/Utils/DateUtils.php
Normal file
32
app/Http/Utils/DateUtils.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2018 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
/**
|
||||||
|
* Class DateUtils
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class DateUtils
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param DateTime $start1
|
||||||
|
* @param DateTime $end1
|
||||||
|
* @param DateTime $start2
|
||||||
|
* @param DateTime $end2
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function checkTimeFramesOverlap(DateTime $start1, DateTime $end1, DateTime $start2, DateTime $end2){
|
||||||
|
return $start1 <= $end2 && $end1 >= $start2;
|
||||||
|
}
|
||||||
|
}
|
24
app/Http/Utils/FileTypes.php
Normal file
24
app/Http/Utils/FileTypes.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FileTypes
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class FileTypes
|
||||||
|
{
|
||||||
|
const SlidesExtensions = ['ppt', 'pptx', 'xps', 'key', 'pdf'];
|
||||||
|
const ImagesExntesions = ['jpg', 'jpeg', 'png', 'svg', 'bmp', 'tga', 'tiff', 'gif'];
|
||||||
|
}
|
79
app/Http/Utils/FileUploader.php
Normal file
79
app/Http/Utils/FileUploader.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Services\Model\IFolderService;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use models\main\File;
|
||||||
|
/**
|
||||||
|
* Class FileUploader
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class FileUploader implements IFileUploader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IFolderService
|
||||||
|
*/
|
||||||
|
private $folder_service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IBucket
|
||||||
|
*/
|
||||||
|
private $bucket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FileUploader constructor.
|
||||||
|
* @param IFolderService $folder_service
|
||||||
|
* @param IBucket $bucket
|
||||||
|
*/
|
||||||
|
public function __construct(IFolderService $folder_service, IBucket $bucket){
|
||||||
|
$this->folder_service = $folder_service;
|
||||||
|
$this->bucket = $bucket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UploadedFile $file
|
||||||
|
* @param $folder_name
|
||||||
|
* @param bool $is_image
|
||||||
|
* @return File
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function build(UploadedFile $file, $folder_name, $is_image = false){
|
||||||
|
$attachment = new File();
|
||||||
|
try {
|
||||||
|
|
||||||
|
$local_path = Storage::putFileAs(sprintf('/public/%s', $folder_name), $file, $file->getClientOriginalName());
|
||||||
|
$folder = $this->folder_service->findOrMake($folder_name);
|
||||||
|
$local_path = Storage::disk()->path($local_path);
|
||||||
|
$attachment->setParent($folder);
|
||||||
|
$attachment->setName($file->getClientOriginalName());
|
||||||
|
$attachment->setFilename(sprintf("assets/%s/%s", $folder_name, $file->getClientOriginalName()));
|
||||||
|
$attachment->setTitle(str_replace(array('-', '_'), ' ', preg_replace('/\.[^.]+$/', '', $file->getClientOriginalName())));
|
||||||
|
$attachment->setShowInSearch(true);
|
||||||
|
if ($is_image) // set className
|
||||||
|
$attachment->setImage();
|
||||||
|
|
||||||
|
$this->bucket->put($attachment, $local_path);
|
||||||
|
$attachment->setCloudMeta('LastPut', time());
|
||||||
|
$attachment->setCloudStatus('Live');
|
||||||
|
$attachment->setCloudSize(filesize($local_path));
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (\Exception $ex){
|
||||||
|
Log::error($ex);
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
return $attachment;
|
||||||
|
}
|
||||||
|
}
|
34
app/Http/Utils/FilterAvailableSummitsStrategy.php
Normal file
34
app/Http/Utils/FilterAvailableSummitsStrategy.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\Security\SummitScopes;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use models\oauth2\IResourceServerContext;
|
||||||
|
/**
|
||||||
|
* Class FilterAvailableSummitsStrategy
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class FilterAvailableSummitsStrategy
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param IResourceServerContext $resource_server_ctx
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
static public function shouldReturnAllSummits(IResourceServerContext $resource_server_ctx){
|
||||||
|
$scopes = $resource_server_ctx->getCurrentScope();
|
||||||
|
$current_realm = Config::get('app.scope_base_realm');
|
||||||
|
$needed_scope = sprintf(SummitScopes::ReadAllSummitData, $current_realm);
|
||||||
|
return in_array($needed_scope, $scopes);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
<?php namespace OAuth2\Factories;
|
<?php namespace utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2015 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -11,18 +12,25 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
use OAuth2\Models\IClient;
|
abstract class AbstractFilterElement
|
||||||
/**
|
|
||||||
* Interface IOAuth2ClientFactory
|
|
||||||
* @package OAuth2\Factories
|
|
||||||
*/
|
|
||||||
interface IOAuth2ClientFactory
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param string $app_name
|
* @var string
|
||||||
* @param $owner
|
|
||||||
* @param string $application_type
|
|
||||||
* @return IClient
|
|
||||||
*/
|
*/
|
||||||
public function build($app_name, $owner, $application_type);
|
protected $operator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $operator
|
||||||
|
*/
|
||||||
|
protected function __construct($operator)
|
||||||
|
{
|
||||||
|
$this->operator = $operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getOperator(){
|
||||||
|
return $this->operator;
|
||||||
|
}
|
||||||
}
|
}
|
57
app/Http/Utils/Filters/DoctrineCaseFilterMapping.php
Normal file
57
app/Http/Utils/Filters/DoctrineCaseFilterMapping.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DoctrineCaseFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
class DoctrineCaseFilterMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $condition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DoctrineCaseFilterMapping constructor.
|
||||||
|
* @param string $value
|
||||||
|
* @param string $condition
|
||||||
|
*/
|
||||||
|
public function __construct($value, $condition)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
$this->condition = $condition;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getValue()
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCondition()
|
||||||
|
{
|
||||||
|
return $this->condition;
|
||||||
|
}
|
||||||
|
}
|
90
app/Http/Utils/Filters/DoctrineFilterMapping.php
Normal file
90
app/Http/Utils/Filters/DoctrineFilterMapping.php
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class DoctrineFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
class DoctrineFilterMapping extends FilterMapping
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DoctrineFilterMapping constructor.
|
||||||
|
* @param string $condition
|
||||||
|
*/
|
||||||
|
public function __construct($condition)
|
||||||
|
{
|
||||||
|
parent::__construct("", $condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toRawSQL(FilterElement $filter)
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function apply(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
$query = $query->andWhere($where);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query = $query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function applyOr(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query = $query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
}
|
66
app/Http/Utils/Filters/DoctrineInstanceOfFilterMapping.php
Normal file
66
app/Http/Utils/Filters/DoctrineInstanceOfFilterMapping.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2018 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class DoctrineInstanceOfFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class DoctrineInstanceOfFilterMapping extends FilterMapping
|
||||||
|
{
|
||||||
|
|
||||||
|
private $class_names = [];
|
||||||
|
|
||||||
|
public function __construct($alias, $class_names = [])
|
||||||
|
{
|
||||||
|
$this->class_names = $class_names;
|
||||||
|
parent::__construct($alias, sprintf("%s %s :class_name", $alias, self::InstanceOfDoctrine));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function toRawSQL(FilterElement $filter)
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
const InstanceOfDoctrine = 'INSTANCE OF';
|
||||||
|
|
||||||
|
private function translateClassName($value){
|
||||||
|
if(isset($this->class_names[$value])) return $this->class_names[$value];
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function apply(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$where = str_replace(":class_name", $this->translateClassName($filter->getValue()), $this->where);
|
||||||
|
return $query->andWhere($where);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function applyOr(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$where = str_replace(":class_name", $this->translateClassName($filter->getValue()), $this->where);
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
105
app/Http/Utils/Filters/DoctrineJoinFilterMapping.php
Normal file
105
app/Http/Utils/Filters/DoctrineJoinFilterMapping.php
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2016 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class DoctrineJoinFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
class DoctrineJoinFilterMapping extends FilterMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $alias;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DoctrineJoinFilterMapping constructor.
|
||||||
|
* @param string $table
|
||||||
|
* @param string $alias
|
||||||
|
* @param string $where
|
||||||
|
*/
|
||||||
|
public function __construct($table, $alias, $where)
|
||||||
|
{
|
||||||
|
parent::__construct($table, $where);
|
||||||
|
$this->alias = $alias;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function toRawSQL(FilterElement $filter)
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function apply(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
if(!in_array($this->alias, $query->getAllAliases()))
|
||||||
|
$query->innerJoin($this->table, $this->alias, Join::WITH);
|
||||||
|
|
||||||
|
$query = $query->andWhere($where);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query = $query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function applyOr(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
if(!in_array($this->alias, $query->getAllAliases()))
|
||||||
|
$query->innerJoin($this->table, $this->alias, Join::WITH);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
}
|
81
app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php
Normal file
81
app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class DoctrineLeftJoinFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
class DoctrineLeftJoinFilterMapping extends DoctrineJoinFilterMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function apply(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
if(!in_array($this->alias, $query->getAllAliases()))
|
||||||
|
$query->leftJoin($this->table, $this->alias, Join::WITH);
|
||||||
|
|
||||||
|
$query = $query->andWhere($where);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query = $query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function applyOr(QueryBuilder $query, FilterElement $filter){
|
||||||
|
$param_count = $query->getParameters()->count() + 1;
|
||||||
|
$where = $this->where;
|
||||||
|
$has_param = false;
|
||||||
|
|
||||||
|
if(strstr($where,":value")) {
|
||||||
|
$where = str_replace(":value", ":value_" . $param_count, $where);
|
||||||
|
$has_param = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strstr($where,":operator"))
|
||||||
|
$where = str_replace(":operator", $filter->getOperator(), $where);
|
||||||
|
|
||||||
|
if(!in_array($this->alias, $query->getAllAliases()))
|
||||||
|
$query->leftJoin($this->table, $this->alias, Join::WITH);
|
||||||
|
|
||||||
|
if(!in_array($this->alias, $query->getAllAliases()))
|
||||||
|
$query->leftJoin($this->table, $this->alias, Join::WITH);
|
||||||
|
|
||||||
|
if($has_param){
|
||||||
|
$query->setParameter(":value_".$param_count, $filter->getValue());
|
||||||
|
}
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
}
|
63
app/Http/Utils/Filters/DoctrineSwitchFilterMapping.php
Normal file
63
app/Http/Utils/Filters/DoctrineSwitchFilterMapping.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class DoctrineSwitchFilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
class DoctrineSwitchFilterMapping extends FilterMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var DoctrineCaseFilterMapping[]
|
||||||
|
*/
|
||||||
|
private $case_statements;
|
||||||
|
|
||||||
|
public function __construct($case_statements = [])
|
||||||
|
{
|
||||||
|
parent::__construct("", "");
|
||||||
|
$this->case_statements = $case_statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toRawSQL(FilterElement $filter)
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function apply(QueryBuilder $query, FilterElement $filter){
|
||||||
|
if(!isset($this->case_statements[$filter->getValue()])) return $query;
|
||||||
|
$case_statement = $this->case_statements[$filter->getValue()];
|
||||||
|
return $query->andWhere($case_statement->getCondition());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function applyOr(QueryBuilder $query, FilterElement $filter){
|
||||||
|
if(!isset($this->case_statements[$filter->getValue()])) return $query;
|
||||||
|
$case_statement = $this->case_statements[$filter->getValue()];
|
||||||
|
return $case_statement->getCondition();
|
||||||
|
}
|
||||||
|
}
|
452
app/Http/Utils/Filters/Filter.php
Normal file
452
app/Http/Utils/Filters/Filter.php
Normal file
@ -0,0 +1,452 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\Common\Collections\Criteria;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
/**
|
||||||
|
* Class Filter
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class Filter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $filters = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $bindings = [];
|
||||||
|
|
||||||
|
public function __construct(array $filters = [])
|
||||||
|
{
|
||||||
|
$this->filters = $filters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function addFilterCondition(FilterElement $filter)
|
||||||
|
{
|
||||||
|
$this->filters[] = $filter;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* will return an array of filter elements, OR filters are returned on a sub array
|
||||||
|
* @param string $field
|
||||||
|
* @return null|FilterElement[]
|
||||||
|
*/
|
||||||
|
public function getFilter($field)
|
||||||
|
{
|
||||||
|
$res = [];
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
|
||||||
|
if ($filter instanceof FilterElement && $filter->getField() === $field) {
|
||||||
|
$res[] = $filter;
|
||||||
|
}
|
||||||
|
else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
$or_res = [];
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement && $e->getField() === $field) {
|
||||||
|
$or_res[] = $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count($or_res)) $res[] = $or_res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @return null|FilterElement
|
||||||
|
*/
|
||||||
|
public function getUniqueFilter($field){
|
||||||
|
$res = $this->getFilter($field);
|
||||||
|
return count($res) == 1 ? $res[0]:null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasFilter($field){
|
||||||
|
return count($this->getFilter($field)) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @return null|FilterElement[]
|
||||||
|
*/
|
||||||
|
public function getFlatFilter($field)
|
||||||
|
{
|
||||||
|
$res = [];
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
|
||||||
|
if ($filter instanceof FilterElement && $filter->getField() === $field) {
|
||||||
|
$res[] = $filter;
|
||||||
|
}
|
||||||
|
else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement && $e->getField() === $field) {
|
||||||
|
$res[] = $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getFiltersKeyValues(){
|
||||||
|
$res = [];
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
|
||||||
|
if ($filter instanceof FilterElement) {
|
||||||
|
$res[$filter->getField()] = $filter->getValue();
|
||||||
|
}
|
||||||
|
else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement) {
|
||||||
|
if(!isset($res[$e->getField()])) $res[$e->getField()] = [];
|
||||||
|
$res[$e->getField()][] = $e->getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $rules
|
||||||
|
* @param array $messages
|
||||||
|
* @throws ValidationException
|
||||||
|
*/
|
||||||
|
public function validate(array $rules, array $messages = []){
|
||||||
|
$filter_key_values = $this->getFiltersKeyValues();
|
||||||
|
foreach($rules as $field => $rule) {
|
||||||
|
if(!isset($filter_key_values[$field])) continue;
|
||||||
|
$values = $filter_key_values[$field];
|
||||||
|
if(!is_array($values)) $values = [$values];
|
||||||
|
foreach ($values as $val) {
|
||||||
|
$validation = Validator::make
|
||||||
|
(
|
||||||
|
[$field => $val],
|
||||||
|
[$field => $rule],
|
||||||
|
$messages
|
||||||
|
);
|
||||||
|
if ($validation->fails()) {
|
||||||
|
$ex = new ValidationException();
|
||||||
|
throw $ex->setMessages($validation->messages()->toArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Criteria $criteria
|
||||||
|
* @param array $mappings
|
||||||
|
* @return Criteria
|
||||||
|
*/
|
||||||
|
public function apply2Criteria(Criteria $criteria, array $mappings)
|
||||||
|
{
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
if ($filter instanceof FilterElement) {
|
||||||
|
if (isset($mappings[$filter->getField()])) {
|
||||||
|
$mapping = $mappings[$filter->getField()];
|
||||||
|
|
||||||
|
if ($mapping instanceof FilterMapping) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $filter->getValue();
|
||||||
|
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping[1]);
|
||||||
|
}
|
||||||
|
$criteria->andWhere(Criteria::expr()->eq($mapping[0], $value));
|
||||||
|
}
|
||||||
|
} else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement && isset($mappings[$e->getField()])) {
|
||||||
|
$mapping = $mappings[$e->getField()];
|
||||||
|
if ($mapping instanceof FilterMapping) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $filter->getValue();
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping[1]);
|
||||||
|
}
|
||||||
|
$criteria->orWhere(Criteria::expr()->eq($mapping[0], $value));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $criteria;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param array $mappings
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function apply2Query(QueryBuilder $query, array $mappings)
|
||||||
|
{
|
||||||
|
$param_prefix = "param_%s";
|
||||||
|
$param_idx = 1;
|
||||||
|
$bindings = [];
|
||||||
|
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
if ($filter instanceof FilterElement && isset($mappings[$filter->getField()])) {
|
||||||
|
$mapping = $mappings[$filter->getField()];
|
||||||
|
|
||||||
|
if ($mapping instanceof DoctrineJoinFilterMapping) {
|
||||||
|
$query = $mapping->apply($query, $filter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineSwitchFilterMapping) {
|
||||||
|
$query = $mapping->apply($query, $filter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineFilterMapping) {
|
||||||
|
$query = $mapping->apply($query, $filter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineInstanceOfFilterMapping) {
|
||||||
|
$query = $mapping->apply($query, $filter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if(is_array($mapping)){
|
||||||
|
$condition = '';
|
||||||
|
foreach ($mapping as $mapping_or){
|
||||||
|
$mapping_or = explode(':', $mapping_or);
|
||||||
|
$value = $filter->getValue();
|
||||||
|
if (count($mapping_or) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping_or[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($condition)) $condition .= ' OR ';
|
||||||
|
$bindings[sprintf($param_prefix, $param_idx)] = $value;
|
||||||
|
$condition .= sprintf("%s %s :%s", $mapping_or[0], $filter->getOperator(), sprintf($param_prefix, $param_idx));
|
||||||
|
++$param_idx;
|
||||||
|
}
|
||||||
|
$query->andWhere($condition);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $filter->getValue();
|
||||||
|
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping[1]);
|
||||||
|
}
|
||||||
|
$bindings[sprintf($param_prefix, $param_idx)] = $value;
|
||||||
|
$query = $query->andWhere(sprintf("%s %s :%s", $mapping[0], $filter->getOperator(), sprintf($param_prefix, $param_idx)));
|
||||||
|
++$param_idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
$sub_or_query = '';
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement && isset($mappings[$e->getField()])) {
|
||||||
|
|
||||||
|
$mapping = $mappings[$e->getField()];
|
||||||
|
if ($mapping instanceof DoctrineJoinFilterMapping) {
|
||||||
|
$condition = $mapping->applyOr($query, $e);
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
$sub_or_query .= $condition;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineSwitchFilterMapping) {
|
||||||
|
$condition = $mapping->applyOr($query, $e);
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
$sub_or_query .= $condition;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineFilterMapping) {
|
||||||
|
$condition = $mapping->applyOr($query, $e);
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
$sub_or_query .= $condition;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($mapping instanceof DoctrineInstanceOfFilterMapping) {
|
||||||
|
$condition = $mapping->applyOr($query, $e);
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
$sub_or_query .= $condition;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if(is_array($mapping)){
|
||||||
|
$condition = '';
|
||||||
|
foreach ($mapping as $mapping_or){
|
||||||
|
$mapping_or = explode(':', $mapping_or);
|
||||||
|
$value = $e->getValue();
|
||||||
|
if (count($mapping_or) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping_or[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($condition)) $condition .= ' OR ';
|
||||||
|
$bindings[sprintf($param_prefix, $param_idx)] = $value;
|
||||||
|
$condition .= sprintf(" %s %s :%s ", $mapping_or[0], $e->getOperator(), sprintf($param_prefix, $param_idx));
|
||||||
|
++$param_idx;
|
||||||
|
}
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
$sub_or_query .= ' ( '.$condition.' ) ';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $e->getValue();
|
||||||
|
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$value = $this->convertValue($value, $mapping[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($sub_or_query)) $sub_or_query .= ' OR ';
|
||||||
|
|
||||||
|
$bindings[sprintf($param_prefix, $param_idx)] = $value;
|
||||||
|
$sub_or_query .= sprintf(" %s %s :%s ", $mapping[0], $e->getOperator(), sprintf($param_prefix, $param_idx));
|
||||||
|
++$param_idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$query->andWhere($sub_or_query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach($bindings as $param => $value)
|
||||||
|
$query->setParameter($param, $value);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @param string $original_format
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function convertValue($value, $original_format)
|
||||||
|
{
|
||||||
|
switch ($original_format) {
|
||||||
|
case 'datetime_epoch':
|
||||||
|
$datetime = new \DateTime("@$value");
|
||||||
|
return sprintf("%s", $datetime->format("Y-m-d H:i:s"));
|
||||||
|
break;
|
||||||
|
case 'json_int':
|
||||||
|
return intval($value);
|
||||||
|
break;
|
||||||
|
case 'json_string':
|
||||||
|
return sprintf("%s",$value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return $value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getSQLBindings()
|
||||||
|
{
|
||||||
|
return $this->bindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $mappings
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toRawSQL(array $mappings)
|
||||||
|
{
|
||||||
|
$sql = '';
|
||||||
|
$this->bindings = [];
|
||||||
|
$param_prefix = "param_%s";
|
||||||
|
$param_idx = 1;
|
||||||
|
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
if ($filter instanceof FilterElement) {
|
||||||
|
if (isset($mappings[$filter->getField()])) {
|
||||||
|
|
||||||
|
$mapping = $mappings[$filter->getField()];
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $filter->getValue();
|
||||||
|
$op = $filter->getOperator();
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$filter->setValue($this->convertValue($value, $mapping[1]));
|
||||||
|
}
|
||||||
|
$cond = sprintf(' %s %s :%s', $mapping[0], $op, sprintf($param_prefix, $param_idx));
|
||||||
|
$this->bindings[sprintf($param_prefix, $param_idx)] = $filter->getValue();
|
||||||
|
++$param_idx;
|
||||||
|
if (!empty($sql)) $sql .= " AND ";
|
||||||
|
$sql .= $cond;
|
||||||
|
}
|
||||||
|
} else if (is_array($filter)) {
|
||||||
|
// OR
|
||||||
|
$sql .= " ( ";
|
||||||
|
$sql_or = '';
|
||||||
|
foreach ($filter as $e) {
|
||||||
|
if ($e instanceof FilterElement && isset($mappings[$e->getField()])) {
|
||||||
|
$mapping = $mappings[$e->getField()];
|
||||||
|
$mapping = explode(':', $mapping);
|
||||||
|
$value = $e->getValue();
|
||||||
|
$op = $e->getOperator();
|
||||||
|
if (count($mapping) > 1) {
|
||||||
|
$e->setValue($this->convertValue($value, $mapping[1]));
|
||||||
|
}
|
||||||
|
$cond = sprintf(" %s %s :%s", $mapping[0], $op, sprintf($param_prefix, $param_idx));
|
||||||
|
$this->bindings[sprintf($param_prefix, $param_idx)] = $e->getValue();
|
||||||
|
++$param_idx;
|
||||||
|
if (!empty($sql_or)) $sql_or .= " OR ";
|
||||||
|
$sql_or .= $cond;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$sql .= $sql_or . " ) ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getFilterCollectionByField($field){
|
||||||
|
$list = [];
|
||||||
|
$filter = $this->getFilter($field);
|
||||||
|
|
||||||
|
if(is_array($filter)){
|
||||||
|
if(is_array($filter[0])){
|
||||||
|
foreach ($filter[0] as $filter_element)
|
||||||
|
$list[] = intval($filter_element->getValue());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$list[] = intval($filter[0]->getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
}
|
106
app/Http/Utils/Filters/FilterElement.php
Normal file
106
app/Http/Utils/Filters/FilterElement.php
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
class FilterElement extends AbstractFilterElement
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $value;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $field
|
||||||
|
* @param $value
|
||||||
|
* @param $operator
|
||||||
|
*/
|
||||||
|
protected function __construct($field, $value, $operator)
|
||||||
|
{
|
||||||
|
parent::__construct($operator);
|
||||||
|
$this->field = $field;
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setValue($value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getField()
|
||||||
|
{
|
||||||
|
return $this->field;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getValue()
|
||||||
|
{
|
||||||
|
switch($this->operator)
|
||||||
|
{
|
||||||
|
case 'like':
|
||||||
|
return empty($this->value) ? '' : "%".$this->value."%";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return $this->value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeEqual($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '=');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeGreather($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '>');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeGreatherOrEqual($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '>=');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeLower($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '<');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeLowerOrEqual($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '<=');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeNotEqual($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, '<>');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function makeLike($field, $value)
|
||||||
|
{
|
||||||
|
return new self($field, $value, 'like');
|
||||||
|
}
|
||||||
|
}
|
48
app/Http/Utils/Filters/FilterMapping.php
Normal file
48
app/Http/Utils/Filters/FilterMapping.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
namespace utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FilterMapping
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
abstract class FilterMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $where;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FilterMapping constructor.
|
||||||
|
* @param string $table
|
||||||
|
* @param string $where
|
||||||
|
*/
|
||||||
|
public function __construct($table, $where)
|
||||||
|
{
|
||||||
|
$this->table = $table;
|
||||||
|
$this->where = $where;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FilterElement $filter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public abstract function toRawSQL(FilterElement $filter);
|
||||||
|
}
|
133
app/Http/Utils/Filters/FilterParser.php
Normal file
133
app/Http/Utils/Filters/FilterParser.php
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
final class FilterParser
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param mixed $filters
|
||||||
|
* @param array $allowed_fields
|
||||||
|
* @throws FilterParserException
|
||||||
|
* @return Filter
|
||||||
|
*/
|
||||||
|
public static function parse($filters, $allowed_fields = [])
|
||||||
|
{
|
||||||
|
$res = [];
|
||||||
|
$matches = [];
|
||||||
|
$and_fields = [];
|
||||||
|
|
||||||
|
if (!is_array($filters))
|
||||||
|
$filters = array($filters);
|
||||||
|
|
||||||
|
foreach ($filters as $filter) // parse AND filters
|
||||||
|
{
|
||||||
|
|
||||||
|
$f = null;
|
||||||
|
// parse OR filters
|
||||||
|
$or_filters = explode(',', $filter);
|
||||||
|
|
||||||
|
if (count($or_filters) > 1) {
|
||||||
|
$f = [];
|
||||||
|
foreach ($or_filters as $of) {
|
||||||
|
|
||||||
|
//single filter
|
||||||
|
preg_match('/[=<>][=>@]{0,1}/', $of, $matches);
|
||||||
|
|
||||||
|
if (count($matches) != 1)
|
||||||
|
throw new FilterParserException(sprintf("invalid OR filter format %s (should be [:FIELD_NAME:OPERAND:VALUE])", $of));
|
||||||
|
|
||||||
|
$op = $matches[0];
|
||||||
|
$operands = explode($op, $of);
|
||||||
|
$field = $operands[0];
|
||||||
|
$value = $operands[1];
|
||||||
|
|
||||||
|
if (!isset($allowed_fields[$field])){
|
||||||
|
throw new FilterParserException(sprintf("filter by field %s is not allowed", $field));
|
||||||
|
}
|
||||||
|
if (!in_array($op, $allowed_fields[$field])){
|
||||||
|
throw new FilterParserException(sprintf("%s op is not allowed for filter by field %s",$op, $field));
|
||||||
|
}
|
||||||
|
|
||||||
|
$f_or = self::buildFilter($field, $op, $value);
|
||||||
|
if (!is_null($f_or))
|
||||||
|
$f[] = $f_or;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//single filter
|
||||||
|
preg_match('/[=<>][=>@]{0,1}/', $filter, $matches);
|
||||||
|
|
||||||
|
if (count($matches) != 1)
|
||||||
|
throw new FilterParserException(sprintf("invalid filter format %s (should be [:FIELD_NAME:OPERAND:VALUE])", $filter));
|
||||||
|
|
||||||
|
$op = $matches[0];
|
||||||
|
$operands = explode($op, $filter);
|
||||||
|
$field = $operands[0];
|
||||||
|
$value = $operands[1];
|
||||||
|
|
||||||
|
if (!isset($allowed_fields[$field])){
|
||||||
|
throw new FilterParserException(sprintf("filter by field %s is not allowed", $field));
|
||||||
|
}
|
||||||
|
if (!in_array($op, $allowed_fields[$field])){
|
||||||
|
throw new FilterParserException(sprintf("%s op is not allowed for filter by field %s",$op, $field));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(in_array($field, $and_fields))
|
||||||
|
throw new FilterParserException(sprintf("filter by field %s is already on an and expression", $field));
|
||||||
|
|
||||||
|
$and_fields[] = $field;
|
||||||
|
$f = self::buildFilter($field, $op, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_null($f))
|
||||||
|
$res[] = $f;
|
||||||
|
}
|
||||||
|
return new Filter($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory Method
|
||||||
|
*
|
||||||
|
* @param string $field
|
||||||
|
* @param string $op
|
||||||
|
* @param string $value
|
||||||
|
* @return FilterElement|null
|
||||||
|
*/
|
||||||
|
public static function buildFilter($field, $op, $value)
|
||||||
|
{
|
||||||
|
switch ($op) {
|
||||||
|
case '==':
|
||||||
|
return FilterElement::makeEqual($field, $value);
|
||||||
|
break;
|
||||||
|
case '=@':
|
||||||
|
return FilterElement::makeLike($field, $value);
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
return FilterElement::makeGreather($field, $value);
|
||||||
|
break;
|
||||||
|
case '>=':
|
||||||
|
return FilterElement::makeGreatherOrEqual($field, $value);
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
return FilterElement::makeLower($field, $value);
|
||||||
|
break;
|
||||||
|
case '<=':
|
||||||
|
return FilterElement::makeLowerOrEqual($field, $value);
|
||||||
|
break;
|
||||||
|
case '<>':
|
||||||
|
return FilterElement::makeNotEqual($field, $value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<?php namespace Models;
|
<?php namespace utils;
|
||||||
/**
|
/**
|
||||||
* Copyright 2016 OpenStack Foundation
|
* Copyright 2016 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -11,12 +11,16 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
use Utils\Model\SilverStripeBaseModel;
|
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MemberPhoto
|
* Class FilterParserException
|
||||||
* @package Models
|
* @package utils
|
||||||
*/
|
*/
|
||||||
class MemberPhoto extends SilverStripeBaseModel
|
final class FilterParserException extends ValidationException
|
||||||
{
|
{
|
||||||
protected $table = 'File';
|
public function __construct($message = "") {
|
||||||
|
parent::__construct($message, 0, null);
|
||||||
|
}
|
||||||
}
|
}
|
39
app/Http/Utils/HTMLCleaner.php
Normal file
39
app/Http/Utils/HTMLCleaner.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class HTMLCleaner
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class HTMLCleaner
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @param array $fields
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function cleanData(array $data, array $fields)
|
||||||
|
{
|
||||||
|
$config = \HTMLPurifier_Config::createDefault();
|
||||||
|
// Remove any CSS or inline styles
|
||||||
|
$config->set('CSS.AllowedProperties', []);
|
||||||
|
$purifier = new \HTMLPurifier($config);
|
||||||
|
foreach($fields as $field){
|
||||||
|
if(!isset($data[$field])) continue;
|
||||||
|
$data[$field] = $purifier->purify($data[$field]);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php namespace Auth\Repositories;
|
<?php namespace App\Http\Utils;
|
||||||
/**
|
/**
|
||||||
* Copyright 2016 OpenStack Foundation
|
* Copyright 2018 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
@ -11,17 +11,20 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
use Models\Member;
|
use models\main\File;
|
||||||
use Utils\Db\IBaseRepository;
|
use Exception;
|
||||||
/**
|
/**
|
||||||
* Interface IMemberRepository
|
* Interface IBucket
|
||||||
* @package Auth\Repositories
|
* @package App\Http\Utils
|
||||||
*/
|
*/
|
||||||
interface IMemberRepository extends IBaseRepository
|
interface IBucket
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param string $email
|
* @param File $f
|
||||||
* @return Member
|
* @param string $local_path
|
||||||
|
* @return object
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getByEmail($email);
|
public function put(File $f, $local_path);
|
||||||
}
|
|
||||||
|
}
|
30
app/Http/Utils/IFileUploader.php
Normal file
30
app/Http/Utils/IFileUploader.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use models\main\File;
|
||||||
|
/**
|
||||||
|
* Interface IFileUploader
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
interface IFileUploader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param UploadedFile $file
|
||||||
|
* @param $folder_name
|
||||||
|
* @param bool $is_image
|
||||||
|
* @return File
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function build(UploadedFile $file, $folder_name, $is_image = false);
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php namespace Utils\Model;
|
<?php namespace App\Http\Utils;
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2019 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
@ -13,13 +13,10 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface IEntity
|
* Interface IUserIPHelperProvider
|
||||||
* @package Utils\Model
|
* @package App\Http\Utils
|
||||||
*/
|
*/
|
||||||
interface IEntity
|
interface IUserIPHelperProvider
|
||||||
{
|
{
|
||||||
/**
|
public function getCurrentUserIpAddress():string;
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getId();
|
|
||||||
}
|
}
|
@ -38,13 +38,13 @@ final class LaravelMailerHandler extends MailHandler
|
|||||||
* Optional headers for the message
|
* Optional headers for the message
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $headers = array();
|
protected $headers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional parameters for the message
|
* Optional parameters for the message
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $parameters = array();
|
protected $parameters = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The wordwrap length for the message
|
* The wordwrap length for the message
|
||||||
|
90
app/Http/Utils/Order.php
Normal file
90
app/Http/Utils/Order.php
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Doctrine\Common\Collections\Criteria;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
/**
|
||||||
|
* Class Order
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class Order
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $ordering;
|
||||||
|
|
||||||
|
public function __construct($ordering = [])
|
||||||
|
{
|
||||||
|
$this->ordering = $ordering;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param QueryBuilder $query
|
||||||
|
* @param array $mappings
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function apply2Query(QueryBuilder $query, array $mappings)
|
||||||
|
{
|
||||||
|
foreach ($this->ordering as $order) {
|
||||||
|
if ($order instanceof OrderElement) {
|
||||||
|
if (isset($mappings[$order->getField()])) {
|
||||||
|
$mapping = $mappings[$order->getField()];
|
||||||
|
$orders[$mapping] = $order->getDirection();
|
||||||
|
$query->addOrderBy($mapping, $order->getDirection());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Criteria $criteria
|
||||||
|
* @param array $mappings
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function apply2Criteria(Criteria $criteria, array $mappings)
|
||||||
|
{
|
||||||
|
$orders = [];
|
||||||
|
foreach ($this->ordering as $order) {
|
||||||
|
if ($order instanceof OrderElement) {
|
||||||
|
if (isset($mappings[$order->getField()])) {
|
||||||
|
$mapping = $mappings[$order->getField()];
|
||||||
|
$orders[$mapping] = $order->getDirection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(count($orders) > 0)
|
||||||
|
$criteria->orderBy($orders);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $mappings
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toRawSQL(array $mappings)
|
||||||
|
{
|
||||||
|
$sql = ' ORDER BY ';
|
||||||
|
foreach ($this->ordering as $order) {
|
||||||
|
if ($order instanceof OrderElement) {
|
||||||
|
if (isset($mappings[$order->getField()])) {
|
||||||
|
$mapping = $mappings[$order->getField()];
|
||||||
|
$sql .= sprintf('%s %s, ', $mapping, $order->getDirection());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return substr($sql, 0 , strlen($sql) - 2);
|
||||||
|
}
|
||||||
|
}
|
72
app/Http/Utils/OrderElement.php
Normal file
72
app/Http/Utils/OrderElement.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OrderElement
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class OrderElement
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $direction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OrderElement constructor.
|
||||||
|
* @param $field
|
||||||
|
* @param $direction
|
||||||
|
*/
|
||||||
|
private function __construct($field, $direction)
|
||||||
|
{
|
||||||
|
$this->field = $field;
|
||||||
|
$this->direction = $direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildAscFor($field)
|
||||||
|
{
|
||||||
|
return new OrderElement($field, 'ASC');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildDescFor($field)
|
||||||
|
{
|
||||||
|
return new OrderElement($field, 'DESC');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getField()
|
||||||
|
{
|
||||||
|
return $this->field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAsc()
|
||||||
|
{
|
||||||
|
return $this->direction === 'ASC';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getDirection()
|
||||||
|
{
|
||||||
|
return $this->direction;
|
||||||
|
}
|
||||||
|
}
|
59
app/Http/Utils/OrderParser.php
Normal file
59
app/Http/Utils/OrderParser.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OrderParser
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class OrderParser
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $orders
|
||||||
|
* @param array $allowed_fields
|
||||||
|
* @return Order
|
||||||
|
* @throws OrderParserException
|
||||||
|
*/
|
||||||
|
public static function parse($orders, $allowed_fields = [])
|
||||||
|
{
|
||||||
|
$res = [];
|
||||||
|
$orders = explode(',', trim($orders));
|
||||||
|
//default ordering is asc
|
||||||
|
foreach($orders as $field)
|
||||||
|
{
|
||||||
|
$element = null;
|
||||||
|
if(strpos($field, '+') === 0)
|
||||||
|
{
|
||||||
|
$field = trim($field,'+');
|
||||||
|
if(!in_array($field, $allowed_fields))
|
||||||
|
throw new OrderParserException(sprintf("order by field %s is not allowed", $field));
|
||||||
|
$element = OrderElement::buildAscFor($field);
|
||||||
|
}
|
||||||
|
else if(strpos($field, '-') === 0)
|
||||||
|
{
|
||||||
|
$field = trim($field,'-');
|
||||||
|
if(!in_array($field, $allowed_fields))
|
||||||
|
throw new OrderParserException(sprintf("order by field %s is not allowed", $field));
|
||||||
|
$element = OrderElement::buildDescFor($field);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(!in_array($field, $allowed_fields))
|
||||||
|
throw new OrderParserException(sprintf("order by field %s is not allowed", $field));
|
||||||
|
$element = OrderElement::buildAscFor($field);
|
||||||
|
}
|
||||||
|
array_push($res, $element);
|
||||||
|
}
|
||||||
|
return new Order($res);
|
||||||
|
}
|
||||||
|
}
|
24
app/Http/Utils/OrderParserException.php
Normal file
24
app/Http/Utils/OrderParserException.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2017 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use models\exceptions\ValidationException;
|
||||||
|
/**
|
||||||
|
* Class OrderParserException
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class OrderParserException extends ValidationException
|
||||||
|
{
|
||||||
|
public function __construct($message = "") {
|
||||||
|
parent::__construct($message, 0, null);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php namespace Services\Exceptions;
|
<?php namespace App\Http\Utils;
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2018 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
@ -11,11 +11,13 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
use Exception;
|
|
||||||
/**
|
/**
|
||||||
* Class ValidationException
|
* Class PagingConstants
|
||||||
* @package Services\Exceptions
|
* @package App\Http\Utils
|
||||||
*/
|
*/
|
||||||
final class ValidationException extends Exception
|
final class PagingConstants
|
||||||
{
|
{
|
||||||
|
const DefaultPageSize = 5; // should be >= MinPageSize and <= MaxPageSize
|
||||||
|
const MinPageSize = 5;
|
||||||
|
const MaxPageSize = 100;
|
||||||
}
|
}
|
67
app/Http/Utils/PagingInfo.php
Normal file
67
app/Http/Utils/PagingInfo.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
class PagingInfo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $page;
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $per_page;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $page
|
||||||
|
* @param int $per_page
|
||||||
|
*/
|
||||||
|
public function __construct($page = 1, $per_page = 10)
|
||||||
|
{
|
||||||
|
$this->page = $page;
|
||||||
|
$this->per_page = $per_page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getCurrentPage()
|
||||||
|
{
|
||||||
|
return $this->page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getPerPage()
|
||||||
|
{
|
||||||
|
return $this->per_page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getOffset()
|
||||||
|
{
|
||||||
|
return ($this->page - 1) * $this->per_page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $count
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getLastPage($count){
|
||||||
|
return intval(ceil($count/$this->per_page));
|
||||||
|
}
|
||||||
|
}
|
129
app/Http/Utils/PagingResponse.php
Normal file
129
app/Http/Utils/PagingResponse.php
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use models\utils\IEntity;
|
||||||
|
use App\ModelSerializers\SerializerRegistry;
|
||||||
|
/**
|
||||||
|
* Class PagingResponse
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class PagingResponse
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $total;
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $per_page;
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $page;
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $last_page;
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $items;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $total
|
||||||
|
* @param int $per_page
|
||||||
|
* @param int $page
|
||||||
|
* @param int $last_page
|
||||||
|
* @param array $items
|
||||||
|
*/
|
||||||
|
public function __construct($total, $per_page, $page, $last_page, array $items)
|
||||||
|
{
|
||||||
|
$this->total = $total;
|
||||||
|
$this->per_page = $per_page;
|
||||||
|
$this->page = $page;
|
||||||
|
$this->last_page = $last_page;
|
||||||
|
$this->items = $items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTotal()
|
||||||
|
{
|
||||||
|
return $this->total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTotalPages():int{
|
||||||
|
return $this->per_page > 0 ? intval(ceil($this->total / $this->per_page)) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getPerPage()
|
||||||
|
{
|
||||||
|
return $this->per_page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getCurrentPage()
|
||||||
|
{
|
||||||
|
return $this->page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getLastPage()
|
||||||
|
{
|
||||||
|
return $this->last_page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getItems()
|
||||||
|
{
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null $expand
|
||||||
|
* @param array $fields
|
||||||
|
* @param array $relations
|
||||||
|
* @param array $params
|
||||||
|
* @param string $serializer_type
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($expand = null, array $fields = [], array $relations = [], array $params = [], $serializer_type = SerializerRegistry::SerializerType_Public )
|
||||||
|
{
|
||||||
|
$items = [];
|
||||||
|
foreach($this->items as $i)
|
||||||
|
{
|
||||||
|
if($i instanceof IEntity)
|
||||||
|
{
|
||||||
|
$i = SerializerRegistry::getInstance()->getSerializer($i, $serializer_type)->serialize($expand, $fields, $relations, $params);
|
||||||
|
}
|
||||||
|
$items[] = $i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
[
|
||||||
|
'total' => $this->total,
|
||||||
|
'per_page' => $this->per_page,
|
||||||
|
'current_page' => $this->page,
|
||||||
|
'last_page' => $this->last_page,
|
||||||
|
'data' => $items,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
375
app/Http/Utils/ParseMultiPartFormDataInputStream.php
Normal file
375
app/Http/Utils/ParseMultiPartFormDataInputStream.php
Normal file
@ -0,0 +1,375 @@
|
|||||||
|
<?php namespace utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2015 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
/**
|
||||||
|
* Class ParseMultiPartFormDataInputStream
|
||||||
|
* @package utils
|
||||||
|
*/
|
||||||
|
final class ParseMultiPartFormDataInputStream
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @abstract Raw input stream
|
||||||
|
*/
|
||||||
|
protected $input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ParseMultiPartFormDataInputStream constructor.
|
||||||
|
* @param $input
|
||||||
|
*/
|
||||||
|
public function __construct($input)
|
||||||
|
{
|
||||||
|
$this->input = $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getInput(){
|
||||||
|
|
||||||
|
$boundary = $this->boundary();
|
||||||
|
|
||||||
|
if (!strlen($boundary)) {
|
||||||
|
return [
|
||||||
|
'parameters' => $this->parse(),
|
||||||
|
'files' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$blocks = $this->split($boundary);
|
||||||
|
|
||||||
|
return $this->blocks($blocks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function boundary
|
||||||
|
* @returns string
|
||||||
|
*/
|
||||||
|
private function boundary()
|
||||||
|
{
|
||||||
|
if(!isset($_SERVER['CONTENT_TYPE'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
|
||||||
|
return $matches[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function parse
|
||||||
|
* @returns array
|
||||||
|
*/
|
||||||
|
private function parse()
|
||||||
|
{
|
||||||
|
parse_str(urldecode($this->input), $result);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function split
|
||||||
|
* @param $boundary string
|
||||||
|
* @returns array
|
||||||
|
*/
|
||||||
|
private function split($boundary)
|
||||||
|
{
|
||||||
|
$result = preg_split("/-+$boundary/", $this->input);
|
||||||
|
array_pop($result);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function blocks
|
||||||
|
* @param $array array
|
||||||
|
* @returns array
|
||||||
|
*/
|
||||||
|
private function blocks($array)
|
||||||
|
{
|
||||||
|
$results = [
|
||||||
|
'parameters' => [],
|
||||||
|
'files' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach($array as $key => $value)
|
||||||
|
{
|
||||||
|
if (empty($value))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$block = $this->decide($value);
|
||||||
|
|
||||||
|
foreach ($block['parameters'] as $key => $val ) {
|
||||||
|
$results['parameters'][$key] = $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $block['files'] as $key => $val ) {
|
||||||
|
$results['files'][$key] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function decide
|
||||||
|
* @param $string string
|
||||||
|
* @returns array
|
||||||
|
*/
|
||||||
|
private function decide($string)
|
||||||
|
{
|
||||||
|
if (strpos($string, 'application/octet-stream') !== FALSE)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'parameters' => $this->file($string),
|
||||||
|
'files' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($string, 'filename') !== FALSE)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'parameters' => [],
|
||||||
|
'files' => $this->file_stream($string)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'parameters' => $this->parameter($string),
|
||||||
|
'files' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function file
|
||||||
|
*
|
||||||
|
* @param $string
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function file($string)
|
||||||
|
{
|
||||||
|
preg_match('/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s', $string, $match);
|
||||||
|
return [
|
||||||
|
$match[1] => ($match[2] !== NULL ? $match[2] : '')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function file_stream
|
||||||
|
*
|
||||||
|
* @param $string
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function file_stream($data)
|
||||||
|
{
|
||||||
|
$result = [];
|
||||||
|
$data = ltrim($data);
|
||||||
|
|
||||||
|
$idx = strpos( $data, "\r\n\r\n" );
|
||||||
|
if ( $idx === FALSE ) {
|
||||||
|
Log::warning( "ParseMultiPartFormDataInputStream.file_stream(): Could not locate header separator in data:" );
|
||||||
|
Log::warning( $data );
|
||||||
|
} else {
|
||||||
|
$headers = substr( $data, 0, $idx );
|
||||||
|
$content = substr( $data, $idx + 4, -2 ); // Skip the leading \r\n and strip the final \r\n
|
||||||
|
|
||||||
|
$name = '-unknown-';
|
||||||
|
$filename = '-unknown-';
|
||||||
|
$filetype = 'application/octet-stream';
|
||||||
|
|
||||||
|
$header = strtok( $headers, "\r\n" );
|
||||||
|
while ( $header !== FALSE ) {
|
||||||
|
if ( substr($header, 0, strlen("Content-Disposition: ")) == "Content-Disposition: " ) {
|
||||||
|
// Content-Disposition: form-data; name="attach_file[TESTING]"; filename="label2.jpg"
|
||||||
|
if ( preg_match('/name=\"([^\"]*)\"/', $header, $nmatch ) ) {
|
||||||
|
$name = $nmatch[1];
|
||||||
|
}
|
||||||
|
if ( preg_match('/filename=\"([^\"]*)\"/', $header, $nmatch ) ) {
|
||||||
|
$filename = $nmatch[1];
|
||||||
|
}
|
||||||
|
} elseif ( substr($header, 0, strlen("Content-Type: ")) == "Content-Type: " ) {
|
||||||
|
// Content-Type: image/jpg
|
||||||
|
$filetype = trim( substr($header, strlen("Content-Type: ")) );
|
||||||
|
} else {
|
||||||
|
Log::debug( "PARSEINPUTSTREAM: Skipping Header: " . $header );
|
||||||
|
}
|
||||||
|
|
||||||
|
$header = strtok("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( substr($data, -2) === "\r\n" ) {
|
||||||
|
$data = substr($data, 0, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = sys_get_temp_dir() . '/php' . substr( sha1(rand()), 0, 6 );
|
||||||
|
|
||||||
|
$bytes = file_put_contents( $path, $content );
|
||||||
|
|
||||||
|
if ( $bytes !== FALSE ) {
|
||||||
|
$file = new UploadedFile( $path, $filename, $filetype, $bytes, UPLOAD_ERR_OK );
|
||||||
|
$result = array( $name => $file );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function parameter
|
||||||
|
*
|
||||||
|
* @param $string
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function parameter($string)
|
||||||
|
{
|
||||||
|
$string = trim($string);
|
||||||
|
$data = [];
|
||||||
|
if ( preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)$/s', $string, $match) ) {
|
||||||
|
$val = ($match[2] !== NULL ? $match[2] : '');
|
||||||
|
if(!empty($val) && is_bool($val))
|
||||||
|
$val = boolval($val);
|
||||||
|
if(!empty($val) && is_string($val) && self::checkBool($val))
|
||||||
|
$val = self::boolVal($val);
|
||||||
|
if(!empty($val) && is_int($val))
|
||||||
|
$val = intval($val);
|
||||||
|
if(!empty($val) && is_double($val))
|
||||||
|
$val = doubleval($val);
|
||||||
|
if (preg_match('/^(.*)\[\]$/i', $match[1], $tmp)) {
|
||||||
|
$data[$tmp[1]][] = $val;
|
||||||
|
} else {
|
||||||
|
$data[$match[1]] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function checkBool($string){
|
||||||
|
$string = strtolower($string);
|
||||||
|
return (in_array($string, array("true", "false", "1", "0", "yes", "no"), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
static function boolVal($string){
|
||||||
|
$string = strtolower($string);
|
||||||
|
if(in_array($string, ["true", "1", "yes"])) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function merge
|
||||||
|
* @param $array array
|
||||||
|
*
|
||||||
|
* Ugly ugly ugly
|
||||||
|
*
|
||||||
|
* @returns array
|
||||||
|
*/
|
||||||
|
private function merge($array)
|
||||||
|
{
|
||||||
|
$results = [
|
||||||
|
'parameters' => [],
|
||||||
|
'files' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
if (count($array['parameters']) > 0) {
|
||||||
|
foreach($array['parameters'] as $key => $value) {
|
||||||
|
foreach($value as $k => $v) {
|
||||||
|
if (is_array($v)) {
|
||||||
|
foreach($v as $kk => $vv) {
|
||||||
|
$results['parameters'][$k][] = $vv;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$results['parameters'][$k] = $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($array['files']) > 0) {
|
||||||
|
foreach($array['files'] as $key => $value) {
|
||||||
|
foreach($value as $k => $v) {
|
||||||
|
if (is_array($v)) {
|
||||||
|
foreach($v as $kk => $vv) {
|
||||||
|
if(is_array($vv) && (count($vv) === 1)) {
|
||||||
|
$results['files'][$k][$kk] = $vv[0];
|
||||||
|
} else {
|
||||||
|
$results['files'][$k][$kk][] = $vv[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$results['files'][$k][$key] = $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_parameter( &$params, $parameter, $value ) {
|
||||||
|
if ( strpos($parameter, '[') !== FALSE ) {
|
||||||
|
$matches = [];
|
||||||
|
if ( preg_match( '/^([^[]*)\[([^]]*)\](.*)$/', $parameter, $match ) ) {
|
||||||
|
$name = $match[1];
|
||||||
|
$key = $match[2];
|
||||||
|
$rem = $match[3];
|
||||||
|
|
||||||
|
if ( $name !== '' && $name !== NULL ) {
|
||||||
|
if ( ! isset($params[$name]) || ! is_array($params[$name]) ) {
|
||||||
|
$params[$name] = [];
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
if ( strlen($rem) > 0 ) {
|
||||||
|
if ( $key === '' || $key === NULL ) {
|
||||||
|
$arr = [];
|
||||||
|
$this->parse_parameter( $arr, $rem, $value );
|
||||||
|
$params[$name][] = $arr;
|
||||||
|
} else {
|
||||||
|
if ( !isset($params[$name][$key]) || !is_array($params[$name][$key]) ) {
|
||||||
|
$params[$name][$key] = [];
|
||||||
|
}
|
||||||
|
$this->parse_parameter( $params[$name][$key], $rem, $value );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ( $key === '' || $key === NULL ) {
|
||||||
|
$params[$name][] = $value;
|
||||||
|
} else {
|
||||||
|
$params[$name][$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ( strlen($rem) > 0 ) {
|
||||||
|
if ( $key === '' || $key === NULL ) {
|
||||||
|
// REVIEW Is this logic correct?!
|
||||||
|
$this->parse_parameter( $params, $rem, $value );
|
||||||
|
} else {
|
||||||
|
if ( ! isset($params[$key]) || ! is_array($params[$key]) ) {
|
||||||
|
$params[$key] = [];
|
||||||
|
}
|
||||||
|
$this->parse_parameter( $params[$key], $rem, $value );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ( $key === '' || $key === NULL ) {
|
||||||
|
$params[] = $value;
|
||||||
|
} else {
|
||||||
|
$params[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log::warning( "ParseMultiPartFormDataInputStream.parse_parameter() Parameter name regex failed: '" . $parameter . "'" );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$params[$parameter] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
102
app/Http/Utils/SwiftBucket.php
Normal file
102
app/Http/Utils/SwiftBucket.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2018 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use models\main\File;
|
||||||
|
use OpenStack\ObjectStore\v1\Models\StorageObject;
|
||||||
|
use OpenStack\OpenStack;
|
||||||
|
use GuzzleHttp\Psr7\Stream;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Exception;
|
||||||
|
/**
|
||||||
|
* Class SwiftBucket
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class SwiftBucket implements IBucket
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var StorageObject
|
||||||
|
*/
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \OpenStack\ObjectStore\v1\Models\Container|StorageObject
|
||||||
|
*/
|
||||||
|
protected function getContainer()
|
||||||
|
{
|
||||||
|
if (!isset($this->container)) {
|
||||||
|
|
||||||
|
$configOptions = [
|
||||||
|
'authUrl' => Config::get("cloudstorage.auth_url"),
|
||||||
|
'region' => Config::get("cloudstorage.region"),
|
||||||
|
];
|
||||||
|
|
||||||
|
$userName = Config::get("cloudstorage.user_name");
|
||||||
|
$userPassword = Config::get("cloudstorage.api_key");
|
||||||
|
|
||||||
|
if(!empty($userName) && !empty($userPassword)){
|
||||||
|
$configOptions['user'] = [
|
||||||
|
'name' => $userName,
|
||||||
|
'password' => $userPassword,
|
||||||
|
'domain' => ['id' => Config::get("cloudstorage.user_domain", "default")]
|
||||||
|
];
|
||||||
|
|
||||||
|
$configOptions['scope' ] = [
|
||||||
|
'project' => [
|
||||||
|
'name' => Config::get("cloudstorage.project_name"),
|
||||||
|
'domain' => ['id' => Config::get("cloudstorage.project_domain", "default")]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$appCredentialId = Config::get("cloudstorage.app_credential_id");
|
||||||
|
$appCredentialSecret = Config::get("cloudstorage.app_credential_secret");
|
||||||
|
|
||||||
|
if(!empty($appCredentialId) && !empty($appCredentialSecret)){
|
||||||
|
$configOptions['application_credential'] = [
|
||||||
|
'id' => $appCredentialId,
|
||||||
|
'secret' => $appCredentialSecret,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$openstack = new OpenStack($configOptions);
|
||||||
|
|
||||||
|
$this->container = $openstack->objectStoreV1()->getContainer( Config::get("cloudstorage.assets_container"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->container;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param File $f
|
||||||
|
* @param string $local_path
|
||||||
|
* @return object|StorageObject
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function put(File $f, $local_path)
|
||||||
|
{
|
||||||
|
|
||||||
|
$fp = fopen($local_path, 'r');
|
||||||
|
if (!$fp) {
|
||||||
|
throw new Exception("Unable to open file: " . $f->getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = [
|
||||||
|
'name' => $f->getRelativeLinkFor(),
|
||||||
|
'stream' => new Stream($fp)
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this->getContainer()->createObject($options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
26
app/Http/Utils/UserIPHelperProvider.php
Normal file
26
app/Http/Utils/UserIPHelperProvider.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php namespace App\Http\Utils;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Illuminate\Support\Facades\Request;
|
||||||
|
/**
|
||||||
|
* Class UserIPHelperProvider
|
||||||
|
* @package App\Http\Utils
|
||||||
|
*/
|
||||||
|
final class UserIPHelperProvider implements IUserIPHelperProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
public function getCurrentUserIpAddress(): string
|
||||||
|
{
|
||||||
|
return Request::server('REMOTE_ADDR');
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php namespace Factories;
|
<?php namespace App\Http\Utils;
|
||||||
/**
|
/**
|
||||||
* Copyright 2015 OpenStack Foundation
|
* Copyright 2019 OpenStack Foundation
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
@ -11,16 +11,15 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
use Illuminate\Support\Facades\App;
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
/**
|
/**
|
||||||
* Class FactoriesProvider
|
* Class UtilsProvider
|
||||||
|
* @package App\Http\Utils
|
||||||
*/
|
*/
|
||||||
final class FactoriesProvider extends ServiceProvider
|
final class UtilsProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
protected $defer = true;
|
protected $defer = false;
|
||||||
|
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
@ -28,11 +27,9 @@ final class FactoriesProvider extends ServiceProvider
|
|||||||
|
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
App::singleton(\OAuth2\Factories\IOAuth2ClientFactory::class, \Factories\OAuth2ClientFactory::class);
|
// file uploadedr service
|
||||||
}
|
App::singleton(IBucket::class, SwiftBucket::class);
|
||||||
|
App::singleton(IFileUploader ::class, FileUploader::class);
|
||||||
public function provides()
|
App::singleton(IUserIPHelperProvider::class, UserIPHelperProvider::class);
|
||||||
{
|
|
||||||
return [\OAuth2\Factories\IOAuth2ClientFactory::class];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -35,6 +35,45 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
Route::get('/accounts/openid2', 'OpenIdProviderController@endpoint');
|
Route::get('/accounts/openid2', 'OpenIdProviderController@endpoint');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//user interaction
|
||||||
|
Route::group(array('prefix' => 'auth'), function () {
|
||||||
|
Route::group(array('prefix' => 'login'), function () {
|
||||||
|
Route::get('', "UserController@getLogin");
|
||||||
|
Route::post('', ['middleware' => 'csrf', 'uses' => 'UserController@postLogin']);
|
||||||
|
Route::get('cancel', "UserController@cancelLogin");
|
||||||
|
});
|
||||||
|
|
||||||
|
// registration routes
|
||||||
|
Route::group(array('prefix' => 'register'), function () {
|
||||||
|
Route::get('', 'Auth\RegisterController@showRegistrationForm');
|
||||||
|
Route::post('', ['middleware' => 'csrf', 'uses' => 'Auth\RegisterController@register']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::group(array('prefix' => 'verification'), function () {
|
||||||
|
Route::get('', 'Auth\EmailVerificationController@showVerificationForm');
|
||||||
|
Route::get('{token}', 'Auth\EmailVerificationController@verify')->name("verification_verify");
|
||||||
|
Route::post('', ['middleware' => 'csrf', 'uses' => 'Auth\EmailVerificationController@resend']);
|
||||||
|
});
|
||||||
|
|
||||||
|
// password reset routes
|
||||||
|
|
||||||
|
Route::group(array('prefix' => 'password'), function () {
|
||||||
|
Route::group(array('prefix' => 'set'), function () {
|
||||||
|
Route::get('{token}', 'Auth\PasswordSetController@showPasswordSetForm')->name('password.set');
|
||||||
|
Route::post('', ['middleware' => 'csrf', 'uses' => 'Auth\PasswordSetController@setPassword']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::group(array('prefix' => 'reset'), function () {
|
||||||
|
Route::get('', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
|
||||||
|
Route::get('{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
|
||||||
|
Route::post('', ['middleware' => 'csrf', 'uses' => 'Auth\ResetPasswordController@reset']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::post('email', ['middleware' => 'csrf', 'uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail'])->name('password.email');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the Claimed Identifier was not previously discovered by the Relying Party
|
* If the Claimed Identifier was not previously discovered by the Relying Party
|
||||||
* (the "openid.identity" in the request was "http://specs.openid.net/auth/2.0/identifier_select"
|
* (the "openid.identity" in the request was "http://specs.openid.net/auth/2.0/identifier_select"
|
||||||
@ -43,10 +82,6 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
* the response to make sure that the OP is authorized to make assertions about the Claimed Identifier.
|
* the response to make sure that the OP is authorized to make assertions about the Claimed Identifier.
|
||||||
*/
|
*/
|
||||||
Route::get("/{identifier}", "UserController@getIdentity");
|
Route::get("/{identifier}", "UserController@getIdentity");
|
||||||
//user interaction
|
|
||||||
Route::get('/accounts/user/login', "UserController@getLogin");
|
|
||||||
Route::post('/accounts/user/login', ['middleware' => 'csrf', 'uses' => 'UserController@postLogin']);
|
|
||||||
Route::get('/accounts/user/login/cancel', "UserController@cancelLogin");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//oauth2 endpoints
|
//oauth2 endpoints
|
||||||
@ -80,9 +115,8 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
Route::get('/accounts/user/consent', "UserController@getConsent");
|
Route::get('/accounts/user/consent', "UserController@getConsent");
|
||||||
Route::post('/accounts/user/consent', ['middleware' => 'csrf', 'uses' => 'UserController@postConsent']);
|
Route::post('/accounts/user/consent', ['middleware' => 'csrf', 'uses' => 'UserController@postConsent']);
|
||||||
Route::any("/accounts/user/logout", "UserController@logout");
|
Route::any("/accounts/user/logout", "UserController@logout");
|
||||||
Route::any("/accounts/user/profile", "UserController@getProfile");
|
Route::get("/accounts/user/profile", "UserController@getProfile");
|
||||||
Route::any("/accounts/user/profile/trusted_site/delete/{id}", "UserController@deleteTrustedSite");
|
Route::any("/accounts/user/profile/trusted_site/delete/{id}", "UserController@deleteTrustedSite");
|
||||||
Route::post('/accounts/user/profile/update', 'UserController@postUserProfileOptions');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::group(['prefix' => 'admin', 'middleware' => ['ssl', 'auth']], function () {
|
Route::group(['prefix' => 'admin', 'middleware' => ['ssl', 'auth']], function () {
|
||||||
@ -92,7 +126,7 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
Route::get('/grants', 'AdminController@editIssuedGrants');
|
Route::get('/grants', 'AdminController@editIssuedGrants');
|
||||||
|
|
||||||
//oauth2 server admin UI
|
//oauth2 server admin UI
|
||||||
Route::group(array('middleware' => ['oauth2.currentuser.serveradmin']), function () {
|
Route::group(['middleware' => ['oauth2.currentuser.serveradmin']], function () {
|
||||||
Route::get('/api-scope-groups', 'AdminController@listApiScopeGroups');
|
Route::get('/api-scope-groups', 'AdminController@listApiScopeGroups');
|
||||||
Route::get('/api-scope-groups/{id}', 'AdminController@editApiScopeGroup');
|
Route::get('/api-scope-groups/{id}', 'AdminController@editApiScopeGroup');
|
||||||
Route::get('/resource-servers', 'AdminController@listResourceServers');
|
Route::get('/resource-servers', 'AdminController@listResourceServers');
|
||||||
@ -103,13 +137,26 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
Route::get('/locked-clients', 'AdminController@listLockedClients');
|
Route::get('/locked-clients', 'AdminController@listLockedClients');
|
||||||
// server private keys
|
// server private keys
|
||||||
Route::get('/private-keys', 'AdminController@listServerPrivateKeys');
|
Route::get('/private-keys', 'AdminController@listServerPrivateKeys');
|
||||||
|
//security
|
||||||
|
Route::group(array('prefix' => 'users'), function () {
|
||||||
|
Route::get('', 'AdminController@listUsers');
|
||||||
|
Route::group(array('prefix' => '{user_id}'), function () {
|
||||||
|
Route::get('', 'AdminController@editUser');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::group(array('prefix' => 'groups'), function () {
|
||||||
|
Route::get('', 'AdminController@listGroups');
|
||||||
|
Route::group(array('prefix' => '{group_id}'), function () {
|
||||||
|
Route::get('', 'AdminController@editGroup');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::group(array('middleware' => ['openstackid.currentuser.serveradmin']), function () {
|
Route::group(array('middleware' => ['openstackid.currentuser.serveradmin']), function () {
|
||||||
Route::get('/locked-users', 'AdminController@listLockedUsers');
|
Route::get('server-config', 'AdminController@listServerConfig');
|
||||||
Route::get('/server-config', 'AdminController@listServerConfig');
|
Route::post('server-config', 'AdminController@saveServerConfig');
|
||||||
Route::post('/server-config', 'AdminController@saveServerConfig');
|
Route::get('banned-ips', 'AdminController@listBannedIPs');
|
||||||
Route::get('/banned-ips', 'AdminController@listBannedIPs');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -120,120 +167,199 @@ Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'web' ], fu
|
|||||||
'prefix' => 'admin/api/v1',
|
'prefix' => 'admin/api/v1',
|
||||||
'middleware' => ['ssl', 'auth']], function () {
|
'middleware' => ['ssl', 'auth']], function () {
|
||||||
|
|
||||||
Route::group(array('prefix' => 'users'), function () {
|
Route::group(['prefix' => 'users'], function () {
|
||||||
Route::delete('/{id}/locked', array('middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => 'UserApiController@unlock'));
|
Route::delete('/me/tokens/{value}',"UserApiController@revokeMyToken");
|
||||||
Route::delete('/{id}/token/{value}', array('middleware' => ['currentuser.checkroute'], 'uses' => 'UserApiController@revokeToken'));
|
Route::get('' , "UserApiController@getAll");
|
||||||
Route::get('/fetch', array('uses' => "UserApiController@fetch"));
|
Route::post('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => "UserApiController@create"]);
|
||||||
|
Route::put('me', "UserApiController@updateMe");
|
||||||
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'locked'], function(){
|
||||||
|
Route::put('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => 'UserApiController@unlock']);
|
||||||
|
Route::delete('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => 'UserApiController@lock']);
|
||||||
|
});
|
||||||
|
Route::get('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => "UserApiController@get"]);
|
||||||
|
Route::delete('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' =>"UserApiController@delete"]);
|
||||||
|
Route::put('', ['middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' =>"UserApiController@update"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::group(array('prefix' => 'banned-ips', 'middleware' => ['openstackid.currentuser.serveradmin.json']), function () {
|
|
||||||
Route::get('/{id}', "ApiBannedIPController@get");
|
Route::group(['prefix' => 'groups', 'middleware' => ['openstackid.currentuser.serveradmin.json']], function () {
|
||||||
Route::get('/', "ApiBannedIPController@getByPage");
|
Route::get('', "GroupApiController@getAll");
|
||||||
Route::delete('/{id?}', "ApiBannedIPController@delete");
|
Route::post('', "GroupApiController@create");
|
||||||
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
|
Route::get('', "GroupApiController@get");
|
||||||
|
Route::delete('', "GroupApiController@delete");
|
||||||
|
Route::put('', "GroupApiController@update");
|
||||||
|
Route::group(['prefix' => 'users'], function(){
|
||||||
|
Route::get('', "GroupApiController@getUsersFromGroup");
|
||||||
|
Route::group(['prefix' => '{user_id}'], function(){
|
||||||
|
Route::put('','GroupApiController@addUserToGroup');
|
||||||
|
Route::delete('','GroupApiController@removeUserFromGroup');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'banned-ips', 'middleware' => ['openstackid.currentuser.serveradmin.json']], function () {
|
||||||
|
Route::get('/', "ApiBannedIPController@getAll");
|
||||||
|
Route::group(['prefix' => '{id?}'], function(){
|
||||||
|
Route::get('', "ApiBannedIPController@get");
|
||||||
|
Route::delete('', "ApiBannedIPController@delete");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//client api
|
//client api
|
||||||
Route::group(array('prefix' => 'clients'), function () {
|
Route::group(array('prefix' => 'clients'), function () {
|
||||||
|
|
||||||
// public keys
|
Route::get('', 'ClientApiController@getAll');
|
||||||
Route::post('/{id}/public_keys', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@create'));
|
Route::post('', 'ClientApiController@create');
|
||||||
Route::get('/{id}/public_keys', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@getByPage'));
|
|
||||||
Route::delete('/{id}/public_keys/{public_key_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@delete'));
|
|
||||||
Route::put('/{id}/public_keys/{public_key_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@update'));
|
|
||||||
|
|
||||||
Route::post('/', array('middleware' => ['currentuser.checkroute'], 'uses' => 'ClientApiController@create'));
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::put('/', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@update'));
|
Route::get('', "ClientApiController@get");
|
||||||
Route::get('/{id}', "ClientApiController@get");
|
Route::put('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@update'));
|
||||||
Route::get('/', array('middleware' => ['currentuser.checkroute'], 'uses' => 'ClientApiController@getByPage'));
|
Route::delete('', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@delete'));
|
||||||
Route::delete('/{id}', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@delete'));
|
// particular settings
|
||||||
//allowed redirect uris endpoints
|
|
||||||
Route::get('/{id}/uris', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getRegisteredUris'));
|
|
||||||
Route::post('/{id}/uris', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedRedirectUri'));
|
|
||||||
Route::delete('/{id}/uris/{uri_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@deleteClientAllowedUri'));
|
|
||||||
|
|
||||||
//allowedApiResourceServerControllert('/{id}/origins', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@geAllowedOrigins'));
|
Route::delete('lock', array('middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => 'ClientApiController@unlock'));
|
||||||
Route::post('/{id}/origins', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedOrigin'));
|
Route::put('secret', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@regenerateClientSecret'));
|
||||||
Route::delete('/{id}/origins/{origin_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@deleteClientAllowedOrigin'));
|
Route::put('use-refresh-tokens/{use_refresh_token}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@setRefreshTokenClient'));
|
||||||
Route::delete('/{id}/lock', array('middleware' => ['openstackid.currentuser.serveradmin.json'], 'uses' => 'ClientApiController@unlock'));
|
Route::put('rotate-refresh-tokens/{rotate_refresh_token}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@setRotateRefreshTokenPolicy'));
|
||||||
Route::put('/{id}/secret', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@regenerateClientSecret'));
|
Route::get('access-tokens', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getAccessTokens'));
|
||||||
Route::put('/{id}/use-refresh-token', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@setRefreshTokenClient'));
|
Route::get('refresh-tokens', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getRefreshTokens'));
|
||||||
Route::put('/{id}/rotate-refresh-token', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@setRotateRefreshTokenPolicy'));
|
|
||||||
Route::get('/{id}/access-token', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getAccessTokens'));
|
|
||||||
Route::get('/{id}/refresh-token', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getRefreshTokens'));
|
|
||||||
Route::get('/me/access-tokens', array('middleware' => [], 'uses' => 'ClientApiController@getAccessTokensByCurrentUser'));
|
|
||||||
Route::get('/me/refresh-tokens', array('middleware' => [], 'uses' => 'ClientApiController@getRefreshTokensByCurrentUser'));
|
|
||||||
Route::delete('/{id}/token/{value}/{hint}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@revokeToken'));
|
|
||||||
Route::put('/{id}/scopes/{scope_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedScope'));
|
|
||||||
Route::delete('/{id}/scopes/{scope_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@removeAllowedScope'));
|
|
||||||
Route::put('/{id}/active', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@activate'));
|
|
||||||
Route::delete('/{id}/active', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@deactivate'));
|
|
||||||
|
|
||||||
|
// public keys
|
||||||
|
Route::group(['prefix' => 'public_keys'], function(){
|
||||||
|
Route::post('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@_create'));
|
||||||
|
Route::get('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@getAll'));
|
||||||
|
Route::group(['prefix' => '{public_key_id}'], function(){
|
||||||
|
Route::delete('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@_delete'));
|
||||||
|
Route::put('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientPublicKeyApiController@_update'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//allowed redirect uris endpoints
|
||||||
|
Route::group(['prefix' => 'uris'], function(){
|
||||||
|
Route::get('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@getRegisteredUris'));
|
||||||
|
Route::post('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedRedirectUri'));
|
||||||
|
Route::delete('{uri_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@deleteClientAllowedUri'));
|
||||||
|
});
|
||||||
|
|
||||||
|
// allowed origins
|
||||||
|
Route::group(['prefix' => 'origins'], function(){
|
||||||
|
Route::post('', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedOrigin'));
|
||||||
|
Route::delete('{origin_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@deleteClientAllowedOrigin'));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::delete('token/{value}/{hint}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@revokeToken'));
|
||||||
|
// scopes
|
||||||
|
Route::group(['prefix' => 'scopes'], function(){
|
||||||
|
Route::put('{scope_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@addAllowedScope'));
|
||||||
|
Route::delete('{scope_id}', array('middleware' => ['oauth2.currentuser.allow.client.edition'], 'uses' => 'ClientApiController@removeAllowedScope'));
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::put('active', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@activate'));
|
||||||
|
Route::delete('active', array('middleware' => ['oauth2.currentuser.owns.client'], 'uses' => 'ClientApiController@deactivate'));
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'me'], function(){
|
||||||
|
Route::get('access-tokens', array('middleware' => [], 'uses' => 'ClientApiController@getAccessTokensByCurrentUser'));
|
||||||
|
Route::get('refresh-tokens', array('middleware' => [], 'uses' => 'ClientApiController@getRefreshTokensByCurrentUser'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// resource servers
|
// resource servers
|
||||||
Route::group(array('prefix' => 'resource-servers', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(array('prefix' => 'resource-servers', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
||||||
Route::get('/{id}', "ApiResourceServerController@get");
|
|
||||||
Route::get('/', "ApiResourceServerController@getByPage");
|
Route::get('', "ApiResourceServerController@getAll");
|
||||||
Route::post('/', "ApiResourceServerController@create");
|
Route::post('', "ApiResourceServerController@create");
|
||||||
Route::delete('/{id}', "ApiResourceServerController@delete");
|
|
||||||
Route::put('/', "ApiResourceServerController@update");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::put('/{id}/client-secret', "ApiResourceServerController@regenerateClientSecret");
|
Route::get('', "ApiResourceServerController@get");
|
||||||
Route::put('/{id}/active', "ApiResourceServerController@activate");
|
Route::delete('', "ApiResourceServerController@delete");
|
||||||
Route::delete('/{id}/active', "ApiResourceServerController@deactivate");
|
Route::put('', "ApiResourceServerController@update");
|
||||||
|
Route::put('client-secret', "ApiResourceServerController@regenerateClientSecret");
|
||||||
|
Route::put('active', "ApiResourceServerController@activate");
|
||||||
|
Route::delete('active', "ApiResourceServerController@deactivate");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// api scope groups
|
// api scope groups
|
||||||
Route::group(array('prefix' => 'api-scope-groups', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(['prefix' => 'api-scope-groups', 'middleware' => ['oauth2.currentuser.serveradmin.json']], function () {
|
||||||
Route::get('/{id}', "ApiScopeGroupController@get");
|
Route::get('', "ApiScopeGroupController@getAll");
|
||||||
Route::get('/', "ApiScopeGroupController@getByPage");
|
Route::post('', "ApiScopeGroupController@create");
|
||||||
Route::put('/', "ApiScopeGroupController@update");
|
|
||||||
Route::post('/', "ApiScopeGroupController@create");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::delete('/{id}', "ApiScopeGroupController@delete");
|
Route::put('', "ApiScopeGroupController@update");
|
||||||
Route::put('/{id}/active', "ApiScopeGroupController@activate");
|
Route::get('', "ApiScopeGroupController@get");
|
||||||
Route::delete('/{id}/active', "ApiScopeGroupController@deactivate");
|
Route::delete('', "ApiScopeGroupController@delete");
|
||||||
|
Route::put('/active', "ApiScopeGroupController@activate");
|
||||||
|
Route::delete('/active', "ApiScopeGroupController@deactivate");
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// apis
|
// apis
|
||||||
Route::group(array('prefix' => 'apis', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(['prefix' => 'apis', 'middleware' => ['oauth2.currentuser.serveradmin.json']], function () {
|
||||||
Route::get('/{id}', "ApiController@get");
|
|
||||||
Route::get('/', "ApiController@getByPage");
|
Route::get('', "ApiController@getAll");
|
||||||
Route::post('/', "ApiController@create");
|
Route::post('', "ApiController@create");
|
||||||
Route::delete('/{id}', "ApiController@delete");
|
|
||||||
Route::put('/', "ApiController@update");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::put('/{id}/active', "ApiController@activate");
|
Route::get('', "ApiController@get");
|
||||||
Route::delete('/{id}/active', "ApiController@deactivate");
|
Route::delete('', "ApiController@delete");
|
||||||
|
Route::put('', "ApiController@update");
|
||||||
|
Route::put('/active', "ApiController@activate");
|
||||||
|
Route::delete('/active', "ApiController@deactivate");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// scopes
|
// scopes
|
||||||
Route::group(array('prefix' => 'scopes', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(['prefix' => 'scopes', 'middleware' => ['oauth2.currentuser.serveradmin.json']], function () {
|
||||||
Route::get('/{id}', "ApiScopeController@get");
|
|
||||||
Route::get('/', "ApiScopeController@getByPage");
|
Route::get('/', "ApiScopeController@getAll");
|
||||||
Route::post('/', "ApiScopeController@create");
|
Route::post('/', "ApiScopeController@create");
|
||||||
Route::delete('/{id}', "ApiScopeController@delete");
|
|
||||||
Route::put('/', "ApiScopeController@update");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::put('/{id}/active', "ApiScopeController@activate");
|
Route::get('', "ApiScopeController@get");
|
||||||
Route::delete('/{id}/active', "ApiScopeController@deactivate");
|
Route::delete('', "ApiScopeController@delete");
|
||||||
|
Route::put('', "ApiScopeController@update");
|
||||||
|
Route::put('/active', "ApiScopeController@activate");
|
||||||
|
Route::delete('/active', "ApiScopeController@deactivate");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// endpoints
|
// endpoints
|
||||||
Route::group(array('prefix' => 'endpoints', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(['prefix' => 'endpoints', 'middleware' => ['oauth2.currentuser.serveradmin.json']], function () {
|
||||||
Route::get('/{id}', "ApiEndpointController@get");
|
|
||||||
Route::get('/', "ApiEndpointController@getByPage");
|
Route::get('', "ApiEndpointController@getAll");
|
||||||
Route::post('/', "ApiEndpointController@create");
|
Route::post('', "ApiEndpointController@create");
|
||||||
Route::delete('/{id}', "ApiEndpointController@delete");
|
|
||||||
Route::put('/', "ApiEndpointController@update");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
Route::put('/{id}/scope/{scope_id}', "ApiEndpointController@addRequiredScope");
|
Route::get('', "ApiEndpointController@get");
|
||||||
Route::delete('/{id}/scope/{scope_id}', "ApiEndpointController@removeRequiredScope");
|
Route::delete('', "ApiEndpointController@delete");
|
||||||
Route::put('/{id}/active', "ApiEndpointController@activate");
|
Route::put('', "ApiEndpointController@update");
|
||||||
Route::delete('/{id}/active', "ApiEndpointController@deactivate");
|
Route::put('/active', "ApiEndpointController@activate");
|
||||||
|
Route::delete('/active', "ApiEndpointController@deactivate");
|
||||||
|
Route::group(['prefix' => 'scope'], function(){
|
||||||
|
Route::group(['prefix' => '{scope_id}'], function(){
|
||||||
|
Route::put('', "ApiEndpointController@addRequiredScope");
|
||||||
|
Route::delete('', "ApiEndpointController@removeRequiredScope");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// private keys
|
// private keys
|
||||||
Route::group(array('prefix' => 'private-keys', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
Route::group(array('prefix' => 'private-keys', 'middleware' => ['oauth2.currentuser.serveradmin.json']), function () {
|
||||||
Route::get('/', "ServerPrivateKeyApiController@getByPage");
|
Route::get('', "ServerPrivateKeyApiController@getAll");
|
||||||
Route::post('/', "ServerPrivateKeyApiController@create");
|
Route::post('', "ServerPrivateKeyApiController@create");
|
||||||
Route::delete('/{id}', "ServerPrivateKeyApiController@delete");
|
|
||||||
Route::put('/{id}', "ServerPrivateKeyApiController@update");
|
Route::group(['prefix' => '{id}'], function(){
|
||||||
|
Route::delete('', "ServerPrivateKeyApiController@delete");
|
||||||
|
Route::put('', "ServerPrivateKeyApiController@update");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -247,9 +373,14 @@ Route::group(
|
|||||||
'middleware' => ['api']
|
'middleware' => ['api']
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
Route::group(array('prefix' => 'users'), function () {
|
Route::group(['prefix' => 'users'], function () {
|
||||||
|
Route::get('', 'OAuth2UserApiController@getAll');
|
||||||
Route::get('/me', 'OAuth2UserApiController@me');
|
Route::get('/me', 'OAuth2UserApiController@me');
|
||||||
Route::get('/info', 'OAuth2UserApiController@userInfo');
|
Route::get('/info', 'OAuth2UserApiController@userInfo');
|
||||||
Route::post('/info', 'OAuth2UserApiController@userInfo');
|
Route::post('/info', 'OAuth2UserApiController@userInfo');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'user-registration-requests'], function(){
|
||||||
|
Route::post('', 'OAuth2UserRegistrationRequestApiController@register');
|
||||||
|
});
|
||||||
});
|
});
|
@ -1,61 +0,0 @@
|
|||||||
<?php namespace App\Listeners;
|
|
||||||
|
|
||||||
use Illuminate\Database\Events\QueryExecuted;
|
|
||||||
use Illuminate\Support\Facades\Config;
|
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use DateTime;
|
|
||||||
|
|
||||||
class QueryExecutedListener
|
|
||||||
{
|
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle the event.
|
|
||||||
*
|
|
||||||
* @param QueryExecuted $event
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function handle(QueryExecuted $event)
|
|
||||||
{
|
|
||||||
if(Config::get("server.db_log_enabled", false)) {
|
|
||||||
|
|
||||||
$query = $event->sql;
|
|
||||||
$bindings = $event->bindings;
|
|
||||||
|
|
||||||
// Format binding data for sql insertion
|
|
||||||
foreach ($bindings as $i => $binding) {
|
|
||||||
if ($binding instanceof DateTime) {
|
|
||||||
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
|
|
||||||
} else {
|
|
||||||
if (is_string($binding)) {
|
|
||||||
$bindings[$i] = "'$binding'";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$time = $event->time;
|
|
||||||
$connection = $event->connectionName;
|
|
||||||
$data = compact('bindings', 'time', 'connection');
|
|
||||||
// Insert bindings into query
|
|
||||||
$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
|
|
||||||
$query = vsprintf($query, $bindings);
|
|
||||||
Log::info($query, $data);
|
|
||||||
|
|
||||||
//trace
|
|
||||||
|
|
||||||
/*$trace = '';
|
|
||||||
$entries = debug_backtrace();
|
|
||||||
unset($entries[0]);
|
|
||||||
foreach($entries as $entry){
|
|
||||||
if(!isset($entry['file']) || !isset($entry['line'])) continue;
|
|
||||||
$trace .= $entry['file'].' '.$entry['line'].PHP_EOL;
|
|
||||||
}
|
|
||||||
Log::debug($trace);*/
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
79
app/Mail/OAuth2ClientLocked.php
Normal file
79
app/Mail/OAuth2ClientLocked.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Models\OAuth2\Client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OAuth2ClientLocked
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
class OAuth2ClientLocked extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $client_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $client_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(Client $client)
|
||||||
|
{
|
||||||
|
$this->client_id = $client->getClientId();
|
||||||
|
$this->client_name = $client->getApplicationName();
|
||||||
|
$this->user_email = $client->getOwner()->getEmail();
|
||||||
|
$this->user_fullname = $client->getOwner()->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.verification_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] Verify Email Address", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.oauth2_client_locked');
|
||||||
|
}
|
||||||
|
}
|
72
app/Mail/UserEmailVerificationRequest.php
Normal file
72
app/Mail/UserEmailVerificationRequest.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserEmailVerificationRequest
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
final class UserEmailVerificationRequest extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $verification_link;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerificationRequest constructor.
|
||||||
|
* @param User $user
|
||||||
|
* @param string $verification_link
|
||||||
|
*/
|
||||||
|
public function __construct(User $user, string $verification_link)
|
||||||
|
{
|
||||||
|
$this->verification_link = $verification_link;
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.verification_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] Verify Email Address", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.auth.email_verification_request');
|
||||||
|
}
|
||||||
|
}
|
63
app/Mail/UserEmailVerificationSuccess.php
Normal file
63
app/Mail/UserEmailVerificationSuccess.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2016 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
/**
|
||||||
|
* Class UserEmailVerificationSuccess
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
class UserEmailVerificationSuccess extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerificationRequest constructor.
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function __construct(User $user)
|
||||||
|
{
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.verification_sucessfull_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] You have verified your email address", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.auth.email_verification_request_success');
|
||||||
|
}
|
||||||
|
}
|
80
app/Mail/UserLockedEmail.php
Normal file
80
app/Mail/UserLockedEmail.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2016 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserLocked
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
final class UserLockedEmail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $support_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $attempts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserLocked constructor.
|
||||||
|
* @param User $user
|
||||||
|
* @param string $support_email
|
||||||
|
* @param int $attempts
|
||||||
|
*/
|
||||||
|
public function __construct(User $user, string $support_email, int $attempts)
|
||||||
|
{
|
||||||
|
$this->support_email = $support_email;
|
||||||
|
$this->attempts = $attempts;
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.locked_user_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] Your User has been locked", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.auth.user_locked');
|
||||||
|
}
|
||||||
|
}
|
64
app/Mail/UserPasswordResetMail.php
Normal file
64
app/Mail/UserPasswordResetMail.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
/**
|
||||||
|
* Class UserPasswordResetMail
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
final class UserPasswordResetMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserPasswordResetMail constructor.
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function __construct(User $user)
|
||||||
|
{
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.reset_password_success_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] Your Password Reset was successful", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.auth.reset_password_successfull');
|
||||||
|
}
|
||||||
|
}
|
73
app/Mail/UserPasswordResetRequestMail.php
Normal file
73
app/Mail/UserPasswordResetRequestMail.php
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserPasswordResetRequestMail
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
final class UserPasswordResetRequestMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $reset_link;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserEmailVerificationRequest constructor.
|
||||||
|
* @param User $user
|
||||||
|
* @param string $reset_link
|
||||||
|
*/
|
||||||
|
public function __construct(User $user, string $reset_link)
|
||||||
|
{
|
||||||
|
$this->reset_link = $reset_link;
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.reset_password_email_subject");
|
||||||
|
if(empty($subject))
|
||||||
|
$subject = sprintf("[%s] Reset Password Notification", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.auth.reset_password_request');
|
||||||
|
}
|
||||||
|
}
|
65
app/Mail/WelcomeNewUserEmail.php
Normal file
65
app/Mail/WelcomeNewUserEmail.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php namespace App\Mail;
|
||||||
|
/**
|
||||||
|
* Copyright 2016 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use Auth\User;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
/**
|
||||||
|
* Class WelcomeNewUserEmail
|
||||||
|
* @package App\Mail
|
||||||
|
*/
|
||||||
|
final class WelcomeNewUserEmail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $user_fullname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WelcomeNewUserEmail constructor.
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function __construct(User $user)
|
||||||
|
{
|
||||||
|
$this->user_email = $user->getEmail();
|
||||||
|
$this->user_fullname = $user->getFullName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
$subject = Config::get("mail.welcome_new_user_email_subject");
|
||||||
|
if (empty($subject))
|
||||||
|
$subject = sprintf("[%s] Welcome, Thanks for registering !!!", Config::get('app.app_name'));
|
||||||
|
|
||||||
|
return $this->from(Config::get("mail.from"))
|
||||||
|
->to($this->user_email)
|
||||||
|
->subject($subject)
|
||||||
|
->view('emails.welcome_new_user_email');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
213
app/ModelSerializers/AbstractSerializer.php
Normal file
213
app/ModelSerializers/AbstractSerializer.php
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
<?php namespace App\ModelSerializers;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use models\utils\IEntity;
|
||||||
|
use OAuth2\IResourceServerContext;
|
||||||
|
use Utils\JsonUtils;
|
||||||
|
/**
|
||||||
|
* Class AbstractSerializer
|
||||||
|
* @package App\ModelSerializers
|
||||||
|
*/
|
||||||
|
abstract class AbstractSerializer implements IModelSerializer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IEntity
|
||||||
|
*/
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IResourceServerContext
|
||||||
|
*/
|
||||||
|
protected $resource_server_context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AbstractSerializer constructor.
|
||||||
|
* @param $object
|
||||||
|
* @param IResourceServerContext $resource_server_context
|
||||||
|
*/
|
||||||
|
public function __construct($object, IResourceServerContext $resource_server_context){
|
||||||
|
$this->object = $object;
|
||||||
|
$this->resource_server_context = $resource_server_context;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static $array_mappings = [];
|
||||||
|
|
||||||
|
protected static $allowed_fields = [];
|
||||||
|
|
||||||
|
protected static $allowed_relations = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAllowedFields()
|
||||||
|
{
|
||||||
|
$mappings = [];
|
||||||
|
$hierarchy = $this->getClassHierarchy();
|
||||||
|
|
||||||
|
foreach($hierarchy as $class_name){
|
||||||
|
if($class_name === AbstractSerializer::class ) continue;
|
||||||
|
$class = new $class_name($this->object, $this->resource_server_context);
|
||||||
|
$mappings = array_merge($mappings, $class->getSelfAllowedFields());
|
||||||
|
}
|
||||||
|
$mappings = array_merge($mappings, $this->getSelfAllowedFields());
|
||||||
|
return $mappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getSelfAllowedFields(){
|
||||||
|
return static::$allowed_fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAllowedRelations()
|
||||||
|
{
|
||||||
|
$mappings = [];
|
||||||
|
$hierarchy = $this->getClassHierarchy();
|
||||||
|
|
||||||
|
foreach($hierarchy as $class_name){
|
||||||
|
if($class_name === AbstractSerializer::class ) continue;
|
||||||
|
$class = new $class_name($this->object, $this->resource_server_context);
|
||||||
|
$mappings = array_merge($mappings, $class->getSelfAllowedRelations());
|
||||||
|
}
|
||||||
|
$mappings = array_merge($mappings, $this->getSelfAllowedRelations());
|
||||||
|
return $mappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getSelfAllowedRelations(){
|
||||||
|
return static::$allowed_relations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getAttributeMappings()
|
||||||
|
{
|
||||||
|
$mappings = [];
|
||||||
|
$hierarchy = $this->getClassHierarchy();
|
||||||
|
|
||||||
|
foreach($hierarchy as $class_name){
|
||||||
|
if($class_name === AbstractSerializer::class) continue;
|
||||||
|
$class = new $class_name($this->object, $this->resource_server_context);
|
||||||
|
$mappings = array_merge($mappings, $class->getSelfMappings());
|
||||||
|
}
|
||||||
|
$mappings = array_merge($mappings, $this->getSelfMappings());
|
||||||
|
return $mappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getSelfMappings(){
|
||||||
|
return static::$array_mappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getClassHierarchy(){
|
||||||
|
return array_reverse($this->get_class_lineage($this));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_class_lineage($object)
|
||||||
|
{
|
||||||
|
$class_name = get_class($object);
|
||||||
|
$parents = array_values(class_parents($class_name));
|
||||||
|
return array_merge(array($class_name), $parents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null $expand
|
||||||
|
* @param array $fields
|
||||||
|
* @param array $relations
|
||||||
|
* @param array $params
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
|
||||||
|
{
|
||||||
|
$values = [];
|
||||||
|
$method_prefix = ['get', 'is'];
|
||||||
|
if(!count($fields)) $fields = $this->getAllowedFields();
|
||||||
|
$mappings = $this->getAttributeMappings();
|
||||||
|
if (count($mappings)) {
|
||||||
|
$new_values = [];
|
||||||
|
foreach ($mappings as $attribute => $mapping) {
|
||||||
|
$mapping = preg_split('/:/', $mapping);
|
||||||
|
if(count($fields) > 0 && !in_array($mapping[0], $fields)) continue;
|
||||||
|
$value = null;
|
||||||
|
foreach($method_prefix as $prefix){
|
||||||
|
if(method_exists($this->object, $prefix.$attribute)){
|
||||||
|
$value = call_user_func([$this->object, $prefix.$attribute ]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($mapping) > 1)
|
||||||
|
{
|
||||||
|
//we have a formatter ...
|
||||||
|
switch(strtolower($mapping[1]))
|
||||||
|
{
|
||||||
|
case 'datetime_epoch':
|
||||||
|
{
|
||||||
|
if(!is_null($value)) {
|
||||||
|
$value = $value->getTimestamp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'json_string':
|
||||||
|
{
|
||||||
|
$value = JsonUtils::toJsonString($value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'json_boolean':
|
||||||
|
{
|
||||||
|
$value = JsonUtils::toJsonBoolean($value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'json_int':
|
||||||
|
{
|
||||||
|
$value = JsonUtils::toJsonInt($value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'json_float':
|
||||||
|
{
|
||||||
|
$value = JsonUtils::toJsonFloat($value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$new_values[$mapping[0]] = $value;
|
||||||
|
}
|
||||||
|
$values = $new_values;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $expand_str
|
||||||
|
* @param string $prefix
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function filterExpandByPrefix($expand_str, $prefix ){
|
||||||
|
|
||||||
|
$expand_to = explode(',', $expand_str);
|
||||||
|
$filtered_expand = array_filter($expand_to, function($element) use($prefix){
|
||||||
|
return preg_match('/^' . preg_quote($prefix, '/') . '/', strtolower(trim($element))) > 0;
|
||||||
|
});
|
||||||
|
$res = '';
|
||||||
|
foreach($filtered_expand as $filtered_expand_elem){
|
||||||
|
if(strlen($res) > 0) $res .= ',';
|
||||||
|
$res .= explode('.', strtolower(trim($filtered_expand_elem)))[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
}
|
27
app/ModelSerializers/Auth/GroupSerializer.php
Normal file
27
app/ModelSerializers/Auth/GroupSerializer.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php namespace App\ModelSerializers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\ModelSerializers\BaseSerializer;
|
||||||
|
/**
|
||||||
|
* Class GroupSerializer
|
||||||
|
* @package App\ModelSerializers\Auth
|
||||||
|
*/
|
||||||
|
class PublicGroupSerializer extends BaseSerializer
|
||||||
|
{
|
||||||
|
protected static $array_mappings = [
|
||||||
|
'Name' => 'name:json_string',
|
||||||
|
'Slug' => 'slug:json_string',
|
||||||
|
'Active' => 'active:json_boolean',
|
||||||
|
'Default' => 'default:json_boolean',
|
||||||
|
];
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
<?php namespace App\ModelSerializers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\libs\Auth\Models\UserRegistrationRequest;
|
||||||
|
use App\ModelSerializers\BaseSerializer;
|
||||||
|
use Illuminate\Support\Facades\URL;
|
||||||
|
/**
|
||||||
|
* Class UserRegistrationRequestSerializer
|
||||||
|
* @package App\ModelSerializers\Auth
|
||||||
|
*/
|
||||||
|
final class UserRegistrationRequestSerializer extends BaseSerializer
|
||||||
|
{
|
||||||
|
protected static $array_mappings = [
|
||||||
|
'Email' => 'email:json_string',
|
||||||
|
'FirstName' => 'first_name:json_string',
|
||||||
|
'LastName' => 'last_name:json_string',
|
||||||
|
'Country' => 'country:json_string',
|
||||||
|
'Hash' => 'hash:json_string',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null $expand
|
||||||
|
* @param array $fields
|
||||||
|
* @param array $relations
|
||||||
|
* @param array $params
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
|
||||||
|
{
|
||||||
|
$request = $this->object;
|
||||||
|
if(!$request instanceof UserRegistrationRequest) return [];
|
||||||
|
if(!count($relations)) $relations = $this->getAllowedRelations();
|
||||||
|
$values = parent::serialize($expand, $fields, $relations, $params);
|
||||||
|
$values['set_password_link'] = URL::route("password.set", ["token" => $request->getHash()]);
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
}
|
38
app/ModelSerializers/Auth/UserSerializer.php
Normal file
38
app/ModelSerializers/Auth/UserSerializer.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php namespace App\ModelSerializers\Auth;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
use App\ModelSerializers\BaseSerializer;
|
||||||
|
/**
|
||||||
|
* Class BaseUserSerializer
|
||||||
|
* @package App\ModelSerializers\Auth
|
||||||
|
*/
|
||||||
|
class BaseUserSerializer extends BaseSerializer
|
||||||
|
{
|
||||||
|
protected static $array_mappings = [
|
||||||
|
'FirstName' => 'first_name:json_string',
|
||||||
|
'LastName' => 'last_name:json_string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
final class PublicUserSerializer extends BaseUserSerializer {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
final class PrivateUserSerializer extends BaseUserSerializer {
|
||||||
|
protected static $array_mappings = [
|
||||||
|
'Email' => 'email:json_string',
|
||||||
|
'Identifier' => 'identifier:json_string',
|
||||||
|
'LastLoginDate' => 'last_login_date:datetime_epoch',
|
||||||
|
'Active' => 'active:json_boolean',
|
||||||
|
];
|
||||||
|
}
|
26
app/ModelSerializers/BaseSerializer.php
Normal file
26
app/ModelSerializers/BaseSerializer.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php namespace App\ModelSerializers;
|
||||||
|
/**
|
||||||
|
* Copyright 2019 OpenStack Foundation
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class BaseSerializer
|
||||||
|
* @package App\ModelSerializers
|
||||||
|
*/
|
||||||
|
class BaseSerializer extends AbstractSerializer
|
||||||
|
{
|
||||||
|
protected static $array_mappings = [
|
||||||
|
'Id' => 'id:json_int',
|
||||||
|
'CreatedAt' => 'created_at:datetime_epoch',
|
||||||
|
'UpdatedAt' => 'updated_at:datetime_epoch',
|
||||||
|
];
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user