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
+1
View File
@@ -0,0 +1 @@
*
+220
View File
@@ -0,0 +1,220 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:33:23
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:34:36
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\base\NotSupportedException;
use backend\models\AuthAssignment;
use yii\web\IdentityInterface;
class Adminer extends ActiveRecord implements IdentityInterface
{
public $password;
const STATUS_DELETE = 0;
const STATUS_ACTIVE = 10;
public static $statusTexts = [
self::STATUS_DELETE => '禁用',
self::STATUS_ACTIVE => '启用',
];
public static $statusStyles = [
self::STATUS_DELETE => 'label-warning',
self::STATUS_ACTIVE => 'label-info',
];
public static function tableName() {
return '{{%c_adminer}}';
}
public function behaviors() {
return [
TimestampBehavior::className(),
];
}
public function rules() {
return [
[['username', 'email'], 'required'],
[['username'], 'string', 'length' => [5, 32]],
[['nickname'],'string','max'=>32],
[['password'], 'required', 'on' => ['create']],
[['auth_key'], 'string', 'length' => 32],
[['username', 'email','mobile'], 'unique'],
[['email'], 'email'],
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [
self::STATUS_DELETE,self::STATUS_ACTIVE,1
]], //状态值1是软删除
[['status', 'created_at', 'updated_at'], 'integer'],
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
//去除首尾空白
[['username', 'nickname', 'mobile', 'email'], 'filter', 'filter' => 'trim'],
];
}
public function attributeLabels() {
return [
'id' => 'ID',
'username' => Yii::t('common' ,'Username'),
'nickname' => '姓名',
'auth_key' => Yii::t('common' ,'Password'),
'password_hash' => 'Password Hash',
'email' => Yii::t('common' ,'Email'),
'mobile' => Yii::t('common' ,'Mobile'),
'status' => '状态',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'user_role' => '所属角色',
'role' => Yii::t('common' ,'Role'),
];
}
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['create'] = ['username', 'email', 'password', 'status'];
$scenarios['update'] = ['username', 'email', 'password', 'status'];
return $scenarios;
}
/*用户组 S*/
public function getUsergrouplist()
{
/**
* 第一个参数为要关联的字表模型类名称,
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
*/
return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
}
public function getUsergroup()
{
/**
* 第一个参数为要关联的字表模型类名称,
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
*/
return $this->hasOne(AuthAssignment::className(), ['user_id' => 'id']);
}
/*用户组 E*/
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
public function beforeSave($insert) {
if($this->isNewRecord) {
$this->generateAuthKey();
}
if(!empty($this->password)) $this->setPassword($this->password);
return parent::beforeSave($insert);
}
/**
* 生成加密后的密码
* @param [string] $password [用户密码]
*/
public function setPassword($password) {
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/*获取所有用户*/
public function get_all_user(){
// $user = Yii::$app->db->createCommand('select * from '. tableName())->queryAll();
$user = static::findAll();
return $user;
}
/**
* 获取账号信息
* @param [int] $id [后台用户id]
*/
public static function findIdentity($id) {
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
public static function findIdentityByAccessToken($token, $type = null) {
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* 获取账号信息
* @param [string] $username [用户名]
*/
public static function findByUsername($username) {
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
//获取用户所在的用户组
public function getGroup() {
return implode(',', array_keys(Yii::$app->authManager->getRolesByUser($this->id)));
}
/**
* 获取账号主键值(id)
*/
public function getId() {
return $this->getPrimaryKey();
}
/**
* 获取账号auth_key值
*/
public function getAuthKey() {
return $this->auth_key;
}
/**
* 验证auth_key
* @param [string] $authKey [auth key]
*/
public function validateAuthKey($authKey) {
return $this->getAuthKey() === $authKey;
}
/**
* 验证用户密码
* @param [string] $password [用户密码]
*/
public function validatePassword($password) {
return Yii::$app->security->validatePassword($password, $this->password_hash);
return true;
}
/**
* 获取账号状态
*/
public function getStatus() {
return self::$statusTexts[$this->status];
}
/**
* 获取账号状态样式
*/
public function getStatusStyle() {
return self::$statusStyles[$this->status];
}
/**
* 获取状态
*/
public function getStatusTexts() {
return self::$statusTexts;
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:34:17
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:34:25
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
/**
* This is the model class for table "auth_assignment".
*
* @property string $item_name
* @property string $user_id
* @property integer $created_at
*
* @property AuthItem $itemName
*/
class AuthAssignment extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%auth_assignment}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['item_name', 'user_id'], 'required'],
[['created_at'], 'integer'],
[['item_name', 'user_id'], 'string', 'max' => 64],
[['item_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['item_name' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'item_name' => 'Item Name',
'user_id' => 'User ID',
'created_at' => 'Created At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getItemName()
{
return $this->hasOne(AuthItem::className(), ['name' => 'item_name']);
}
}
+224
View File
@@ -0,0 +1,224 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:35:20
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:35:48
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
use common\models\base\BaseModel;
use backend\models\Menu;
/**
* This is the model class for table "auth_item".
*
* @property string $name
* @property integer $type
* @property string $description
* @property string $rule_name
* @property string $data
* @property integer $created_at
* @property integer $updated_at
*
* @property AuthAssignment[] $authAssignments
* @property AuthRule $ruleName
* @property AuthItemChild[] $authItemChildren
* @property AuthItemChild[] $authItemChildren0
* @property AuthItem[] $children
* @property AuthItem[] $parents
*/
class AuthItem extends BaseModel
{
const T_ROLE = 1; //角色
const T_POWER = 2; //权限
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_DELETE = 'delete';
const SCENARIOS_UPDATE = 'update';
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%auth_item}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'type'], 'required'],
[['type', 'created_at', 'updated_at'], 'integer'],
[['description', 'data'], 'string'],
[['name', 'rule_name'], 'string', 'max' => 64],
[['rule_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthRule::className(), 'targetAttribute' => ['rule_name' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => '名称',
'type' => '类型',
'description' => '描述',
'rule_name' => '规则名称',
'data' => 'Data',
'created_at' => '创建时间',
'updated_at' => '更新时间',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthAssignments()
{
return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRuleName()
{
return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren()
{
return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren0()
{
return $this->hasMany(AuthItemChild::className(), ['child' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChildren()
{
return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable('auth_item_child', ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParents()
{
return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']);
}
/**
* 场景设置
* @see \yii\base\Model::scenarios()
*/
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['name', 'type', 'description'],
self:: SCENARIOS_DELETE => ['name'],
self:: SCENARIOS_UPDATE => ['name', 'type', 'description'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
/**
* 角色&权限的创建方法
* @return boolean 返回成功或者失败的状态值
*/
public function addItem()
{
//实例化AuthManager类
$auth = Yii::$app->authManager;
if($this->type == self::T_ROLE){
$item = $auth->createRole($this->name);
$item->description = $this->description?:'创建['.$this->name.']角色';
}else{
$item = $auth->createPermission($this->name);
$item->description = $this->description?:'创建['.$this->name.']权限';
}
return $auth->add($item);
}
/**
* 角色&权限的删除方法
* @return boolean 返回成功或者失败的状态值
*/
public function romoveItem()
{
$this->name = trim($this->name);
if($this->validate()){
$auth = Yii::$app->authManager;
$item = $auth->getRole($this->name)?:$auth->getPermission($this->name);
return $auth->remove($item);
}
return false;
}
/**
* 获取权限
* @param unknown $id
* @throws \Exception
*/
public function getItem($id) {
$model = AuthItem:: findOne(['name'=>$id]);
if(!$model)
throw new \Exception( '编辑的角色或权限不存在!' );
$this-> name = $model-> name;
$this-> type = $model-> type;
$this-> description = $model-> description;
return $this;
}
/**
* 编辑权限
* @param unknown $item_name
* @return boolean
*/
public function updateItem($item_name)
{
$auth = Yii:: $app-> authManager;
if($this->type == self::T_ROLE){
$item = $auth->createRole($this-> name);
$item-> description = $this-> description?: '创建['.$this-> name. ']角色';
}else{
$item = $auth->createPermission($this-> name);
$item-> description = $this-> description?: '创建['.$this-> name. ']权限';
}
return $auth->update($item_name, $item);
}
//获取所有的权限
public function getAllPermission(){
$permission = self::find()->where(['type'=>2])->all();
return $permission;
}
//关联菜单数据
public function getPermissionName(){
return $this->hasOne(Menu::className(), ['route' => 'name']);
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
/**
* @Author: fm453
* @Date: 2021-04-26 08:32:06
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:36:26
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
/**
* This is the model class for table "auth_item_child".
*
* @property string $parent
* @property string $child
*
* @property AuthItem $parent0
* @property AuthItem $child0
*/
class AuthItemChild extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%auth_item_child}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['parent', 'child'], 'required'],
[['parent', 'child'], 'string', 'max' => 64],
[['parent'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['parent' => 'name']],
[['child'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['child' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'parent' => 'Parent',
'child' => 'Child',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParent0()
{
return $this->hasOne(AuthItem::className(), ['name' => 'parent']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChild0()
{
return $this->hasOne(AuthItem::className(), ['name' => 'child']);
}
public function getPermission($id){
$permission = self::find()->where(['parent'=>$id])->asArray()->all();
return $permission;
}
}
+67
View File
@@ -0,0 +1,67 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:36:45
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:40:19
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
/**
* This is the model class for table "auth_rule".
*
* @property string $name
* @property string $data
* @property integer $created_at
* @property integer $updated_at
*
* @property AuthItem[] $authItems
*/
class AuthRule extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%auth_rule}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['data'], 'string'],
[['created_at', 'updated_at'], 'integer'],
[['name'], 'string', 'max' => 64],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => 'Name',
'data' => 'Data',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItems()
{
return $this->hasMany(AuthItem::className(), ['rule_name' => 'name']);
}
}
+85
View File
@@ -0,0 +1,85 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:37:16
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:40:11
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
/**
* @author Xianan Huang
*
*/
use Yii;
use yii\base\Model;
use backend\models\AuthRule;
class ItemForm extends Model
{
public $name;
public $type;
public $description;
const T_ROLE = 1; //角色
const T_POWER = 2; //权限
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_DELETE = 'delete';
public function rules()
{
return [
[['name', 'type'], 'required'],
['name', 'unique', 'targetClass' => '\common\models\AuthItemModel', 'message' => '此名称已经被占用。'],
['type', 'integer'],
[['name', 'description'], 'string', 'max' => 25],
[['rule_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthRule::className(), 'targetAttribute' => ['rule_name' => 'name']]
];
}
public function attributeLabels()
{
return [
'name' => '名称',
'type' => '类型',
'description' => '描述',
];
}
/**
* 场景设置
* @see \yii\base\Model::scenarios()
*/
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['name', 'type', 'description'],
self:: SCENARIOS_DELETE => ['name'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
/**
* 角色&权限的创建方法
* @return boolean 返回成功或者失败的状态值
*/
public function addItem()
{
//实例化AuthManager类
$auth = Yii::$app->authManager;
if($this->type == self::T_ROLE){
$item = $auth->createRole($this->name);
$item->description = $this->description?:'创建['.$this->name.']角色';
}else{
$item = $auth->createPermission($this->name);
$item->description = $this->description?:'创建['.$this->name.']权限';
}
return $auth->add($item);
}
}
+133
View File
@@ -0,0 +1,133 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-09 22:59:04
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 19:55:54
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
use yii\base\Model;
use common\models\CLog;
use backend\models\Adminer;
/**
* Login form
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user; //防止被外部程序修改
/**
* @inheritdoc
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => Yii::t('common','Username'),
'password' => Yii::t('common','Password'),
'rememberMe' => Yii::t('common','Remember Me'),
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, Yii::t('common','Incorrect username or password.'));
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false; //如果为true 将允许任意密码登陆
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = Adminer::findByUsername($this->username); //千万注意后端的登陆使用的是Adminer模型,不是User
}
return $this->_user;
}
//登录记录
public function loginLog(){
$request = Yii::$app->getRequest();
$nowFile = $request -> getScriptFile();
$data = "登陆系统|途径:".$nowFile;
$model = new CLog();
$model->username = $this->username;
$model->ip = Yii::$app->request->userIP;
$model->data = $data;
$model->create_time = time();
$model->save();
//编制邮件发送通知
$message = Yii::$app->mailer->compose();
$message->setFrom(Yii::$app->params['noticeEmail']);
//给登陆者发送
$subject = "账号登陆提醒";
$body = "您的账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl;
$message->setTo(Yii::$app->user->identity->email)
->setSubject($subject)
->setTextBody($body)
->send();
//给管理人员发送
$subject = "账号登陆提醒";
$body = "运维账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl;
$message->setTo('1280880631@qq.com')
->setSubject($subject)
->setTextBody($body)
->send();
}
}
+159
View File
@@ -0,0 +1,159 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-10 17:39:57
* @Last Modified by: fm453
* @Last Modified time: 2021-09-10 17:40:04
* @Email: fm453@lukegzs.com
*/
namespace backend\models;
use Yii;
/**
* This is the model class for table "c_menu".
*
* @property integer $id
* @property string $name
* @property integer $parent
* @property string $route
* @property integer $order
* @property string $data
*/
class Menu extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%c_menu}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['parent', 'sort'], 'integer'],
[['data'], 'string'],
[['status'], 'string'],
[['name'], 'string', 'max' => 128],
[['route'], 'string', 'max' => 256],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => '名称',
'parent' => '父级',
'route' => '路由',
'sort' => '排序',
'data' => '描述',
];
}
//获取顶级菜单列表
public function getAllMenu(){
$menu = Yii::$app->db->createCommand("SELECT * FROM ".$this->tableName()." WHERE parent='0' ")->queryAll();
return $menu;
}
//获取所有菜单列表
public function getMenuList(){
$menu = Yii::$app->db->createCommand("SELECT * FROM ".$this->tableName()." ORDER BY sort ASC ")->queryAll();
$menu = self::list_to_tree($menu,'id','parent');
return $menu;
}
//获取左侧菜单列表
public function getLeftMenuList(){
$uid = Yii::$app->user->identity->getId();
$auth = Yii::$app->authManager;
$Permission = $auth->getPermissionsByUser($uid);
$RoutesList = '';
foreach($Permission as $vo){
$RoutesList .= "'".$vo->name."',";
}
$RoutesList = substr($RoutesList,0,-1);
if($RoutesList == ''){
$RoutesList = "'/index/welcome','/site/index'";
}
$menu = Yii::$app->db->createCommand("SELECT * FROM ".$this->tableName()." WHERE route IN ($RoutesList) ORDER BY `order` ASC")->queryAll();
$menu = Yii::$app->db->createCommand("SELECT * FROM ".$this->tableName()." ORDER BY `order` ASC")->queryAll();
$menu = self::list_to_tree2($menu,'id','parent');
return $menu;
}
//通过id找到router
public function getRouteById($id){
$router = Yii::$app->db->createCommand("SELECT * FROM `".$this->tableName()."` WHERE id='$id'")->queryOne();
return $router['route'];
}
/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$list[$key]['name'] ='&nbsp;&nbsp;&nbsp;&nbsp;|--'.$list[$key]['name'];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
function list_to_tree2($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$list[$key]['name'] =$list[$key]['name'];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
}