Initial commit,修正

This commit is contained in:
2025-07-13 19:37:38 +08:00
parent 38ce288e27
commit 65bbd06981
12384 changed files with 3985464 additions and 8 deletions
+142
View File
@@ -0,0 +1,142 @@
<?php
namespace mdm\admin\controllers;
use Yii;
use mdm\admin\models\Assignment;
use mdm\admin\models\searchs\Assignment as AssignmentSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* AssignmentController implements the CRUD actions for Assignment model.
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class AssignmentController extends Controller
{
public $userClassName;
public $idField = 'id';
public $usernameField = 'username';
public $fullnameField;
public $searchClass;
public $extraColumns = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->userClassName === null) {
$this->userClassName = Yii::$app->getUser()->identityClass;
$this->userClassName = $this->userClassName ? : 'mdm\admin\models\User';
}
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'assign' => ['post'],
'assign' => ['post'],
'revoke' => ['post'],
],
],
];
}
/**
* Lists all Assignment models.
* @return mixed
*/
public function actionIndex()
{
if ($this->searchClass === null) {
$searchModel = new AssignmentSearch;
$dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams(), $this->userClassName, $this->usernameField);
} else {
$class = $this->searchClass;
$searchModel = new $class;
$dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams());
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'idField' => $this->idField,
'usernameField' => $this->usernameField,
'extraColumns' => $this->extraColumns,
]);
}
/**
* Displays a single Assignment model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model,
'idField' => $this->idField,
'usernameField' => $this->usernameField,
'fullnameField' => $this->fullnameField,
]);
}
/**
* Assign items
* @param string $id
* @return array
*/
public function actionAssign($id)
{
$items = Yii::$app->getRequest()->post('items', []);
$model = new Assignment($id);
$success = $model->assign($items);
Yii::$app->getResponse()->format = 'json';
return array_merge($model->getItems(), ['success' => $success]);
}
/**
* Assign items
* @param string $id
* @return array
*/
public function actionRevoke($id)
{
$items = Yii::$app->getRequest()->post('items', []);
$model = new Assignment($id);
$success = $model->revoke($items);
Yii::$app->getResponse()->format = 'json';
return array_merge($model->getItems(), ['success' => $success]);
}
/**
* Finds the Assignment model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Assignment the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
$class = $this->userClassName;
if (($user = $class::findIdentity($id)) !== null) {
return new Assignment($id, $user);
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace mdm\admin\controllers;
use Yii;
/**
* DefaultController
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class DefaultController extends \yii\web\Controller
{
/**
* Action index
*/
public function actionIndex($page = 'README.md')
{
if (strpos($page, '.png') !== false) {
$file = Yii::getAlias("@mdm/admin/{$page}");
return Yii::$app->getResponse()->sendFile($file);
}
return $this->render('index', ['page' => $page]);
}
}
+134
View File
@@ -0,0 +1,134 @@
<?php
namespace mdm\admin\controllers;
use Yii;
use mdm\admin\models\Menu;
use mdm\admin\models\searchs\Menu as MenuSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use mdm\admin\components\Helper;
/**
* MenuController implements the CRUD actions for Menu model.
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class MenuController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['get'],
],
],
];
}
/**
* Lists all Menu models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new MenuSearch;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
/**
* Displays a single Menu model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Menu model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Menu;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Helper::invalidate();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Menu model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->menuParent) {
$model->parent_name = $model->menuParent->name;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Helper::invalidate();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Menu model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
Helper::invalidate();
return $this->redirect(['index']);
}
/**
* Finds the Menu model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Menu the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Menu::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace mdm\admin\controllers;
use mdm\admin\components\ItemController;
use yii\rbac\Item;
/**
* PermissionController implements the CRUD actions for AuthItem model.
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class PermissionController extends ItemController
{
/**
* @inheritdoc
*/
public function labels()
{
return[
'Item' => 'Permission',
'Items' => 'Permissions',
];
}
/**
* @inheritdoc
*/
public function getType()
{
return Item::TYPE_PERMISSION;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace mdm\admin\controllers;
use mdm\admin\components\ItemController;
use yii\rbac\Item;
/**
* RoleController implements the CRUD actions for AuthItem model.
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class RoleController extends ItemController
{
/**
* @inheritdoc
*/
public function labels()
{
return[
'Item' => 'Role',
'Items' => 'Roles',
];
}
/**
* @inheritdoc
*/
public function getType()
{
return Item::TYPE_ROLE;
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
namespace mdm\admin\controllers;
use Yii;
use mdm\admin\models\Route;
use yii\web\Controller;
use yii\filters\VerbFilter;
/**
* Description of RuleController
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class RouteController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'create' => ['post'],
'assign' => ['post'],
'remove' => ['post'],
'refresh' => ['post'],
],
],
];
}
/**
* Lists all Route models.
* @return mixed
*/
public function actionIndex()
{
$model = new Route();
return $this->render('index', ['routes' => $model->getRoutes()]);
}
/**
* Creates a new AuthItem model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
Yii::$app->getResponse()->format = 'json';
$routes = Yii::$app->getRequest()->post('route', '');
$routes = preg_split('/\s*,\s*/', trim($routes), -1, PREG_SPLIT_NO_EMPTY);
$model = new Route();
$model->addNew($routes);
return $model->getRoutes();
}
/**
* Assign routes
* @return array
*/
public function actionAssign()
{
$routes = Yii::$app->getRequest()->post('routes', []);
$model = new Route();
$model->addNew($routes);
Yii::$app->getResponse()->format = 'json';
return $model->getRoutes();
}
/**
* Remove routes
* @return array
*/
public function actionRemove()
{
$routes = Yii::$app->getRequest()->post('routes', []);
$model = new Route();
$model->remove($routes);
Yii::$app->getResponse()->format = 'json';
return $model->getRoutes();
}
/**
* Refresh cache
* @return type
*/
public function actionRefresh()
{
$model = new Route();
$model->invalidate();
Yii::$app->getResponse()->format = 'json';
return $model->getRoutes();
}
}
+130
View File
@@ -0,0 +1,130 @@
<?php
namespace mdm\admin\controllers;
use Yii;
use mdm\admin\models\BizRule;
use yii\web\Controller;
use mdm\admin\models\searchs\BizRule as BizRuleSearch;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;
use mdm\admin\components\Helper;
/**
* Description of RuleController
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class RuleController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all AuthItem models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BizRuleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
/**
* Displays a single AuthItem model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', ['model' => $model]);
}
/**
* Creates a new AuthItem model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new BizRule(null);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Helper::invalidate();
return $this->redirect(['view', 'id' => $model->name]);
} else {
return $this->render('create', ['model' => $model,]);
}
}
/**
* Updates an existing AuthItem model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Helper::invalidate();
return $this->redirect(['view', 'id' => $model->name]);
}
return $this->render('update', ['model' => $model,]);
}
/**
* Deletes an existing AuthItem model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
Yii::$app->authManager->remove($model->item);
Helper::invalidate();
return $this->redirect(['index']);
}
/**
* Finds the AuthItem model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return AuthItem the loaded model
* @throws HttpException if the model cannot be found
*/
protected function findModel($id)
{
$item = Yii::$app->authManager->getRule($id);
if ($item) {
return new BizRule($item);
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+276
View File
@@ -0,0 +1,276 @@
<?php
namespace mdm\admin\controllers;
use Yii;
use mdm\admin\models\form\Login;
use mdm\admin\models\form\PasswordResetRequest;
use mdm\admin\models\form\ResetPassword;
use mdm\admin\models\form\Signup;
use mdm\admin\models\form\ChangePassword;
use mdm\admin\models\User;
use mdm\admin\models\searchs\User as UserSearch;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\base\UserException;
use yii\mail\BaseMailer;
/**
* User controller
*/
class UserController extends Controller
{
private $_oldMailPath;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
// 'access' => [
// 'class' => AccessControl::className(),
// 'rules' => [
// [
// 'actions' => ['signup', 'reset-password', 'login', 'request-password-reset'],
// 'allow' => true,
// 'roles' => ['?'],
// ],
// [
// 'actions' => ['logout', 'change-password', 'index', 'view', 'delete', 'activate'],
// 'allow' => true,
// 'roles' => ['@'],
// ],
// ],
// ],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
'logout' => ['post'],
'activate' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if (Yii::$app->has('mailer') && ($mailer = Yii::$app->getMailer()) instanceof BaseMailer) {
/* @var $mailer BaseMailer */
$this->_oldMailPath = $mailer->getViewPath();
$mailer->setViewPath('@mdm/admin/mail');
}
return true;
}
return false;
}
/**
* @inheritdoc
*/
public function afterAction($action, $result)
{
if ($this->_oldMailPath !== null) {
Yii::$app->getMailer()->setViewPath($this->_oldMailPath);
}
return parent::afterAction($action, $result);
}
/**
* Lists all User models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single User model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Deletes an existing User model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Login
* @return string
*/
public function actionLogin()
{
if (!Yii::$app->getUser()->isGuest) {
return $this->goHome();
}
$model = new Login();
if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
/**
* Logout
* @return string
*/
public function actionLogout()
{
Yii::$app->getUser()->logout();
return $this->goHome();
}
/**
* Signup new user
* @return string
*/
public function actionSignup()
{
$model = new Signup();
if ($model->load(Yii::$app->getRequest()->post())) {
if ($user = $model->signup()) {
return $this->goHome();
}
}
return $this->render('signup', [
'model' => $model,
]);
}
/**
* Request reset password
* @return string
*/
public function actionRequestPasswordReset()
{
$model = new PasswordResetRequest();
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
return $this->goHome();
} else {
Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
}
}
return $this->render('requestPasswordResetToken', [
'model' => $model,
]);
}
/**
* Reset password
* @return string
*/
public function actionResetPassword($token)
{
try {
$model = new ResetPassword($token);
} catch (InvalidParamException $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->resetPassword()) {
Yii::$app->getSession()->setFlash('success', 'New password was saved.');
return $this->goHome();
}
return $this->render('resetPassword', [
'model' => $model,
]);
}
/**
* Reset password
* @return string
*/
public function actionChangePassword()
{
$model = new ChangePassword();
if ($model->load(Yii::$app->getRequest()->post()) && $model->change()) {
return $this->goHome();
}
return $this->render('change-password', [
'model' => $model,
]);
}
/**
* Activate new user
* @param integer $id
* @return type
* @throws UserException
* @throws NotFoundHttpException
*/
public function actionActivate($id)
{
/* @var $user User */
$user = $this->findModel($id);
if ($user->status == User::STATUS_INACTIVE) {
$user->status = User::STATUS_ACTIVE;
if ($user->save()) {
return $this->goHome();
} else {
$errors = $user->firstErrors;
throw new UserException(reset($errors));
}
}
return $this->goHome();
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}