cub / cub-laravel
cub/cub的Laravel包装器
Requires
- php: >=7.0.0
- cub/cub: ~0.1
- firebase/php-jwt: ^5.0
- laravel/framework: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.1
- dev-master
- 5.0.0
- 4.0.0
- 3.0.0
- 2.3.0
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.20.8
- 1.20.7
- 1.20.6
- 1.20.5
- 1.20.4
- 1.20.3
- 1.20.2
- 1.20.1
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.8
- 1.9.7
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-1.20/master
This package is auto-updated.
Last update: 2024-09-26 08:57:32 UTC
README
Laravel的cub/cub包装器。
用于使用Cub进行用户身份验证。保持用户数据与Cub中的情况同步。
兼容性
目前兼容Laravel 5.*
安装
通过Composer
$ composer require "cub/cub-laravel:~2.0"
更新composer后,将CubLaravelServiceProvider添加到config/app.php
中的providers
数组中
Cub\CubLaravel\ServiceProvider::class,
接下来,确保将Cub别名添加到同一config/app.php
中的aliases
数组中
'Cub' => Cub\CubLaravel\Facades\Cub::class, 'CubWidget' => Cub\CubLaravel\Facades\CubWidget::class,
接下来,使用以下命令将cub_id
字段添加到您的相关模型中
php artisan migrate --package="cub/cub-laravel"
将cub_id
添加到相关模型的$fillable
数组中
public $fillable = [ ... 'cub_id', ... ];
重要:您需要确保为相关模型触发Cub webhook。
用法
登录
只需将用户的用户名和密码传递给Cub外观。您将获得一个用于访问用户和Cub令牌的登录对象。
$login = Cub::login($username, $password); // assuming the login was successful, this will be // an instance of your application's User model $user = $login->getUser(); // JWT token returned by Cub during login $token = $login->getToken();
通过Cub user
id获取用户
方便起见,如果您拥有Cub user
id,您将能够获取应用程序的用户模型的一个实例。
// an instance of your application's User model $user = Cub::getUserById($cubId);
通过JWT获取用户
方便起见,如果您拥有Cub JWT,您将能够获取应用程序的用户模型的一个实例。
// an instance of your application's User model $user = Cub::getUserByJWT($jwt);
或者,如果Cub JWT已通过请求的Authorization请求头传递,或者请求中存在cubUserToken
cookie,或者请求通过具有键cub_token
的查询参数进行。 (检查顺序如上所述)。
// an instance of your application's User model $user = Cub::getUserByJWT();
JWT的路由过滤
根据Cub JWT过滤路由。过滤器将检查请求的Authorization头或检查请求查询字符串中cub_token
键的内容。
Route::get('restricted', ['before' => 'cub-auth', function() { // after cub-auth the application user will be accessible as currentUser // an instance of your application's User model $user = Cub::currentUser(); return json_encode(['message' => "You're in, {$user->first_name}!"]); }]);
便利助手
有时您可能只是想知道是否有有效的Cub JWT在传入的请求中。使用这个
$exists = Cub::validJWTExists();
获取当前用户和令牌
// an instance of your application's User model $user = Cub::currentUser(); // the current Cub JWT $token = Cub::currentToken();
CubWidget
使用CubWidget
外观获取您需要的所有小部件元素。有关小部件的更多信息,请参阅Cub Widget 文档。
CubWidget::headerScript(); CubWidget::menu(); CubWidget::app(); // Footer script will be configured with your application's public key CubWidget::footerScript();
配置
为了使此包正确地为您工作,您需要提供一些东西。
如果您不需要更改默认用户模型,您可以通过设置适当的env变量来避免更改。必要的env变量如下。
CUB_PUBLIC CUB_SECRET CUB_API_URL CUB_WEBHOOK_URL
但如果您需要更改用户模型,或者您更喜欢在配置文件中设置很多内容,您可以这样做。
php artisan config:publish cub/cub-laravel
然后您可以根据以下内容更新配置文件。
(请勿将您的密钥放入配置文件中)。
<?php return array( /* |-------------------------------------------------------------------------- | Cub Application Public Key |-------------------------------------------------------------------------- | | This is the Cub application public key. | */ 'public_key' => getEnv('CUB_PUBLIC'), // set to your application's public key /* |-------------------------------------------------------------------------- | Cub Application Secret Key |-------------------------------------------------------------------------- | | This is the Cub application secret key. | */ 'secret_key' => getEnv('CUB_SECRET'), // you should keep this as an environment variable /* |-------------------------------------------------------------------------- | Cub Site Uid |-------------------------------------------------------------------------- | | This is the Cub site uid. | */ 'site' => getEnv('CUB_SITE'), // this is needed for creation of cub objects /* |-------------------------------------------------------------------------- | Cub Application API Url |-------------------------------------------------------------------------- | | This is the Cub application api url. | */ 'api_url' => getEnv('CUB_API_URL'), // set to your application's api url /* |-------------------------------------------------------------------------- | Cub Application Webhook Domain |-------------------------------------------------------------------------- | | This is the Cub application webhook url domain. | */ 'webhook_domain' => getEnv('CUB_WEBHOOK_DOMAIN'), // this give you more control over the webhook /* |-------------------------------------------------------------------------- | Cub Application Webhook Url |-------------------------------------------------------------------------- | | This is the Cub application webhook url. | */ 'webhook_url' => 'webhooks/cub', // set this to your application's webhook url /* |-------------------------------------------------------------------------- | Cub Object Mapping |-------------------------------------------------------------------------- | | This is where you will configure how Cub objects map | to your application Models | */ 'maps' => [ /* |-------------------------------------------------------------------------- | User Mapping Information |-------------------------------------------------------------------------- */ 'user' => [ /* |-------------------------------------------------------------------------- | Application User Model |-------------------------------------------------------------------------- | | This is the user model which will be returned. | */ 'model' => 'App\User', /* |-------------------------------------------------------------------------- | Application User Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your User Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application User Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub User keys can | be mapped to the fields on your User model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'birth_date' => 'birth_date', 'date_joined' => 'date_joined', 'email' => 'email', 'email_confirmed' => 'email_confirmed', 'first_name' => 'first_name', 'gender' => 'gender', 'id' => 'cub_id', 'invalid_email' => 'invalid_email', 'invitation_last_sent_on' => 'invitation_last_sent_on', 'invitation_sent_count' => 'invitation_sent_count', 'last_login' => 'last_login', 'last_name' => 'last_name', 'middle_name' => 'middle_name', 'original_username' => 'original_username', 'password_change_required' => 'password_change_required', 'photo_large' => 'photo_large', 'photo_small' => 'photo_small', 'purchasing_role_buy_for_organization' => 'purchasing_role_buy_for_organization', 'purchasing_role_buy_for_self_only' => 'purchasing_role_buy_for_self_only', 'purchasing_role_recommend' => 'purchasing_role_recommend', 'purchasing_role_specify_for_organization' => 'purchasing_role_specify_for_organization', 'registration_site' => 'registration_site', 'retired' => 'retired', 'token' => 'token', 'username' => 'username', ], ], 'organization' => [ /* |-------------------------------------------------------------------------- | Application Organization Model |-------------------------------------------------------------------------- | | This is the organization model which will be returned. | */ 'model' => 'App\Organization', /* |-------------------------------------------------------------------------- | Application Organization Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your Organization Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application Organization Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub Organization keys can | be mapped to the fields on your Organization model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'name' => 'name', 'employees' => 'employees', 'tags' => 'tags', 'country' => 'country', 'state' => 'state', 'city' => 'city', 'county' => 'county', 'postal_code' => 'postal_code', 'address' => 'address', 'phone' => 'phone', 'hr_phone' => 'hr_phone', 'fax' => 'fax', 'website' => 'website', 'created' => 'created', 'logo' => 'logo', ], /* |-------------------------------------------------------------------------- | Application Organization Model Related Models |-------------------------------------------------------------------------- | | This is where to set the Cub Organization related models. | Related models will be updated whenever the Organization | is updated. | */ 'relations' => [ 'state' => 'state_id', 'country' => 'country_id', ], ], 'member' => [ /* |-------------------------------------------------------------------------- | Application Member Model |-------------------------------------------------------------------------- | | This is the member model which will be returned. | */ 'model' => 'App\Member', /* |-------------------------------------------------------------------------- | Application Member Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your Member Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application Member Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub Member keys can | be mapped to the fields on your Member model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'organization' => 'organization', 'user' => 'user', 'invitation' => 'invitation', 'personal_id' => 'personal_id', 'post_id' => 'post_id', 'notes' => 'notes', 'is_active' => 'is_active', 'is_admin' => 'is_admin', 'positions' => 'positions', 'group_membership' => 'group_membership', 'created' => 'created', ], /* |-------------------------------------------------------------------------- | Application Member Model Related Models |-------------------------------------------------------------------------- | | This is where to set the Cub Member related models. | Related models will be updated whenever the Member | is updated. | */ 'relations' => [ 'organization' => 'organization_id', 'user' => 'user_id', ], ], 'group' => [ /* |-------------------------------------------------------------------------- | Application Group Model |-------------------------------------------------------------------------- | | This is the group model which will be returned. | */ 'model' => 'App\Group', /* |-------------------------------------------------------------------------- | Application Group Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your Group Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application Group Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub Group keys can | be mapped to the fields on your Group model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'organization' => 'organization', 'name' => 'name', 'type' => 'type', 'description' => 'description', 'created' => 'created', ], /* |-------------------------------------------------------------------------- | Application Group Model Related Models |-------------------------------------------------------------------------- | | This is where to set the Cub Group related models. | Related models will be updated whenever the Group | is updated. | */ 'relations' => [ 'organization' => 'organization_id', ], ], 'groupmember' => [ /* |-------------------------------------------------------------------------- | Application GroupMember Model |-------------------------------------------------------------------------- | | This is the GroupMember model which will be returned. | */ 'model' => 'App\GroupMember', /* |-------------------------------------------------------------------------- | Application GroupMember Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your GroupMember Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application GroupMember Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub GroupMember keys can | be mapped to the fields on your GroupMember model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'group' => 'group', 'member' => 'member', 'is_admin' => 'is_admin', 'created' => 'created', ], /* |-------------------------------------------------------------------------- | Application GroupMember Model Related Models |-------------------------------------------------------------------------- | | This is where to set the Cub GroupMember related models. | Related models will be updated whenever the GroupMember | is updated. | */ 'relations' => [ 'group' => 'group_id', 'member' => 'member_id', ], ], 'country' => [ /* |-------------------------------------------------------------------------- | Application Country Model |-------------------------------------------------------------------------- | | This is the Country model which will be returned. | */ 'model' => 'App\Country', /* |-------------------------------------------------------------------------- | Application Country Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your Country Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application Country Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub Country keys can | be mapped to the fields on your Country model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'name' => 'name', 'code' => 'code', 'code2' => 'code2', 'code3' => 'code3', ], ], 'state' => [ /* |-------------------------------------------------------------------------- | Application State Model |-------------------------------------------------------------------------- | | This is the State model which will be returned. | */ 'model' => 'App\State', /* |-------------------------------------------------------------------------- | Application State Transformer |-------------------------------------------------------------------------- | | This is the class that will handle the creating, updating, | and deleting of your State Models. | */ // if you need to customize the processing of the // webhook data, create the transformer and update here 'transformer' => 'Cub\CubLaravel\Transformers\CubObjectTransformer', /* |-------------------------------------------------------------------------- | Application State Model Fields Map |-------------------------------------------------------------------------- | | This is where the mapping of the Cub State keys can | be mapped to the fields on your State model. | i.e. 'cub_field' => 'application_field', | */ 'fields' => [ // update the values of this array with the corresponding // fields on your model or comment them out // to ignore them 'id' => 'cub_id', 'name' => 'name', 'code' => 'code', 'country' => 'country', ], /* |-------------------------------------------------------------------------- | Application State Model Related Models |-------------------------------------------------------------------------- | | This is where to set the Cub State related models. | Related models will be updated whenever the State | is updated. | */ 'relations' => [ 'country' => 'country_id', ], ], ], );
开发
提供了一个docker-compose.yml
文件,用于在已定义的Docker容器中本地运行测试。
运行以下命令安装所有依赖项
docker compose run php74 composer update
运行以下命令运行测试
docker compose run php74 vendor/bin/phpunit