php框架授权,Laravel Authorization:世界上最好的语言PHP+最牛B的访问控制Casbin+最优雅的框架Laravel打造的授权库...

Laravel-authz是一个基于Casbin的Laravel框架授权库,支持ACL、RBAC和ABAC等访问控制模型。安装该库后,你可以通过添加权限、角色和规则来管理用户权限。例如,可以为用户分配角色、添加权限,以及检查用户是否具有特定权限。此外,库还提供了丰富的API、中间件和artisan命令用于操作政策。同时,可以通过缓存策略提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Laravel Authorization

Laravel-authz is an authorization library for the laravel framework.

1460000018819033

1460000018819034

1460000018819035

1460000018819036

1460000018819037

It's based on Casbin, an authorization library that supports access control models like ACL, RBAC, ABAC.

All you need to learn to use Casbin first.

Installation

Require this package in the composer.json of your Laravel project. This will download the package.

composer require casbin/laravel-authz

The Lauthz\LauthzServiceProvider is auto-discovered and registered by default, but if you want to register it yourself:

Add the ServiceProvider in config/app.php

'providers' => [

/*

* Package Service Providers...

*/

Lauthz\LauthzServiceProvider::class,

]

The Enforcer facade is also auto-discovered, but if you want to add it manually:

Add the Facade in config/app.php

'aliases' => [

// ...

'Enforcer' => Lauthz\Facades\Enforcer::class,

]

To publish the config, run the vendor publish command:

php artisan vendor:publish

This will create a new model config file named config/lauthz-rbac-model.conf and a new lauthz config file named config/lauthz.php.

To migrate the migrations, run the migrate command:

php artisan migrate

This will create a new table named rules

Usage

Quick start

Once installed you can do stuff like this:

use Enforcer;

// adds permissions to a user

Enforcer::addPermissionForUser('eve', 'articles', 'read');

// adds a role for a user.

Enforcer::addRoleForUser('eve', 'writer');

// adds permissions to a rule

Enforcer::addPolicy('writer', 'articles','edit');

You can check if a user has a permission like this:

// to check if a user has permission

if (Enforcer::enforce("eve", "articles", "edit")) {

// permit eve to edit articles

} else {

// deny the request, show an error

}

Using Enforcer Api

It provides a very rich api to facilitate various operations on the Policy:

Gets all roles:

Enforcer::getAllRoles(); // ['writer', 'reader']

Gets all the authorization rules in the policy.:

Enforcer::getPolicy();

Gets the roles that a user has.

Enforcer::getRolesForUser('eve'); // ['writer']

Gets the users that has a role.

Enforcer::getUsersForRole('writer'); // ['eve']

Determines whether a user has a role.

Enforcer::hasRoleForUser('eve', 'writer'); // true or false

Adds a role for a user.

Enforcer::addRoleForUser('eve', 'writer');

Adds a permission for a user or role.

// to user

Enforcer::addPermissionForUser('eve', 'articles', 'read');

// to role

Enforcer::addPermissionForUser('writer', 'articles','edit');

Deletes a role for a user.

Enforcer::deleteRoleForUser('eve', 'writer');

Deletes all roles for a user.

Enforcer::deleteRolesForUser('eve');

Deletes a role.

Enforcer::deleteRole('writer');

Deletes a permission.

Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).

Deletes a permission for a user or role.

Enforcer::deletePermissionForUser('eve', 'articles', 'read');

Deletes permissions for a user or role.

// to user

Enforcer::deletePermissionsForUser('eve');

// to role

Enforcer::deletePermissionsForUser('writer');

Gets permissions for a user or role.

Enforcer::getPermissionsForUser('eve'); // return array

Determines whether a user has a permission.

Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false

Using a middleware

This package comes with EnforcerMiddleware, RequestMiddleware middlewares. You can add them inside your app/Http/Kernel.php file.

protected $routeMiddleware = [

// ...

// a basic Enforcer Middleware

'enforcer' => \Lauthz\Middlewares\EnforcerMiddleware::class,

// an HTTP Request Middleware

'http_request' => \Lauthz\Middlewares\RequestMiddleware::class,

];

basic Enforcer Middleware

Then you can protect your routes using middleware rules:

Route::group(['middleware' => ['enforcer:articles,read']], function () {

// pass

});

HTTP Request Middleware ( RESTful is also supported )

If you need to authorize a Request,you need to define the model configuration first in config/lauthz-rbac-model.conf:

[request_definition]

r = sub, obj, act

[policy_definition]

p = sub, obj, act

[role_definition]

g = _, _

[policy_effect]

e = some(where (p.eft == allow))

[matchers]

m = g(r.sub, p.sub) && r.sub == p.sub && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act)

Then, using middleware rules:

Route::group(['middleware' => ['http_request']], function () {

Route::resource('photo', 'PhotoController');

});

Using artisan commands

You can create a policy from a console with artisan commands.

To user:

php artisan policy:add eve,articles,read

To Role:

php artisan policy:add writer,articles,edit

Adds a role for a user:

php artisan role:assign eve writer

Using cache

Authorization rules are cached to speed up performance. The default is off.

Sets your own cache configs in Laravel's config/lauthz.php.

'cache' => [

// changes whether Lauthz will cache the rules.

'enabled' => false,

// cache store

'store' => 'default',

// cache Key

'key' => 'rules',

// ttl \DateTimeInterface|\DateInterval|int|null

'ttl' => 24 * 60,

],

Thinks

Casbin in Laravel. You can find the full documentation of Casbin on the website.

License

This project is licensed under the Apache 2.0 license.

通达信行情API是金融数据提供商通达信(TongDaXin)为开发者和金融机构提供的接口服务,用于获取实时及历史的股票、期货、期权等金融市场数据。这个API允许用户在自己的应用程序中集成通达信的数据服务,实现个性化数据分析、交易策略开发等功能。 1. **API基本概念** - **API**:Application Programming Interface,应用程序编程接口,是软件之间交互的一种方式,提供预定义的函数和方法,使得其他软件能够调用特定功能。 - **通达信**:国内知名的金融终端软件提供商,提供股票、期货、基金等市场数据,以及交易服务。 2. **通达信API的功能** - **实时行情**:获取股票、期货、期权等市场的实时报价信息,包括最新价、涨跌额、涨跌幅、成交量等。 - **历史数据**:获取历史交易日的K线数据、分时数据、交易量等信息,支持自定义时间段查询。 - **深度数据**:获取买卖盘口的五档报价和成交量,有助于分析市场买卖意愿。 - **资讯信息**:获取公告、研报、新闻等市场资讯。 - **交易委托**:通过API进行交易下单、撤单等操作,实现自动化交易。 3. **TdxHqApi** - **TdxHqApi** 是通达信行情API的具体实现,它包含了调用通达信数据服务的各种函数和类,如获取股票列表、获取实时行情、获取历史数据等。 - 开发者需要按照API文档的指示,导入TdxHqApi库,然后通过调用相应的函数来获取所需数据。 4. **使用步骤** - **安装**:下载并安装通达信API的SDK,通常包括头文件和动态链接库。 - **初始化**:在代码中实例化API对象,进行连接设置,如服务器地址、端口号等。 - **连接**:连接到通达信服务器,进行身份验证。 - **数据请求**:调用对应的API函数,例如`GetS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值