AuthorizeMiddlewareTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace Illuminate\Tests\Auth;
  3. use Illuminate\Auth\Access\AuthorizationException;
  4. use Illuminate\Auth\Access\Gate;
  5. use Illuminate\Auth\Middleware\Authorize;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Contracts\Auth\Access\Gate as GateContract;
  8. use Illuminate\Contracts\Routing\Registrar;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Events\Dispatcher;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Routing\Middleware\SubstituteBindings;
  13. use Illuminate\Routing\Router;
  14. use Mockery as m;
  15. use PHPUnit\Framework\TestCase;
  16. use stdClass;
  17. class AuthorizeMiddlewareTest extends TestCase
  18. {
  19. protected $container;
  20. protected $user;
  21. protected $router;
  22. protected function setUp(): void
  23. {
  24. parent::setUp();
  25. $this->user = new stdClass;
  26. Container::setInstance($this->container = new Container);
  27. $this->container->singleton(GateContract::class, function () {
  28. return new Gate($this->container, function () {
  29. return $this->user;
  30. });
  31. });
  32. $this->router = new Router(new Dispatcher, $this->container);
  33. $this->container->singleton(Registrar::class, function () {
  34. return $this->router;
  35. });
  36. }
  37. protected function tearDown(): void
  38. {
  39. m::close();
  40. Container::setInstance(null);
  41. }
  42. public function testSimpleAbilityUnauthorized()
  43. {
  44. $this->expectException(AuthorizationException::class);
  45. $this->expectExceptionMessage('This action is unauthorized.');
  46. $this->gate()->define('view-dashboard', function ($user, $additional = null) {
  47. $this->assertNull($additional);
  48. return false;
  49. });
  50. $this->router->get('dashboard', [
  51. 'middleware' => Authorize::class.':view-dashboard',
  52. 'uses' => function () {
  53. return 'success';
  54. },
  55. ]);
  56. $this->router->dispatch(Request::create('dashboard', 'GET'));
  57. }
  58. public function testSimpleAbilityAuthorized()
  59. {
  60. $this->gate()->define('view-dashboard', function ($user) {
  61. return true;
  62. });
  63. $this->router->get('dashboard', [
  64. 'middleware' => Authorize::class.':view-dashboard',
  65. 'uses' => function () {
  66. return 'success';
  67. },
  68. ]);
  69. $response = $this->router->dispatch(Request::create('dashboard', 'GET'));
  70. $this->assertSame('success', $response->content());
  71. }
  72. public function testSimpleAbilityWithStringParameter()
  73. {
  74. $this->gate()->define('view-dashboard', function ($user, $param) {
  75. return $param === 'some string';
  76. });
  77. $this->router->get('dashboard', [
  78. 'middleware' => Authorize::class.':view-dashboard,"some string"',
  79. 'uses' => function () {
  80. return 'success';
  81. },
  82. ]);
  83. $response = $this->router->dispatch(Request::create('dashboard', 'GET'));
  84. $this->assertSame('success', $response->content());
  85. }
  86. public function testSimpleAbilityWithNullParameter()
  87. {
  88. $this->gate()->define('view-dashboard', function ($user, $param = null) {
  89. $this->assertNull($param);
  90. return true;
  91. });
  92. $this->router->get('dashboard', [
  93. 'middleware' => Authorize::class.':view-dashboard,null',
  94. 'uses' => function () {
  95. return 'success';
  96. },
  97. ]);
  98. $this->router->dispatch(Request::create('dashboard', 'GET'));
  99. }
  100. public function testSimpleAbilityWithOptionalParameter()
  101. {
  102. $post = new stdClass;
  103. $this->router->bind('post', function () use ($post) {
  104. return $post;
  105. });
  106. $this->gate()->define('view-comments', function ($user, $model = null) {
  107. return true;
  108. });
  109. $middleware = [SubstituteBindings::class, Authorize::class.':view-comments,post'];
  110. $this->router->get('comments', [
  111. 'middleware' => $middleware,
  112. 'uses' => function () {
  113. return 'success';
  114. },
  115. ]);
  116. $this->router->get('posts/{post}/comments', [
  117. 'middleware' => $middleware,
  118. 'uses' => function () {
  119. return 'success';
  120. },
  121. ]);
  122. $response = $this->router->dispatch(Request::create('posts/1/comments', 'GET'));
  123. $this->assertSame('success', $response->content());
  124. $response = $this->router->dispatch(Request::create('comments', 'GET'));
  125. $this->assertSame('success', $response->content());
  126. }
  127. public function testSimpleAbilityWithStringParameterFromRouteParameter()
  128. {
  129. $this->gate()->define('view-dashboard', function ($user, $param) {
  130. return $param === 'true';
  131. });
  132. $this->router->get('dashboard/{route_parameter}', [
  133. 'middleware' => Authorize::class.':view-dashboard,route_parameter',
  134. 'uses' => function () {
  135. return 'success';
  136. },
  137. ]);
  138. $response = $this->router->dispatch(Request::create('dashboard/true', 'GET'));
  139. $this->assertSame('success', $response->content());
  140. }
  141. public function testModelTypeUnauthorized()
  142. {
  143. $this->expectException(AuthorizationException::class);
  144. $this->expectExceptionMessage('This action is unauthorized.');
  145. $this->gate()->define('create', function ($user, $model) {
  146. $this->assertSame('App\User', $model);
  147. return false;
  148. });
  149. $this->router->get('users/create', [
  150. 'middleware' => [SubstituteBindings::class, Authorize::class.':create,App\User'],
  151. 'uses' => function () {
  152. return 'success';
  153. },
  154. ]);
  155. $this->router->dispatch(Request::create('users/create', 'GET'));
  156. }
  157. public function testModelTypeAuthorized()
  158. {
  159. $this->gate()->define('create', function ($user, $model) {
  160. $this->assertSame('App\User', $model);
  161. return true;
  162. });
  163. $this->router->get('users/create', [
  164. 'middleware' => Authorize::class.':create,App\User',
  165. 'uses' => function () {
  166. return 'success';
  167. },
  168. ]);
  169. $response = $this->router->dispatch(Request::create('users/create', 'GET'));
  170. $this->assertSame('success', $response->content());
  171. }
  172. public function testModelUnauthorized()
  173. {
  174. $this->expectException(AuthorizationException::class);
  175. $this->expectExceptionMessage('This action is unauthorized.');
  176. $post = new stdClass;
  177. $this->router->bind('post', function () use ($post) {
  178. return $post;
  179. });
  180. $this->gate()->define('edit', function ($user, $model) use ($post) {
  181. $this->assertSame($model, $post);
  182. return false;
  183. });
  184. $this->router->get('posts/{post}/edit', [
  185. 'middleware' => [SubstituteBindings::class, Authorize::class.':edit,post'],
  186. 'uses' => function () {
  187. return 'success';
  188. },
  189. ]);
  190. $this->router->dispatch(Request::create('posts/1/edit', 'GET'));
  191. }
  192. public function testModelAuthorized()
  193. {
  194. $post = new stdClass;
  195. $this->router->bind('post', function () use ($post) {
  196. return $post;
  197. });
  198. $this->gate()->define('edit', function ($user, $model) use ($post) {
  199. $this->assertSame($model, $post);
  200. return true;
  201. });
  202. $this->router->get('posts/{post}/edit', [
  203. 'middleware' => [SubstituteBindings::class, Authorize::class.':edit,post'],
  204. 'uses' => function () {
  205. return 'success';
  206. },
  207. ]);
  208. $response = $this->router->dispatch(Request::create('posts/1/edit', 'GET'));
  209. $this->assertSame('success', $response->content());
  210. }
  211. public function testModelInstanceAsParameter()
  212. {
  213. $instance = m::mock(Model::class);
  214. $this->gate()->define('success', function ($user, $model) use ($instance) {
  215. $this->assertSame($model, $instance);
  216. return true;
  217. });
  218. $request = m::mock(Request::class);
  219. $nextParam = null;
  220. $next = function ($param) use (&$nextParam) {
  221. $nextParam = $param;
  222. };
  223. (new Authorize($this->gate()))
  224. ->handle($request, $next, 'success', $instance);
  225. }
  226. /**
  227. * Get the Gate instance from the container.
  228. *
  229. * @return \Illuminate\Auth\Access\Gate
  230. */
  231. protected function gate()
  232. {
  233. return $this->container->make(GateContract::class);
  234. }
  235. }