Skip to content

Commit

Permalink
项目的用户关系绑定、个人信息、项目信息预览
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuiyong committed Oct 3, 2015
1 parent 1beff4b commit c599d84
Show file tree
Hide file tree
Showing 22 changed files with 1,034 additions and 79 deletions.
14 changes: 14 additions & 0 deletions components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

class Controller extends yii\web\Controller {

public $uid = null;

/**
* @param \yii\base\Action $action
* @return bool
*/
public function beforeAction($action) {
parent::beforeAction($action);
if (Yii::$app->user->id) {
$this->uid = Yii::$app->user->id;
}
return true;
}

/**
* json渲染. PS:调用此方法之前若有输出将会出错
*
Expand Down
5 changes: 5 additions & 0 deletions components/GlobalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace app\components;

use app\models\User;
use yii;

class GlobalHelper {
Expand Down Expand Up @@ -54,4 +55,8 @@ public static function convert2Utf8($text) {

return $out;
}

public static function formatAvatar($pic) {
return rtrim(User::AVATAR_ROOT, '/') . '/' . $pic;
}
}
26 changes: 17 additions & 9 deletions config/params.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<?php
/**
* Application parameters
* 亲,为方便大家,已经把必须修改为自己配置的选项已经带上*****了
* 此配置为测试配置,如果你不想消息泄露,请尽快修改为自己的邮箱smtp
*/
return [
'support.email' => '[email protected]',
'support.name' => 'admin',
// ******必须要与config/web.php 中mail模块的username一致*****
'support.email' => '[email protected]',
// 显示发件人的名字,可以随意
'support.name' => 'service',

'user.passwordResetTokenExpire' => 3600,
'user.emailConfirmationTokenExpire' => 43200, // 5 days

// 操作日志目录
'user.emailConfirmationTokenExpire' => 43200, // 5 days有效

// 头像图片后缀
'user.avatar.extension' => [
'jpg', 'png', 'jpeg',
],

// *******操作日志目录*******
'log.dir' => '/tmp/walle/',
// 指定邮箱后缀
// *******指定公司邮箱后缀*******
'mail-suffix' => [
'公司邮箱后缀.com', // 限制只有公司同学可注册,可多个
]
'huamanshu.com', # 支持多个
],
];
10 changes: 5 additions & 5 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'ip or host', # smtp 发件地址
'username' => 'admin@huamanshu.com', # smtp 发件用户名
'password' => 'password', # smtp 发件人的密码
'port' => 25, # smtp 端口
'encryption' => 'tls', # smtp 协议
'host' => 'smtp.exmail.qq.com', # smtp 发件地址
'username' => 'service@huamanshu.com', # smtp 发件用户名
'password' => 'K84erUuxg1bHqrfD', # smtp 发件人的密码
'port' => 25, # smtp 端口
'encryption' => 'tls', # smtp 协议
],
'messageConfig' => [
'charset' => 'UTF-8',
Expand Down
97 changes: 92 additions & 5 deletions controllers/ConfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
use app\components\Controller;
use app\models\Conf;
use app\models\User;
use app\models\Group;

class ConfController extends Controller
{

/**
* @param \yii\base\Action $action
* @return bool
*/
public function beforeAction($action) {
parent::beforeAction($action);
if (\Yii::$app->user->identity->role != User::ROLE_ADMIN) {
throw new \Exception('非管理员不能操作:(');
}
return true;
}

/**
* 配置项目列表
*
Expand All @@ -27,6 +40,62 @@ public function actionIndex() {
]);
}

/**
* 配置项目
*
* @param $projectId
* @return string
* @throws \Exception
*/
public function actionPreview($projectId) {
$this->layout = 'modal';
$conf = Conf::findOne($projectId);
if (!$conf) throw new \Exception('找不到项目');

return $this->render('preview', [
'conf' => $conf,
]);
}

/**
* 配置项目
*
* @param $projectId
* @return string
* @throws \Exception
*/
public function actionGroup($projectId) {
// 配置信息
$conf = Conf::findOne($projectId);
if (!$conf) {
throw new \Exception('项目不存在:)');
}
if ($conf->user_id != $this->uid) {
throw new \Exception('不可以操作其它人的项目:)');
}
// 添加用户
if (\Yii::$app->request->getIsPost()) {
Group::addGroupUser($projectId, \Yii::$app->request->post('user'));
}
// 项目的分组用户
$group = Group::find()
->with('user')
->where(['project_id' => $projectId])
->indexBy('user_id')
->asArray()->all();
// 所有用户
$users = User::find()
->select(['id', 'email', 'realname'])
->where(['is_email_verified' => 1])
->asArray()->all();

return $this->render('group', [
'conf' => $conf,
'users' => $users,
'group' => $group,
]);
}

/**
* 配置项目
*
Expand All @@ -35,10 +104,9 @@ public function actionIndex() {
* @throws \Exception
*/
public function actionEdit($projectId = null) {
if (\Yii::$app->user->identity->role != User::ROLE_ADMIN) throw new \Exception('非管理员不能操作:(');
$conf = $projectId ? Conf::findOne($projectId) : new Conf();
if (\Yii::$app->request->getIsPost() && $conf->load(Yii::$app->request->post())) {
$conf->user_id = \Yii::$app->user->id;
$conf->user_id = $this->uid;
if ($conf->save()) {
$this->redirect('/conf/');
}
Expand All @@ -56,16 +124,35 @@ public function actionEdit($projectId = null) {
* @return string
* @throws \Exception
*/
public function actionDelete($confId) {
$conf = Conf::findOne($confId);
public function actionDelete($projectId) {
$conf = Conf::findOne($projectId);
if (!$conf) {
throw new \Exception('项目不存在:)');
}
if ($conf->user_id != \Yii::$app->user->id) {
if ($conf->user_id != $this->uid) {
throw new \Exception('不可以操作其它人的项目:)');
}
if (!$conf->delete()) throw new \Exception('删除失败');
$this->renderJson([]);
}

/**
* 删除项目的用户关系
*
* @return string
* @throws \Exception
*/
public function actionDeleteRelation($id) {
$group = Group::findOne($id);
if (!$group) {
throw new \Exception('关系不存在:)');
}
$conf = Conf::findOne($group->project_id);
if ($conf->user_id != $this->uid) {
throw new \Exception('不可以操作其它人的项目:)');
}

if (!$group->delete()) throw new \Exception('删除失败');
$this->renderJson([]);
}
}
42 changes: 26 additions & 16 deletions controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
use app\models\Task;
use app\models\Conf;
use app\models\User;
use app\models\Group;

class TaskController extends Controller {

protected $task;

public function actionIndex($page = 1, $size = 10) {
$size = $this->getParam('per-page') ?: $size;
$user = User::findOne(\Yii::$app->user->id);
$list = Task::find()
->with('user')
->with('conf');
if ($user->role != User::ROLE_ADMIN) {
$list->where(['user_id' => \Yii::$app->user->id]);
if (\Yii::$app->user->identity->role != User::ROLE_ADMIN) {
$list->where(['user_id' => $this->uid]);
}

$kw = \Yii::$app->request->post('kw');
Expand All @@ -31,7 +31,7 @@ public function actionIndex($page = 1, $size = 10) {
$list = $tasks->offset(($page - 1) * $size)->limit(10)
->asArray()->all();

$view = $user->role == User::ROLE_ADMIN ? 'admin-list' : 'dev-list';
$view = \Yii::$app->user->identity->role == User::ROLE_ADMIN ? 'admin-list' : 'dev-list';
$pages = new Pagination(['totalCount' => $tasks->count(), 'pageSize' => 10]);
return $this->render($view, [
'list' => $list,
Expand All @@ -53,14 +53,22 @@ public function actionSubmit($projectId = null) {
->where(['id' => $projectId, 'status' => Conf::STATUS_VALID])
->one();
}
if (\Yii::$app->request->getIsPost() && $conf && $task->load(\Yii::$app->request->post())) {
// 是否需要审核
$status = $conf->audit == Conf::AUDIT_YES ? Task::STATUS_SUBMIT : Task::STATUS_PASS;
$task->user_id = \Yii::$app->user->id;
$task->project_id = $projectId;
$task->status = $status;
if ($task->save()) {
$this->redirect('/task/');
if (\Yii::$app->request->getIsPost()) {
if (!$conf) throw new \Exception('未知的项目,请确认:)');
$group = Group::find()
->where(['user_id' => $this->uid, 'project_id' => $projectId])
->count();
if (!$group) throw new \Exception('非该项目成员,无权限');

if ($task->load(\Yii::$app->request->post())) {
// 是否需要审核
$status = $conf->audit == Conf::AUDIT_YES ? Task::STATUS_SUBMIT : Task::STATUS_PASS;
$task->user_id = $this->uid;
$task->project_id = $projectId;
$task->status = $status;
if ($task->save()) {
$this->redirect('/task/');
}
}
}
if ($projectId) {
Expand All @@ -69,8 +77,10 @@ public function actionSubmit($projectId = null) {
'conf' => $conf,
]);
}
// 成员所属项目
$projects = Conf::find()
->where(['status' => Conf::STATUS_VALID])
->leftJoin(Group::tableName(), '`group`.project_id=conf.id')
->where(['conf.status' => Conf::STATUS_VALID, '`group`.user_id' => $this->uid])
->asArray()->all();
return $this->render('select-project', [
'projects' => $projects,
Expand All @@ -89,7 +99,7 @@ public function actionDelete($taskId) {
if (!$task) {
throw new \Exception('任务号不存在:)');
}
if ($task->user_id != \Yii::$app->user->id) {
if ($task->user_id != $this->uid) {
throw new \Exception('不可以操作其它人的任务:)');
}
if ($task->status == Task::STATUS_DONE) {
Expand All @@ -111,7 +121,7 @@ public function actionRollback($taskId) {
if (!$this->task) {
throw new \Exception('任务号不存在:)');
}
if ($this->task->user_id != \Yii::$app->user->id) {
if ($this->task->user_id != $this->uid) {
throw new \Exception('不可以操作其它人的任务:)');
}
if ($this->task->ex_link_id == $this->task->link_id) {
Expand All @@ -129,7 +139,7 @@ public function actionRollback($taskId) {

$rollbackTask = new Task();
$rollbackTask->attributes = [
'user_id' => \Yii::$app->user->id,
'user_id' => $this->uid,
'project_id' => $this->task->project_id,
'status' => $status,
'action' => Task::ACTION_ROLLBACK,
Expand Down
46 changes: 46 additions & 0 deletions controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace app\controllers;

use app\components\Controller;
use app\components\GlobalHelper;
use app\models\User;

class UserController extends Controller {

// 头像大小限制200k
const AVATAR_SIZE = 200000;

public function actionIndex() {
$user = User::findOne($this->uid);
return $this->render('index', [
'user' => $user,
]);
}

public function actionAvatar() {
$fileParts = pathinfo($_FILES['avatar']['name']);
if ($_FILES['avatar']['error']) {
$this->renderJson([], -1, '上传附件失败');
}
if ($_FILES['avatar']['size'] > static::AVATAR_SIZE) {
$this->renderJson([], -1, '文件过大');
}
if (!in_array(strtolower($fileParts['extension']), \Yii::$app->params['user.avatar.extension'])) {
$this->renderJson([], -1, '上传附件失败,附件格式只支持:' . join(', ', \Yii::$app->params['user.avatar.extension']));
}
$tempFile = $_FILES['avatar']['tmp_name'];
$baseName = sprintf('%s-%d.%s', date("YmdHis", time()), rand(10, 99), $fileParts['extension']);
$newFile = GlobalHelper::formatAvatar($baseName);
$targetFile = sprintf("%s/web/%s", rtrim(\Yii::$app->basePath, '/'), ltrim($newFile, '/'));
$ret = move_uploaded_file($tempFile, $targetFile);
if ($ret) {
$user = User::findOne($this->uid);
$user->avatar = $baseName;
$ret = $user->save();
}

$this->renderJson(['url' => $newFile], !$ret, $ret ?: '更新头像失败');
}

}
Loading

0 comments on commit c599d84

Please sign in to comment.