AssignmentController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace common\modules\rbac\controllers;
  3. use common\modules\rbac\components\MenuHelper;
  4. use common\modules\rbac\models\Assignment;
  5. use common\modules\rbac\models\searchs\Assignment as AssignmentSearch;
  6. use Yii;
  7. use yii\filters\VerbFilter;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\web\Response;
  11. /**
  12. * AssignmentController implements the CRUD actions for Assignment model.
  13. *
  14. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  15. *
  16. * @since 1.0
  17. */
  18. class AssignmentController extends Controller
  19. {
  20. public $userClassName;
  21. public $idField = 'id';
  22. public $usernameField = 'username';
  23. public $searchClass;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function init()
  28. {
  29. parent::init();
  30. if ($this->userClassName === null) {
  31. $this->userClassName = Yii::$app->getUser()->identityClass;
  32. $this->userClassName = $this->userClassName ?: 'common\models\User';
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function behaviors()
  39. {
  40. return [
  41. 'verbs' => [
  42. 'class' => VerbFilter::className(),
  43. 'actions' => [
  44. 'assign' => ['post'],
  45. ],
  46. ],
  47. ];
  48. }
  49. /**
  50. * Lists all Assignment models.
  51. *
  52. * @return mixed
  53. */
  54. public function actionIndex()
  55. {
  56. if ($this->searchClass === null) {
  57. $searchModel = new AssignmentSearch();
  58. } else {
  59. $class = $this->searchClass;
  60. $searchModel = new $class();
  61. }
  62. $dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams(), $this->userClassName, $this->usernameField);
  63. return $this->render('index', [
  64. 'dataProvider' => $dataProvider,
  65. 'searchModel' => $searchModel,
  66. 'idField' => $this->idField,
  67. 'usernameField' => $this->usernameField,
  68. ]);
  69. }
  70. /**
  71. * Displays a single Assignment model.
  72. *
  73. * @param int $id
  74. *
  75. * @return mixed
  76. */
  77. public function actionView($id)
  78. {
  79. $model = $this->findModel($id);
  80. return $this->render('view', [
  81. 'model' => $model,
  82. 'idField' => $this->idField,
  83. 'usernameField' => $this->usernameField,
  84. ]);
  85. }
  86. /**
  87. * Assign or revoke assignment to user.
  88. *
  89. * @param int $id
  90. * @param string $action
  91. *
  92. * @return mixed
  93. */
  94. public function actionAssign()
  95. {
  96. $post = Yii::$app->request->post();
  97. $id = $post['id'];
  98. $action = $post['action'];
  99. $roles = $post['roles'];
  100. $manager = Yii::$app->authManager;
  101. $error = [];
  102. if ($action == 'assign') {
  103. foreach ($roles as $name) {
  104. try {
  105. $item = $manager->getRole($name);
  106. $item = $item ?: $manager->getPermission($name);
  107. $manager->assign($item, $id);
  108. } catch (\Exception $exc) {
  109. $error[] = $exc->getMessage();
  110. }
  111. }
  112. } else {
  113. foreach ($roles as $name) {
  114. try {
  115. $item = $manager->getRole($name);
  116. $item = $item ?: $manager->getPermission($name);
  117. $manager->revoke($item, $id);
  118. } catch (\Exception $exc) {
  119. $error[] = $exc->getMessage();
  120. }
  121. }
  122. }
  123. MenuHelper::invalidate();
  124. Yii::$app->response->format = Response::FORMAT_JSON;
  125. return[
  126. 'type' => 'S',
  127. 'errors' => $error,
  128. ];
  129. }
  130. /**
  131. * Search roles of user.
  132. *
  133. * @param int $id
  134. * @param string $target
  135. * @param string $term
  136. *
  137. * @return string
  138. */
  139. public function actionSearch($id, $target, $term = '')
  140. {
  141. Yii::$app->response->format = 'json';
  142. $authManager = Yii::$app->authManager;
  143. $roles = $authManager->getRoles();
  144. $permissions = $authManager->getPermissions();
  145. $avaliable = [];
  146. $assigned = [];
  147. foreach ($authManager->getAssignments($id) as $assigment) {
  148. if (isset($roles[$assigment->roleName])) {
  149. if (empty($term) || strpos($assigment->roleName, $term) !== false) {
  150. $assigned['Roles'][$assigment->roleName] = $assigment->roleName;
  151. }
  152. unset($roles[$assigment->roleName]);
  153. } elseif (isset($permissions[$assigment->roleName]) && $assigment->roleName[0] != '/') {
  154. if (empty($term) || strpos($assigment->roleName, $term) !== false) {
  155. $assigned['Permissions'][$assigment->roleName] = $assigment->roleName;
  156. }
  157. unset($permissions[$assigment->roleName]);
  158. }
  159. }
  160. if ($target == 'avaliable') {
  161. if (count($roles)) {
  162. foreach ($roles as $role) {
  163. if (empty($term) || strpos($role->name, $term) !== false) {
  164. $avaliable['Roles'][$role->name] = $role->name;
  165. }
  166. }
  167. }
  168. if (count($permissions)) {
  169. foreach ($permissions as $role) {
  170. if ($role->name[0] != '/' && (empty($term) || strpos($role->name, $term) !== false)) {
  171. $avaliable['Permissions'][$role->name] = $role->name;
  172. }
  173. }
  174. }
  175. return $avaliable;
  176. } else {
  177. return $assigned;
  178. }
  179. }
  180. /**
  181. * 获取公司管理员角色
  182. * @author nodelog
  183. */
  184. public function actionGetCompanyRole()
  185. {
  186. }
  187. /**
  188. * Finds the Assignment model based on its primary key value.
  189. * If the model is not found, a 404 HTTP exception will be thrown.
  190. *
  191. * @param int $id
  192. *
  193. * @return Assignment the loaded model
  194. *
  195. * @throws NotFoundHttpException if the model cannot be found
  196. */
  197. protected function findModel($id)
  198. {
  199. $class = $this->userClassName;
  200. if (($model = $class::findIdentity($id)) !== null) {
  201. return $model;
  202. } else {
  203. throw new NotFoundHttpException('The requested page does not exist.');
  204. }
  205. }
  206. }