├── .gitignore ├── basics ├── basics.md ├── setlogin.md ├── login.md └── priLogin.md ├── users ├── userList.md ├── deleteUser.md ├── actUser.md ├── restUser.md ├── updateUser.md ├── createUser.md └── selectUser.md ├── group ├── groupList.md ├── deleteGroup.md ├── createGroup.md ├── updateGroup.md └── selectGroup.md ├── start ├── 1.md └── install.md ├── README.md ├── SUMMARY.md ├── say.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /bootstrap/compiled.php 2 | .env.*.php 3 | .env.php 4 | -------------------------------------------------------------------------------- /basics/basics.md: -------------------------------------------------------------------------------- 1 | #基础使用 2 | 3 | 本章主要讲解,如何使用 sentry 实现登陆,登出,判断是否登陆 等基本的功能,非常简单,几行代码即可实现! -------------------------------------------------------------------------------- /users/userList.md: -------------------------------------------------------------------------------- 1 | # 用户管理 2 | 3 | 本节主要讲解一下内容 4 | 5 | - 创建用户 6 | - 修改用户 7 | - 删除用户 8 | - 激活用户 9 | - 重置密码 10 | - 各种查询用户方法集合 11 | 12 | 13 | 这些在我们的 sentry 中,必定是非常的简单的,Let's go! 14 | -------------------------------------------------------------------------------- /group/groupList.md: -------------------------------------------------------------------------------- 1 | # 分组 2 | 3 | 4 | 给用户分组,更方便管理权限等等。。。。分组其实比用户还简单,一起来学习吧 5 | 6 | 有问题? 7 | 8 | 欢迎加入 QQ 交流群:365969825 9 | 10 | 或者 在新浪微博 11 | 12 | [@袁超](http://www.weibo.com/28ex) 13 | 14 | -------------------------------------------------------------------------------- /start/1.md: -------------------------------------------------------------------------------- 1 | # 开始使用 Sentry 2 | 3 | 4 | #### Sentry 是什么? 5 | 6 | sentry 是 laravel 的一个用户管理扩展,他可以帮我们更好的管理用户&用户组&权限等等信息,其中一些内置方法,更实用,使开发更加迅速 7 | 8 | 9 | #### Sentry 不是什么? 10 | 11 | sentry 不是神,不能给你什么都实现了,所以我们的业务逻辑还是得自己写 12 | 13 | 14 | [laravel 官网入口](https://cartalyst.com/) 15 | -------------------------------------------------------------------------------- /group/deleteGroup.md: -------------------------------------------------------------------------------- 1 | #删除分组 2 | 3 | 4 | 我们依旧简单的删除分组 5 | 6 | ``` 7 | 8 | try 9 | { 10 | // 通过 分组ID 查找分组 11 | $group = Sentry::findGroupById(1); 12 | 13 | // 删除分组 14 | $group->delete(); 15 | } 16 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 17 | { 18 | echo '分组不存在'; 19 | } 20 | 21 | 22 | ``` 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | > laravel 交流群 365969825 3 | 4 | > 如果文档中有翻译错误 or 技术逻辑错误,请 [@袁超](http://weibo.com/28ex) 新浪微博,我会在第一时间进行修正处理 5 | 6 | 7 | laravel Sentry 中文教程 8 | ============================= 9 | 10 | 11 | #### 为什么要重复造轮子 12 | 13 |   接触laravel 没多久,感觉成系统文档还是比较少的,而且不适合新手查阅,laravel Sentry 也有中文文档,但是他是完全对着 官方文档进行的翻译,对新手帮助甚少,所以此篇教程,针对于新手,每个知识点都配对着详细的列子,欢迎加群,一起学习交流 14 | -------------------------------------------------------------------------------- /users/deleteUser.md: -------------------------------------------------------------------------------- 1 | # 删除用户 2 | 3 | 前面我们说了创建用户,修改用户,本节我们说下删除用户! 4 | 5 | 看你不爽,delete 掉你!就是这么任性! 6 | 7 | ** delete 删除用户** 8 | 9 | ``` 10 | try 11 | { 12 | // 根据 user id 查询用户信息 13 | $user = Sentry::findUserById(1); 14 | 15 | // 删除用户 16 | $user->delete(); 17 | } 18 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 19 | { 20 | echo '用户不存在!'; 21 | } 22 | 23 | ``` 24 | 25 | sentry 暂时就提供 delete 方法来删除用户,所以那句 26 | 27 | >看你不爽,delete 掉你 28 | 29 | 可不是说白话哦 -------------------------------------------------------------------------------- /basics/setlogin.md: -------------------------------------------------------------------------------- 1 | #配置登陆字段 2 | 3 | sentry 中,默认的登陆凭证是 email ,但是老板是要求,用户要通过手机号登录,这个时候,怎么办呢? 4 | 5 | 其实很好解决,sentry 灵活的配置,帮你排忧解难 6 | 7 | 我们打开 sentry 的配置文件, 8 | 9 | 打开 **verdor/cartalyst/sentry/src/config/config.php** 10 | 11 | 找到 12 | 13 | >login_attribute 14 | 15 | 我们把他修改为 16 | 17 | ``` 18 | 19 | 'login_attribute' => 'phone', 20 | 21 | ``` 22 | 23 | 这个时候,去你的数据库 user 表里面,新加一个字段,取名为 phone ,就可以啦 24 | 25 | 如果有 问题 ,或者不理解的地方,欢迎加入 QQ群:365969825 26 | 27 | 或者 新浪微博 [@袁超](http://www.weibo.com/28ex) -------------------------------------------------------------------------------- /group/createGroup.md: -------------------------------------------------------------------------------- 1 | #创建分组 2 | 3 | 4 | 创建一个分组,是非常简单的,我们来学习下吧 5 | 6 | ``` 7 | try 8 | { 9 | // 创建分组 10 | $group = Sentry::createGroup(array( 11 | 'name' => 'admin', 12 | 'permissions' => array( 13 | 'admin.index' => 1, 14 | 'users.index' => 1, 15 | ), 16 | )); 17 | } 18 | catch (Cartalyst\Sentry\Groups\NameRequiredException $e) 19 | { 20 | echo '分组名称必须存在'; 21 | } 22 | catch (Cartalyst\Sentry\Groups\GroupExistsException $e) 23 | { 24 | echo '分组已经存在'; 25 | } 26 | 27 | ``` 28 | 29 | 值得注意的是,permissions 的值是一个数组哦~ 30 | 31 | 创建分组篇完毕,简单吧? -------------------------------------------------------------------------------- /users/actUser.md: -------------------------------------------------------------------------------- 1 | #激活用户 2 | 3 | 4 | 在 [3.1创建用户](createUser.md) 中,我们注册了一个用户,并且产生了激活码,这个时候,我们得到激活码,怎么激活用户呢? 5 | 6 | 其实很简单,看代码 7 | 8 | ``` 9 | try 10 | { 11 | // 根据 userid 查询用户 12 | $user = Sentry::findUserById(1); 13 | 14 | // 使用激活码激活用户 15 | if ($user->attemptActivation('产生的激活码')) 16 | { 17 | // 用户激活成功 18 | } 19 | else 20 | { 21 | // 用户激活失败 22 | } 23 | } 24 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 25 | { 26 | echo '用户不存在'; 27 | } 28 | catch (Cartalyst\Sentry\Users\UserAlreadyActivatedException $e) 29 | { 30 | echo '用户已经被激活,不能重复激活'; 31 | } 32 | 33 | ``` 34 | 35 | 很简单的,我们就可以实现激活用户的功能啦! -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | * [Introduction](README.md) 4 | * [开始使用](start/1.md) 5 | * [安装](start/install.md) 6 | * [基础使用](basics/basics.md) 7 | * [登陆&登出&检测是否登陆](basics/login.md) 8 | * [配置登陆字段](basics/setlogin.md) 9 | * [权限篇](basics/priLogin.md) 10 | * [用户](users/userList.md) 11 | * [创建用户](users/createUser.md) 12 | * [修改用户](users/updateUser.md) 13 | * [删除用户](users/deleteUser.md) 14 | * [激活用户](users/actUser.md) 15 | * [重置密码](users/restUser.md) 16 | * [查询用户方法集合](users/selectUser.md) 17 | * [分组](group/groupList.md) 18 | * [创建分组](group/createGroup.md) 19 | * [修改用户](group/updateGroup.md) 20 | * [删除用户](group/deleteGroup.md) 21 | * [查询分组方法集合](group/selectGroup.md) 22 | * [完结感言](say.md) 23 | 24 | -------------------------------------------------------------------------------- /say.md: -------------------------------------------------------------------------------- 1 | #完结感言 2 | 3 | 4 | 其实,我也是刚接触 laravel 的小菜,从刚接触 laravel 就接触到了 sentry 5 | 6 | 然后开始 意识到,这个东西,能让用户管理变的更加轻松,权限控制更加可视化,然后 7 | 8 | 我踏上了一条不归路 9 | 10 | 一开始,我看的是因为文档,我英语很差,看起来很吃力,于是,我想翻译这个英文文档,一方面自己可以学习一下英语,不至于连自己本身专业的英语问答都看不懂 11 | 12 | 结果,第二天,一个群友告诉我说,sentry 已经有中文文档了,在 github 上 13 | 14 | 好吧,既然有了,我就不去瞎折腾了, 15 | 16 | 结果,我看那个中文文档,完全是照着官方的英文文档进行翻译,对于我这种新手来说,完全理解不了呀 17 | 18 | 于是,自己又起了翻译的念头 19 | 20 | 好吧,说干就干,年轻不怕失败,最多就是浪费点时间而已嘛 21 | 22 | 但是,翻译的东西发布到哪呢? 23 | 24 | gitbook 是个不错的选择 25 | 26 | 但是,我git 很差, 27 | 28 | 我复习了一遍 git 操作,摸透了 gitbook 的操作流程 29 | 30 | 找了个 makedown 编辑器,学习他的语法 31 | 32 | 终于,准备工作做完了 33 | 34 | 我对照着 中文文档 + 英文文档进行翻译,重新文档目录,配上 代码 35 | 36 | 嗯嗯,终于可以看了。 37 | 38 | 12 月 12 号,在 gitbook 上首次提交 39 | 40 | 12 月 17 号,翻译完成 41 | 42 | 几十次的 commits 43 | 44 | 自己得到了成长,我觉得很好! -------------------------------------------------------------------------------- /group/updateGroup.md: -------------------------------------------------------------------------------- 1 | #修改分组 2 | 3 | 4 | 我们上节中,几行代码创建了分组,在这节中,我们依旧几行代码来修改我们的分组 5 | 6 | ``` 7 | 8 | try 9 | { 10 | // 通过 分组ID 查找分组 11 | $group = Sentry::findGroupById(1); 12 | 13 | // 更新分组详情 14 | $group->name = 'Users'; 15 | $group->permissions = array( 16 | 'admin.index' => 0, 17 | 'users.index' => 0, 18 | ); 19 | 20 | // 更新分组 21 | if ($group->save()) 22 | { 23 | // 分组信息已被更新 24 | } 25 | else 26 | { 27 | // 分组信息未被更新 28 | } 29 | } 30 | catch (Cartalyst\Sentry\Groups\NameRequiredException $e) 31 | { 32 | echo '分组名称必须存在'; 33 | } 34 | catch (Cartalyst\Sentry\Groups\GroupExistsException $e) 35 | { 36 | echo '分组已经存在'; 37 | } 38 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 39 | { 40 | echo '分组不存在'; 41 | } 42 | 43 | ``` 44 | 45 | 好了,简简单单的,我们已经修改了一个分组啦 46 | 47 | -------------------------------------------------------------------------------- /group/selectGroup.md: -------------------------------------------------------------------------------- 1 | #查询分组 2 | 3 | 4 | 我们来说说几种查询分组的方法 5 | 6 | 7 | 8 | ### 查找所有分组 9 | 10 | ``` 11 | 12 | $groups = Sentry::findAllGroups(); 13 | 14 | ``` 15 | 16 | ### 通过分组 id 查找一个分组 17 | 18 | ``` 19 | try 20 | { 21 | $group = Sentry::findGroupById(1); 22 | } 23 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 24 | { 25 | echo '分组不存在'; 26 | } 27 | 28 | ``` 29 | 30 | ### 通过分组名称 查找一个分组 31 | ``` 32 | try 33 | { 34 | $group = Sentry::findGroupByName('admin'); 35 | } 36 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 37 | { 38 | echo '分组不存在'; 39 | } 40 | 41 | ``` 42 | 43 | ### 查找分组的权限 44 | 45 | ``` 46 | try 47 | { 48 | $group = Sentry::findGroupById(1); 49 | 50 | // 获取用户的权限 51 | $groupPermissions = $group->getPermissions(); 52 | } 53 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 54 | { 55 | echo '分组不存在'; 56 | } 57 | ``` -------------------------------------------------------------------------------- /users/restUser.md: -------------------------------------------------------------------------------- 1 | #重置密码 2 | 3 | 4 | 我们已经讲解完了,如果创建,注册用户,激活,修改,删除用户了,看似已经全了,但是我们还缺了一点 5 | 6 | 那就是为用户提供重置密码! 这个是一个网站必不可少的功能之一吧 7 | 8 | 我们重置密码,在 sentry 中分成 两步 9 | 10 | 1、发送 重置代码给用户 11 | 2、验证 重置代码,成功则修改密码 12 | 13 | 14 | 首先我们看 第一步 15 | 16 | **获取重置密码的代码** 17 | 18 | 19 | ``` 20 | try 21 | { 22 | // 根据 email 查找用户 23 | $user = Sentry::findUserByLogin('yccphp@163.com'); 24 | 25 | // 获取重置代码 26 | $resetCode = $user->getResetPasswordCode(); 27 | 28 | //你可以把 重置代码,通过 email 或者 短信 发送给用户 29 | } 30 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 31 | { 32 | echo '用户不存在'; 33 | } 34 | 35 | ``` 36 | 37 | 哈哈,是不是和我们激活用户很像呢 ? 38 | 39 | 第二步, 40 | 41 | **验证重置代码** 42 | 43 | ``` 44 | try 45 | { 46 | // 查询用户 47 | $user = Sentry::findUserById(1); 48 | 49 | // 检查重置密码的代码是否有效 50 | if ($user->checkResetPasswordCode(重置代码)) 51 | { 52 | // 重置用户密码 53 | if ($user->attemptResetPassword(重置密码,新密码)) 54 | { 55 | 56 | // 密码重置通过 57 | } 58 | else 59 | { 60 | // 密码重置失败 61 | } 62 | } 63 | else 64 | { 65 | // 所提供的密码重置代码是无效的 66 | } 67 | } 68 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 69 | { 70 | echo '用户不存在.'; 71 | } 72 | 73 | ``` 74 | 75 | 76 | 77 | 简单两步,重置密码,就如同简单两步实现激活用户,sentry 的魅力,就是体现在这里 -------------------------------------------------------------------------------- /users/updateUser.md: -------------------------------------------------------------------------------- 1 | # 修改用户 2 | 3 | 前面我们说过了 创建一个用户,本节我们学习如何修改一个用户 4 | 5 | 首先,我们弄清楚,我们的需求是什么 6 | 7 | 1、更新用户信息 8 | 9 | 2、更新用户信息,并且更新用户组 10 | 11 | 这些在 sentry 中也是非常简单的,我们先说更新用户信息 12 | 13 | **仅更新用户信息** 14 | 15 | ``` 16 | 17 | try 18 | { 19 | // 根据id 查询用户 20 | $user = Sentry::findUserById(1); 21 | 22 | // 更新用户的属性 23 | $user->email = 'yccphp@163.com'; 24 | $user->first_name = 'Yuan'; 25 | 26 | // 执行更新 27 | if ($user->save()) 28 | { 29 | // 更新成功 30 | } 31 | else 32 | { 33 | // 更新失败 34 | } 35 | } 36 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 37 | { 38 | echo 'login 字段是必须的'; 39 | } 40 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 41 | { 42 | echo '用户不存在'; 43 | } 44 | 45 | 46 | ``` 47 | 48 | 先查询用户,查询出来后修改用户的属性,最后调用 save 方法进行保存,是不是很简单呢? 49 | 50 | 51 | 好了,我们接着说第二种需求,更新用户时,把用户组也更新了, 52 | 53 | 也就是,这个用户本来是 超级 vip 会员组,给他更新资料的时候,顺带把他的组更新为 普通会员 用户组 54 | 55 | ** 更新用户信息顺带更新绑定的用户组** 56 | 57 | ``` 58 | try 59 | { 60 | // 查询用户 61 | $user = Sentry::findUserById(1); 62 | 63 | // 查出 新的用户组 64 | $adminGroup = Sentry::findGroupById(1); 65 | 66 | // 把 用户 与 用户组绑定 67 | if ($user->addGroup($adminGroup)) 68 | { 69 | // 绑定成功 70 | } 71 | else 72 | { 73 | // 绑定失败 74 | } 75 | 76 | // 更新用户信息 77 | $user->email = 'john.doe@example.com'; 78 | $user->first_name = 'John'; 79 | 80 | // 执行保存动作 81 | if ($user->save()) 82 | { 83 | // 保存成功 84 | } 85 | else 86 | { 87 | // 保存失败 88 | } 89 | } 90 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 91 | { 92 | echo 'login 字段是必须的'; 93 | } 94 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 95 | { 96 | echo '用户不存在'; 97 | } 98 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 99 | { 100 | echo '用户组不存在'; 101 | } 102 | 103 | ``` 104 | 105 | 是不是很简单,就是根据 用户组 id 查出需要绑定的用户组 , 106 | 107 | 根据 用户id 查出 绑定的用户 108 | 109 | 然后调用 addGroup ,就更新了它绑定的用户组啦 110 | 111 | 更新用户,就这么多,非常简单是吧? -------------------------------------------------------------------------------- /basics/login.md: -------------------------------------------------------------------------------- 1 | # 登陆 & 登出 & 是否登陆 2 | 3 | 4 | #### 使用 sentry 5 | 6 | 在控制器里面,我们使用 sentry,需要在控制器顶部 use 以下 7 | 8 | ``` 9 | use Sentry 10 | 11 | ``` 12 | 13 | 14 | #### 登陆 之 基于凭证登陆用户 15 | 16 | 首先,登陆非常简单,就是收集表单数据,验证表单数据,我们这里主要讲解怎么验证表单数据 17 | 18 | 看下面代码 19 | 20 | ``` 21 | 22 | $cred = [ 23 | 'email'=>Input::get('email'), 24 | 'password'=>Input::get('password'), 25 | ]; 26 | 27 | try{ 28 | $user = Sentry::authenticate($cred,false); 29 | 30 | if($user){ 31 | return Redirect::route('admin.index.index'); 32 | } 33 | }catch (\Exception $e){ 34 | return Redirect::route('admin.login')->withErrors(array('login' => $e->getMessage())); 35 | } 36 | 37 | 38 | ``` 39 | 40 | 我们使用 Input 接受到了表单数据,然后我们调用了 Sentry::authenticate 来验证登陆数据,那么这个 authenticate 的参数是什么意思呢?我们看下面参数表 41 | 42 | Param | Required | Default | Type | Description 43 | ------------ | -------- | ------- | ----- | ----------------------------------- 44 | $credentials | true | null | array | 数组内必须包含用户凭证,例如 `email` 和 `password`。`password`. 45 | $remember | false | false | bool | 它将设置一个 Cookie,用来标记 Sentry 是否应该记住这个用户。 46 | 47 | 48 | 第一个参数就是 我们用户填入的 email 和密码数组, 49 | 第二个参数则为 布尔值,为 true 则表示记住这个用户,你可以拿他实现网站上面的 记住我 的这个功能 50 | 51 | 简单的几行代码,即可实现我们的 验证数据登陆啦,是不是很简单呢? 52 | 53 | 54 | 55 | 56 | #### 登陆 之 根据所提供的 user 模型登陆 57 | 58 | 看文档看到这个方法,不知道有啥用,先根据 用户id 查询出它的信息,然后在登陆,好像我是没咋用,但还是和大家说说吧 59 | 60 | ``` 61 | // 通过 user id 查找用户 62 | $user = Sentry::findUserById(1); 63 | 64 | // 登录用户 65 | Sentry::login($user, false); 66 | 67 | ``` 68 | 69 | 老规矩,我们看 Sentry::login 所需的参数 70 | 71 | Param | Required | Default | Type | Description 72 | ------------ | -------- | ------- | ------ | ----------------------------------- 73 | $user | true | null | object | 一个 user 的查询结果 74 | $remember | false | false | bool | 是否记住用户 75 | 76 | 和我们上面的 基于数据登陆很像对不对,只不过 第一个参数是 user 的查询结果罢了 77 | 78 | 79 | 80 | 81 | 82 | #### 检测用户是否登陆 83 | 84 | 检测用户是否登陆也是非常方便的,我们来看代码 85 | 86 | ``` 87 | if ( ! Sentry::check()) 88 | { 89 | // 用户“未登录”或“未激活” 90 | } 91 | else 92 | { 93 | // 用户已经登录 94 | } 95 | 96 | ``` 97 | Sentry::check() 就是检测用户是否登陆的方法, 98 | 99 | 返回值 100 | 101 | true : 用户已经登陆 102 | false : 用户没有登陆 103 | 104 | 105 | #### 退出登录 106 | 107 | 上面的方法是不是使用起来很简单呢?我们的退出登录也是非常简单的,只需一行代码 108 | 109 | ``` 110 | Sentry::logout(); 111 | 112 | ``` 113 | 114 | 115 | 116 | > 以上就是 登录,检测是否登陆,退出的方法了,是不是很简单呢?哈哈,有问题可以加 我们的交流QQ群:365969825 ,或者新浪微博 [@袁超](http://weibo.com/28ex) -------------------------------------------------------------------------------- /start/install.md: -------------------------------------------------------------------------------- 1 | # 安装 2 | 3 | 安装 sentry 是一件非常简单的事情哦,下面跟着我们一起来安装吧! 4 | 5 | 6 | #### 加入 composer.json 7 | 8 | composer 是一个项目依赖管理工具,我们把我们需要安装的 sentry 版本号加入进入,执行安装命令后,会自动帮我们安装 sentry 并且安装其依赖的软件包 9 | 10 | 往里面写入 11 | 12 | ``` 13 | "require": { 14 | "cartalyst/sentry": "2.1.4" 15 | }, 16 | ``` 17 | 18 | 现在,在 命令行中,切换到你的项目根目录,输入安装命令来安装吧 19 | 20 | ``` 21 | composer update 22 | ``` 23 | 24 | 25 | 26 | ### app 配置 27 | 28 | 29 | 等你的 composer update 执行完毕后,恭喜你已经完成了安装的 50% ,接下来,我们把 sentry 与我们的 laravel 框架关联起来,这个步骤是每个扩展包安装时,都会走的一步,别紧张,慢慢来 30 | 31 | 32 | 33 | 打开 app/config/app.php 文件,往 *providers* 这个数组里面加入一行 34 | 35 | ``` 36 | 'Cartalyst\Sentry\SentryServiceProvider', 37 | 38 | ``` 39 | 40 | 完成后,我们看下面一个数组 *aliases* ,我们往里面加入一行 41 | 42 | ``` 43 | 'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry', 44 | 45 | ``` 46 | 47 | 好了,配置完成后,又要恭喜你了,你已经完成了安装的 80% ,哈哈,是不是很简单呢?继续跟着我们来安装吧 48 | 49 | 50 | ### 数据库配置 51 | 52 | 在进行这一步之前,请先保证你的 app/config/database.php 里面配置了数据库连接信息 53 | 54 | 在命令行中,输入以下代码运行 55 | 56 | ``` 57 | php artisan migrate --package=cartalyst/sentry 58 | 59 | ``` 60 | 61 | 这个时候,看下你的数据库里面,是不是多了 5 张数据表呢?哈哈,就是这么智能,但是这些表暂时是没有数据的,我们要做一些初始化工作 62 | 63 | 64 | ### 初始化数据 65 | 66 | 这个时候,虽然我们数据库里面有了 5 张表,但是我们表里面是没有数据的,我们给他添加一些初始化数据测试一下 67 | 68 | 在 app/database/seeds/ 下新建 SentrySeeder.php 写入以下内容 69 | 70 | ``` 71 | 74 | * Time: 2014.11.19 下午6:12 75 | */ 76 | 77 | class SentrySeeder extends Seeder { 78 | 79 | public function run() 80 | { 81 | // 清空数据 82 | DB::table('users')->delete(); 83 | DB::table('groups')->delete(); 84 | DB::table('users_groups')->delete(); 85 | 86 | // 创建用户 87 | Sentry::getUserProvider()->create(array( 88 | 'email' => '653069653@qq.com', 89 | 'password' => "101058", 90 | 'first_name' => '超', 91 | 'last_name' => '袁', 92 | 'activated' => 1, 93 | )); 94 | 95 | // 创建用户组 96 | Sentry::getGroupProvider()->create(array( 97 | 'name' => 'Admin', 98 | 'permissions' => ['admin' => 1], 99 | )); 100 | 101 | // 将用户加入用户组 102 | $adminUser = Sentry::getUserProvider()->findByLogin('653069653@qq.com'); 103 | $adminGroup = Sentry::getGroupProvider()->findByName('Admin'); 104 | $adminUser->addGroup($adminGroup); 105 | } 106 | } 107 | 108 | ``` 109 | 110 | 111 | 以上代码都是 laravel 基础知识,看不懂就需要去看看官方手册了哦! 112 | 113 | 我们打开 app/database/seeds/ DatabaseSeeder.php 在他的 run 方法中,新增一行 114 | 115 | ``` 116 | $this->call('SentrySeeder'); 117 | 118 | ``` 119 | 120 | 接着,我们在命令行运行 121 | ``` 122 | php artisan db:seed 123 | ``` 124 | 125 | 126 | 打开你的数据库,查看 127 | 128 | users 是否有了 用户数据 ? 129 | groups 是否有了 用户组数据 ? 130 | 131 | users_groups 是否有了用户id 与 用户组id ? 132 | 133 | 如果都有了的话,恭喜你,安装顺利完成! 134 | 135 | 如果在安装中遇到问题,可加入我们的 qq 交流群:365969825 ,或者在新浪微博 [@袁超](http://weibo.com/28ex) 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /users/createUser.md: -------------------------------------------------------------------------------- 1 | #创建用户 2 | 3 | 4 | 5 | 6 | #### 创建用户 之 createUser 方法 7 | 8 | 9 | ** 创建用户并分配用户组 ** 10 | 11 | 我们创建一个用户,并且为它分配用户组,他会继承自用户组的权限 12 | 13 | ``` 14 | try 15 | { 16 | // 创建一个用户 login字段是必须的 默认是 email 可配置,参考 [2.2 配置登陆字段](setlogin.md) 17 | $user = Sentry::createUser(array( 18 | 'email' => 'yccphp@163.com', 19 | 'password' => '123456', 20 | 'activated' => true, 21 | )); 22 | 23 | // 查找用户组 where 为 组id = 1 24 | $adminGroup = Sentry::findGroupById(1); 25 | 26 | // 把 用户加到 用户组 27 | $user->addGroup($adminGroup); 28 | } 29 | catch (Cartalyst\Sentry\Users\LoginRequiredException $e) 30 | { 31 | echo 'login 字段是必须的'; 32 | } 33 | catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) 34 | { 35 | echo '没有提供密码'; 36 | } 37 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 38 | { 39 | echo '用户已经存在'; 40 | } 41 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 42 | { 43 | echo '用户组不存在'; 44 | } 45 | 46 | ``` 47 | 48 | ** 创建一个用户 单独设置权限** 49 | 50 | 上面代码中,我们创建了一个用户,并且为他分配了用户组,继承了用户组的权限 51 | 52 | 但是,我们想一个用户,不加入用户组,但是又有自己的权限,sentry 可以帮我们实现吗? 53 | 54 | 答案是,必须的! 55 | 56 | ``` 57 | try 58 | { 59 | // 创建一个用户 60 | $user = Sentry::createUser(array( 61 | 'email' => 'yccphp@163.com', // 邮箱 62 | 'password' => '123456', // 密码 63 | 'activated' => true, // 是否允许登陆 64 | 'permissions' => array( // 权限组 65 | 'user.create' => -1, // 拒绝访问 user.create 66 | 'user.delete' => -1, // 拒绝访问 user.delete 67 | 'user.view' => 1, // 允许访问 user.view 68 | 'user.update' => 1, // 允许访问 user.update 69 | ), 70 | )); 71 | } 72 | catch (Cartalyst\Sentry\Users\LoginRequiredException $e) 73 | { 74 | echo 'login 字段是必须的'; 75 | } 76 | catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) 77 | { 78 | echo '密码字段不存在' 79 | } 80 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 81 | { 82 | echo '创建的用户已经存在'; 83 | } 84 | 85 | ```` 86 | 87 | 88 | 可以看到我们对 permissions 字段进行了赋值,这个字段正是表明了 这个用户有什么权限,我们看下用户的权限码 89 | 90 | ``` 91 | -1 : 拒绝 92 | 1 : 允许 93 | 0 : 继承 94 | 95 | ``` 96 | 97 | 我们可以限制用户,可以访问什么,不可以访问什么,权限控制的是不是很灵活呢 ? 98 | 99 | -1 ,1 我们可以很轻易的理解,但是这个 0 权限继承是什么意思呢? 100 | 101 | 0 表示继承,继承自 分组!就是说,我们 设置权限 1 或者 -1 的时候,其实是覆盖了分组的权限,但是我们设置为 0 的时候,是继承自分组的权限哦! 102 | 103 | 104 | 105 | #### 创建用户 之 register 注册用户 106 | 107 | 上面我们讲解了 创建用户,本节我们讲解下 注册用户。 108 | 109 | 需求如下: 110 | 用户通过 表单注册用户,初始注册的 用户是不可以登录的,必须通过邮件激活用户,这个需求我们怎么做呢? 111 | 112 | 其实,在 sentry 中,为我们提供了 一条龙的 用户体系服务,这些 在 sentry 中都变的很简单 113 | 114 | 我们先讲解 注册用户,产生激活码吧 115 | 116 | 117 | ``` 118 | try 119 | { 120 | // 从表单收集的数据 注册用户 121 | $user = Sentry::register(array( 122 | 'email' => 'yccphp@163.com', 123 | 'password' => '123456', 124 | )); 125 | 126 | // 获取此用户的 激活码 127 | $activationCode = $user->getActivationCode(); 128 | 129 | // 你可以继续 ,把激活码通过短信,或者 email 发送给 用户 130 | } 131 | catch (Cartalyst\Sentry\Users\LoginRequiredException $e) 132 | { 133 | echo 'login 字段是必须的'; 134 | } 135 | catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) 136 | { 137 | echo '没有提供密码'; 138 | } 139 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 140 | { 141 | echo '用户已存在'; 142 | } 143 | 144 | ``` 145 | 我们看上面的代码,首先我们调用 register 注册了一个新用户,这个 register 的参数如下 146 | 147 | Param | Required | Default | Type | Description 148 | ------------ | -------- | ------- | ------- | ----------------------------------- 149 | $credentials | true | null | array | 用户的资料。 150 | $activate | false | false | boolean | 是否手动激活 151 | 152 | 153 | 我们看 第二个参数 activate ,默认是 false ,就是需要会员手动来激活。 154 | 155 | 我们上面的代码,就是需要会员手动激活,然后我们调用了 getActivationCode ,产生了一个 激活码,来作为激活用户的凭证 156 | 157 | 158 | 如果我们不需要会员手动激活,只需要要 把 第二个参数设置为 true ,就可以跳过此步骤 159 | 160 | 161 | 162 | 好了,我们已经完成了,注册用户,产生激活码的步骤了,至于怎么激活用户呢?请看 [3.5激活用户](actUser.md) -------------------------------------------------------------------------------- /users/selectUser.md: -------------------------------------------------------------------------------- 1 | #各种查询用户的方法集合 2 | 3 | 具体的检查用户是否具有某个权限,我们单开权限篇讲解,本来给用户规划了一个权限篇的,但是分组的权限,和用户的权限,其实都是一样的,所以我们单开一个权限篇讲解 4 | 5 | [2.3 权限篇](priLogin.md) 6 | 7 | 8 | 单独开出一节,讲解通过这种方法查询用户的方法 9 | 10 | 11 | ### 查询当前登陆用户 12 | 13 | ``` 14 | // 获得当前登陆的用户 15 | $user = Sentry::getUser(); 16 | 17 | $user->first_name; 18 | 19 | 20 | ``` 21 | 22 | 23 | ### 查询所有用户 24 | 25 | ``` 26 | $users = Sentry::findAllUsers(); 27 | 28 | ``` 29 | 30 | ### 查找具有指定权限的用户 31 | 32 | ``` 33 | 34 | // 当仅指定一个权限时,数组参数,只要数组元素中的权限码,用户具有一个 就可以查出 35 | $users = Sentry::findAllUsersWithAccess(array('admin', 'other')); 36 | 37 | // 查询 具有 admin 权限的用户 38 | $users = Sentry::findAllUsersWithAccess('admin'); 39 | 40 | ``` 41 | 42 | ### 查找分组下的所有用户 43 | 44 | ``` 45 | // 查出分组 46 | $group = Sentry::findGroupByName('admin'); 47 | 48 | // 查出分组下的所有用户 49 | $users = Sentry::findAllUsersInGroup($group); 50 | 51 | ``` 52 | 53 | 54 | ### 根据资料,查询用户 55 | 56 | 根据凭证数组查找一个用户,其中必须包含登录字段。 57 | 58 | ``` 59 | try 60 | { 61 | $user = Sentry::findUserByCredentials(array( 62 | 'email' => 'yccphp@163.com', 63 | 'password' => '101058', 64 | 'first_name' => 'yuan', 65 | )); 66 | } 67 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 68 | { 69 | echo '用户不存在'; 70 | } 71 | 72 | ``` 73 | 74 | ### 根据用户id 查找用户 75 | 76 | ``` 77 | 78 | try 79 | { 80 | $user = Sentry::findUserById(1); 81 | } 82 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 83 | { 84 | echo '用户不存在'; 85 | } 86 | 87 | ``` 88 | 89 | ### 通过登录字段查找一个用户 90 | 91 | 登录字段,默认为 email ,可配置,详解 [2.2配置登陆字段](setlogin.md) 92 | 93 | ``` 94 | try 95 | { 96 | $user = Sentry::findUserByLogin('yccphp@163.com'); 97 | } 98 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 99 | { 100 | echo '用户不存在'; 101 | } 102 | 103 | ``` 104 | 105 | ### 通过激活码查找一个用户 106 | 107 | 我们注册的时候,有发送给用户一个激活码,这个方法就是根据激活码来查找这个用户 108 | 109 | ``` 110 | 111 | try 112 | { 113 | $user = Sentry::findUserByActivationCode(激活码); 114 | } 115 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 116 | { 117 | echo '用户不存在.'; 118 | } 119 | ``` 120 | 121 | ### 通过密码重置码 查找一个用户 122 | 123 | 对应的,我们在用户重置密码的时候,也给用户发送了一个 重置码 ,我们可以通过它,来查找用户 124 | 125 | ``` 126 | try 127 | { 128 | $user = Sentry::findUserByResetPasswordCode(重置码); 129 | } 130 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 131 | { 132 | echo '用户不存在'; 133 | } 134 | 135 | ``` 136 | 137 | ### 验证密码 138 | 139 | 这个功能有点鸡肋,先查询出用户,在验证密码,不知道在哪些地方有用,所以归类到 查询方法集合 140 | 141 | ``` 142 | try 143 | { 144 | // 查询用户 145 | $user = Sentry::findUserById(1); 146 | 147 | if($user->checkPassword('密码')) 148 | { 149 | echo '密码正确'; 150 | } 151 | else 152 | { 153 | echo '密码错误'; 154 | } 155 | } 156 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 157 | { 158 | echo '用户不存在'; 159 | } 160 | 161 | 162 | ``` 163 | 164 | ### 查询用户的分组 165 | 166 | ``` 167 | try 168 | { 169 | // 查询用户 170 | $user = Sentry::findUserByID(1); 171 | 172 | // 获取用户的分组 173 | $groups = $user->getGroups(); 174 | } 175 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 176 | { 177 | echo '用户不存在'; 178 | } 179 | 180 | ``` 181 | 182 | 183 | ### 获取用户的权限 184 | 185 | ** 注意:只返回用户的权限,不包括所在分组的权限** 186 | ``` 187 | try 188 | { 189 | // 查询一个用户 190 | $user = Sentry::findUserByID(1); 191 | 192 | // 获取用户的权限 193 | $permissions = $user->getPermissions(); 194 | } 195 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 196 | { 197 | echo '用户不存在.'; 198 | } 199 | 200 | ``` 201 | 202 | ### 获取用户分组 + 个人的权限 203 | ** 它合并了 用户的分组权限 和 个人权限** 204 | 205 | ``` 206 | 207 | try 208 | { 209 | // 查询用户 210 | $user = Sentry::getUserProvider()->findById(1); 211 | 212 | // 获得合并后的权限信息 213 | $permissions = $user->getMergedPermissions(); 214 | } 215 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 216 | { 217 | echo '用户不存在.'; 218 | } 219 | 220 | ``` 221 | 222 | -------------------------------------------------------------------------------- /basics/priLogin.md: -------------------------------------------------------------------------------- 1 | #权限篇 2 | 3 | 首先,我们需要知道,用户组权限和用户权限是一样的 4 | 5 | 给用户组分配权限,无非就是给用户组的 permissions 字段赋值 6 | 7 | 给用户分配权限,无非就是给 用户 permissions 字段赋值 8 | 9 | 用户的权限优先级高于 用户组,所以相同的权限,定义在 用户 permissions 里面,会覆盖用户组的权限 10 | 11 | 然后我们看下 12 | 13 | ### 权限是什么? 14 | 15 | 权限,就是定义 你可以干什么,不可以干什么 16 | 17 | 比如: 18 | 19 | 我们的一个学生管理系统,分下列角色 20 | 21 | 一:管理组 22 | 23 | 1.录入员 (只可以录入学生信息) 24 | 2.教师 (只可以查看学生的基本信息) 25 | 3.校长 (可以删除学生信息,查看学生相册,看私房照) 26 | 27 | 28 | 二:用户组 29 | 30 | 1.学生 (只可以查询自己的信息) 31 | 32 | 33 | 34 | 我们分析上面所有动作 35 | 36 | 1.录入学生信息 37 | 2.查看学生信息 38 | 3.删除学生信息 39 | 4.查看学生相册 40 | 41 | 可以清晰的看到,管理组特征是 都拥有 查看学生的信息 这个权限,用户组 只有 查看信息的权限 42 | 43 | 44 | 权限对应为 45 | 46 | ``` 47 | // 录入员 48 | $admin_1 = array( 49 | 'create.xuesheng'=>1, // 允许 创建学生 50 | 'xuesheng.chakan'=>0, // 拒绝 查看学生 51 | 'xuesheng.zhaopian'=>0, // 拒绝 看学生私房照 52 | 53 | ); 54 | 55 | // 教师 56 | $admin_2 = array( 57 | 'xuesheng.chakan'=>1, // 允许 查看学生 58 | 'create.xuesheng'=>0, // 拒绝 创建学生 59 | 'xuesheng.zhaopian'=>0, //拒绝 看学生私房照 60 | 61 | ); 62 | 63 | // 校长 64 | $admin_3 = array( 65 | 'create.xuesheng'=>1, // 允许 创建学生 66 | 'xuesheng.chakan'=>1, // 允许 查看学生 67 | 'xuesheng.zhaopian'=>1, //允许 看学生私房照 68 | ); 69 | 70 | // 学生 71 | $user = array( 72 | 'xuesheng.chakan'=>1, //允许 查看学生 73 | 'create.xuesheng'=>0, //拒绝 创建学生 74 | 'xuesheng.zhaopian'=>0, //拒绝 看学生私房照 75 | 76 | ); 77 | 78 | ``` 79 | 80 | 上面的 0 1 代表什么意思呢? 81 | 82 | 在用户组权限中 83 | 84 | ``` 85 | 0 : 拒绝 86 | 1 : 允许 87 | 88 | ``` 89 | 90 | 在 用户权限中 91 | 92 | ``` 93 | -1 : 拒绝 94 | 1 : 允许 95 | 0 : 继承 96 | 97 | ``` 98 | 99 | 所以,我们给不同的组不同的权限,然后 给用户分配 用户组,用户的权限就 继承了用户组的权限啦 100 | 101 | 102 | 103 | ### 用户权限 104 | 105 | **我们曾经说过,用户权限等级 高于 用户组权限** 106 | 107 | 比如: 108 | 109 | 一个 名为:张三 的老师,他属于管理组,但是,他得罪了校长,校长 给他打入冷宫30 天,期间不可以查看学生信息 110 | 111 | 这个时候,他原先就属于管理组,总不能给他 t 出 管理组吧? 112 | 113 | 这个时候,用户权限就派上用场了 114 | 115 | ``` 116 | 117 | 118 | try 119 | { 120 | // 权限信息 121 | 122 | $per = array( 123 | 'xuesheng.chakan'=>-1 124 | ); 125 | 126 | // 查出张三的信息 127 | $user = Sentry::findUserById(1); 128 | 129 | // login 字段是必须的 130 | $user->email = 'yccphp@163.com'; 131 | $user->permissions = $per; 132 | 133 | // 执行更新 134 | if ($user->save()) 135 | { 136 | // 更新成功 137 | } 138 | else 139 | { 140 | // 更新失败 141 | } 142 | } 143 | catch (Cartalyst\Sentry\Users\UserExistsException $e) 144 | { 145 | echo 'login 字段是必须的'; 146 | } 147 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 148 | { 149 | echo '用户不存在'; 150 | } 151 | 152 | ``` 153 | 154 | 假如 在管理员组中 xuesheng.chakan = 1 ,我们就可以定义用户权限 xuesheng.chakan = -1 ,这个时候,用户的权限,就覆盖了管理员组的权限,他就不可以访问了 155 | 156 | 157 | 158 | 159 | ### 验证用户是否具有指定的权限 160 | 161 | 比如我们检测 张三 老师 是否具有查看学生的权限 162 | 163 | ``` 164 | 165 | try 166 | { 167 | // 查出张三老师 168 | $user = Sentry::findUserByID(1); 169 | 170 | // 要检查多个权限需要使用一个数组来进行参数传递 171 | if ($user->hasAccess('xuesheng.chakan')) 172 | { 173 | // 有权限 174 | } 175 | else 176 | { 177 | // 没权限 178 | } 179 | } 180 | catch (Cartalyst\Sentry\UserNotFoundException $e) 181 | { 182 | echo '用户不存在'; 183 | } 184 | 185 | ``` 186 | 187 | 这个检查权限,会先把 用户的权限查出来,用户组的权限查出来,然后合并,检查 188 | 189 | ### 验证用户是否具有指定权限的一种 190 | 191 | 也就是说,我给出很多权限,你只要有 具有一个,你就通过了 192 | 193 | ``` 194 | 195 | try 196 | { 197 | $user = Sentry::getUserProvider()->findById(1); 198 | 199 | // admin 或者 foo 随便具有其中一个,你就通过 200 | if ($user->hasAnyAccess(array('admin', 'foo'))) 201 | { 202 | // 有权限 203 | } 204 | else 205 | { 206 | // 没权限 207 | } 208 | } 209 | catch (Cartalyst\Sentry\UserNotFoundException $e) 210 | { 211 | echo '用户不存在'; 212 | } 213 | 214 | ``` 215 | 216 | ### 检查用户是否被激活 217 | 218 | 我们做网站的什么,通常用户没被激活是不可以登陆的,我们这就是检查他是不是被激活了 219 | 220 | ``` 221 | try 222 | { 223 | 224 | $user = Sentry::findUserByLogin('yccphp@163.com'); 225 | 226 | // 检查是否被激活 227 | if ($user->isActivated()) 228 | { 229 | // 是 230 | } 231 | else 232 | { 233 | //否 234 | } 235 | } 236 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 237 | { 238 | echo '用户不存在'; 239 | } 240 | 241 | ``` 242 | 243 | 244 | ### 检查用户是否存在一个指定的分组中 245 | 246 | 我们做权限控制的时候,有时候,一些东西,只能 admin 这个管理员组可以访问,所以我们也没必要做那么细,直接检查是否存在于 admin 这个管理员组就行了 247 | 248 | ``` 249 | 250 | try 251 | { 252 | $user = Sentry::findUserByID(1); 253 | 254 | // 查询组 255 | $admin = Sentry::findGroupByName('admin'); 256 | 257 | // 检查 258 | if ($user->inGroup($admin)) 259 | { 260 | // 存在 261 | } 262 | else 263 | { 264 | // 不存在 265 | } 266 | } 267 | catch (Cartalyst\Sentry\Users\UserNotFoundException $e) 268 | { 269 | echo '用户不存在'; 270 | } 271 | catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) 272 | { 273 | echo '管理员组不存在'; 274 | } 275 | 276 | ``` 277 | 278 | 权限到这讲完了,其实权限这个东西,没那么复杂,也不是需要多么高深的代码来实现,只是一种原理而已 279 | 280 | ### 扩展篇 281 | 282 | 我们上面学会了,怎么给用户权限,怎么检查权限,那么,问题来了, 283 | 284 | 我们需要在每个 action 里面,都检查用户权限吗 ? 285 | 286 | 答案是,不需要 287 | 288 | ``` 289 | // 他会 获得当前访问的路由名称 290 | Route::currentRouteName() 291 | 292 | ``` 293 | 294 | 我们可以在 filters.php 里面, 295 | 296 | ``` 297 | 298 | // 获得当前登陆的用户 299 | $user = Sentry::getUser(); 300 | 301 | $per = Route::currentRouteName(); 302 | 303 | // 要检查多个权限需要使用一个数组来进行参数传递 304 | if ($user->hasAccess($per)) 305 | { 306 | // 用户有权限访问当前的 action 307 | 308 | } 309 | else 310 | { 311 | // 没权限 312 | } 313 | 314 | 315 | 316 | 317 | ``` 318 | 319 | 哈哈,这个东西手册里面可是没有哦,感谢 @ShineForce 的提醒,为了让大家可以偷下懒,特意去看了下 Route 的代码 320 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------