Commit 90a403fc by 杨昕

修改用户登录接口

parent ab1749ea
Pipeline #10722 passed with stages
in 39 seconds
...@@ -41,3 +41,6 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" ...@@ -41,3 +41,6 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
QINIU_ACCESS_KEY=5ClMHcAd5KN6oN6SC7ni7uuwOOpaSvoDRMHR_3l8 QINIU_ACCESS_KEY=5ClMHcAd5KN6oN6SC7ni7uuwOOpaSvoDRMHR_3l8
QINIU_SECRET_KEY=xoJZ_3crOqMOsznLU8lbjlNU8pXJwEq5-n5vd3Fv QINIU_SECRET_KEY=xoJZ_3crOqMOsznLU8lbjlNU8pXJwEq5-n5vd3Fv
OFFCN_MEDIA_ACCESS_KEY=12485d7307bf347ee5834f98f5465aa4
OFFCN_MEDIA_SECRET_KEY=3d279dde16162fcd4e173707d129d036
MIS_SIN_KEY=ca780358d798c00dc18e148c9eaf618c
...@@ -326,7 +326,7 @@ function getRbac($request,$url='api/ram',$category_id = '' ){ ...@@ -326,7 +326,7 @@ function getRbac($request,$url='api/ram',$category_id = '' ){
} }
$mis_uid = $user['mis_uid']; $mis_uid = $user['mis_uid']??$request->mis_uid;
//https://api.eoffcn.com/demo/get?t=xx&sign=xx&ak=xx //https://api.eoffcn.com/demo/get?t=xx&sign=xx&ak=xx
......
...@@ -5,18 +5,17 @@ namespace App\Http\Controllers\Api\Client; ...@@ -5,18 +5,17 @@ namespace App\Http\Controllers\Api\Client;
use App\Exceptions\ControllerException; use App\Exceptions\ControllerException;
use App\Exceptions\DatabaseException; use App\Exceptions\DatabaseException;
use App\Model\LogModel; use App\Model\LogModel;
use App\Model\MediaCategoryModel;
use App\Model\MediaModel;
use App\Model\UserModel;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use MongoDB\BSON\UTCDateTime;
class UsersController extends Controller class UsersController extends Controller
{ {
private $baseAuth = [
'develop' => 'http://zgadmin.in.d.eoffcn.com',
'test' => 'http://zgadmin.d.eoffcn.com',
'master' => 'http://zgadmin.eoffcn.com',
];
/** /**
* 第三方验权 * 第三方验权
...@@ -44,113 +43,58 @@ class UsersController extends Controller ...@@ -44,113 +43,58 @@ class UsersController extends Controller
if (preg_match("['|\"|\/|\\\|\.|:]", $request->uuid)) { if (preg_match("['|\"|\/|\\\|\.|:]", $request->uuid)) {
throw new ControllerException('uuid含有非法字符',400); throw new ControllerException('uuid含有非法字符',400);
} }
$password = strtolower($request->password);
$username = $request->username;
/**
* 用户为offcn,不走第三方认证
*/
if ($username == 'offcn'){
$user = DB::connection('mongodb') //选择使用mongodb
->collection('users')->where('name',$username)->first();
if (empty($user)){
$data = [
'name' => $username,
'password' => md5($request->password??'123123'),
'organization_id' => 0,
'status' => 0,
'create_time' => date('Y-m-d H:i:s',time()),
'user_type' => 1
];
$id = DB::connection("mongodb")->collection("users")->insertGetId($data);
if (!$id){
$result = false;
}else{
$result = true;
}
}else{
if ($user['password'] == md5($password)){
$result = true;
}else{
$result = false;
}
}
}else{ return UserModel::checkUserRole($request);
});
$branch = empty(getenv('branch')) ? 'test' : getenv('branch'); return success($response);
$baseUrl = $this->baseAuth[$branch];
$url = $baseUrl."/api/user?action=check_user&user_name=".$username."&password=".$password; }
$result = http_request_code($url, /**
['Content-Type'=>'application/x-www-form-urlencoded'], * 获取用户业务信息
'POST', * @param $request
['username' => $username,'password' => $password], * @param $params
2000 * @return mixed
); * @throws ControllerException
$result = json_decode($result,true); */
if ($result['code']!=0){ private function getUserPrivilege($request,$params){
$result = false;
}
$request->replace(array_merge($request->all(),$params));
} $catgList = MediaCategoryModel::get()->toArray();
if ($result){ $tmp = [];
$data = [ foreach ($catgList as $key => $catg){
'name' => $username,
'password' => md5($password),
'organization_id' => 0,
'status' => 0,
'create_time' => date('Y-m-d H:i:s',time()),
];
$user = DB::connection('mongodb') //选择使用mongodb
->collection('users')->where('name',$username)->first();
$token = $this->generateToken();
if (!empty($user)){
$data['user_type'] = $user['user_type'];
$idArr = ($user['_id'])->jsonSerialize();
}else{
$data['user_type'] = 0;
$id = DB::connection("mongodb")->collection("users")->insertGetId($data);
if (!$id){
throw new DatabaseException('用户存储失败');
}
$idArr = ($id)->jsonSerialize();
}
$_id = $idArr['$oid']; $request->replace(array_merge($request->all(),['category_id'=>$catg['media_category_id']]));
$newtoken = encrypt($_id.'|'.$token."|1ZGHd3pCc87AsQFBLLJeMjM4t57cli6Ar78qowe7");
$data['token'] = $newtoken;
$flag = DB::connection('mongodb')->collection('users')->where('_id',$_id)->update($data); $url = getRbac($request,'api/open/check',$catg['media_category_id']);
if (!$flag){ $data['appid'] = $catg['media_category_id'];
throw new DatabaseException('用户名信息更新失败'); $data['uid'] = $request->mis_uid;
} $data['tree'] = 1;
return $newtoken; $result = http_request_code($url,
null,
'GET',
$data
);
}else{ $privilege = json_decode($result,true);
throw new DatabaseException('用户名称或者密码错误');
}
}); if (!empty($privilege['data'])){
array_push($tmp,$catg);
}
}
return success($response); if (empty($tmp)){
LogModel::addlog(["暂无业务权限,请联系管理员",$request->all()]);
throw new ControllerException('暂无业务权限,请联系管理员',403);
}
return $tmp;
} }
......
...@@ -15,6 +15,8 @@ use App\Http\Controllers\Controller; ...@@ -15,6 +15,8 @@ use App\Http\Controllers\Controller;
use App\Model\LogModel; use App\Model\LogModel;
use App\Model\MediaCategoryModel;
use App\Model\MediaModel;
use App\Model\UserModel; use App\Model\UserModel;
use App\User; use App\User;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
...@@ -34,81 +36,101 @@ class LoginController extends Controller ...@@ -34,81 +36,101 @@ class LoginController extends Controller
{ {
if (request()->isMethod('post')) { if (request()->isMethod('post')) {
$user = handler_drive(function () use($request){
if (!$request->password) {
throw new ControllerException(400, '口令不能为空');
}
if (!$request->name) {
throw new ControllerException(400, '昵称必须填写');
}
//$user = DB::connection('mongodb') //选择使用mongodb if (!$request->password) {
//->collection('users')->where('name',$request->name)->where("user_type",1)->first(); throw new ControllerException(400, '口令不能为空');
}
$user = UserModel::where([ if (!$request->username) {
'name' => $request->name, throw new ControllerException(400, '昵称必须填写');
'user_type' => 1, }
])->first();
/**
* 用户名称是有四部分组成,姓名-新账号-旧账号-mis_uid
*/
$user = UserModel::where('name', 'regexp', '/^.*?'.$request->username.'/')->first();
if (!empty($user)){
if (!empty($user)){
if ($user['password'] == md5($request->password)){ if (md5($request->password) != $user->password ){
$result = true; throw new \Exception('用户名或者密码有误');
}else{ }
$result = false;
}
if (!$result){ if ($user['mis_uid']){
return error('用户验证失败');
}
if ($user['status']){ $staffNo = $request->username;
return error('用户已被限制,请联系管理员'); $url = 'http://api-mis.offcn.com/index.php/applogin/isworking';
} $data = sign($staffNo);
$data['type'] = 6;
$data['username'] = $staffNo;
if ($request->name && $request->name != 'offcn' && !isset($user['mis_uid'])){ $result = http_request_code($url,
null,
'POST',
$data
);
$staffNo = $request->name; $userInfo = json_decode($result,true);
$url = 'http://api-mis.offcn.com/index.php/applogin/isworking';
$data = sign($staffNo);
$data['type'] = 6;
$data['username'] = $staffNo;
$result = http_request_code($url, if (!isset($userInfo['data'])){
null, LogModel::addlog(["获取用户信息失败",$request->all()]);
'POST', throw new \Exception("获取用户信息失败");
$data }
);
$userInfo = json_decode($result,true); $userid = $userInfo['data']['userid'];
$mis_uid = $userid;
if (!isset($userInfo['data'])){ }else{
LogModel::addlog(["获取用户信息失败",$request->all()]); $mis_uid = $user->mis_uid;
throw new DatabaseException("获取用户信息失败");
} }
$userid = $userInfo['data']['userid'];
$data = [
'mis_uid' => $userid
];
$flag = UserModel::where("name",$request->name)->update($data); $request->replace(array_merge($request->all(),['mis_uid' => $mis_uid]));
if (!$flag){
LogModel::addlog(['更新用户信息mis_uid失败'=>json_decode($result)]); $catgList = MediaCategoryModel::get()->toArray();
throw new \Exception("mis_uid更新失败");
$tmp = [];
foreach ($catgList as $key => $catg){
$request->replace(array_merge($request->all(),['category_id'=>$catg['media_category_id']]));
$url = getRbac($request,'api/open/check',$catg['media_category_id']);
$data['appid'] = $catg['media_category_id'];
$data['uid'] = $request->mis_uid;
$data['tree'] = 1;
$result = http_request_code($url,
null,
'GET',
$data
);
$privilege = json_decode($result,true);
if (!empty($privilege['data'])){
array_push($tmp,$catg);
}
}
if (empty($tmp)){
LogModel::addlog(["暂无业务权限,请联系管理员",$request->all()]);
throw new \Exception("暂无业务权限,请联系管理员");
} }
LogModel::addlog(['获取用户信息'=>json_decode($result)]); Auth::login($user);
return $user;
} }
Auth::login($user); throw new \Exception('用户验证失败');
return success($user); });
}
return error('用户验证失败'); return success($user);
} else { } else {
return view('admin.login'); return view('admin.login');
......
...@@ -418,9 +418,9 @@ class MediaModel extends Eloquent ...@@ -418,9 +418,9 @@ class MediaModel extends Eloquent
throw new \Exception('媒资不存在!'); throw new \Exception('媒资不存在!');
} }
if (getAdmin()->name != 'offcn'){ // if (getAdmin()->name != 'offcn'){
throw new \Exception("您无权限操作"); // throw new \Exception("您无权限操作");
} // }
$session = DB::connection('mongodb')->getMongoClient()->startSession(); $session = DB::connection('mongodb')->getMongoClient()->startSession();
...@@ -632,9 +632,11 @@ class MediaModel extends Eloquent ...@@ -632,9 +632,11 @@ class MediaModel extends Eloquent
//$aggregate[]['$match']['media_url'] = ['$regex' => 'qcoenfz67.bkt.clouddn.com' ]; //$aggregate[]['$match']['media_url'] = ['$regex' => 'qcoenfz67.bkt.clouddn.com' ];
if (isset($request->username) && !empty($request->username)){ // if (isset($request->username) && !empty($request->username)){
$aggregate[]['$match']['username'] = $request->username; // $aggregate[]['$match']['username'] = $request->username;
} // }
// $aggregate[]['$match']['media_name'] = "test3.mp3";
$aggregate[]['$sort'] = ['start_time' => $sort]; $aggregate[]['$sort'] = ['start_time' => $sort];
...@@ -674,41 +676,18 @@ class MediaModel extends Eloquent ...@@ -674,41 +676,18 @@ class MediaModel extends Eloquent
foreach ($medias as $media){ foreach ($medias as $media){
$start_time = $media['start_time'];
$end_time = $media['end_time'];
$create_time = $media['create_time'];
/** /**
* 构造开始时间,结束时间 * 构造开始时间,结束时间
*/ */
if (!is_object($media['start_time'])){ $tmp_medias = MediaModel::where("media_name",$media['media_name'])->count();
$start_time = strtotime($media['start_time']);
$start_time = new UTCDateTime($start_time * 1000);
}
if (!is_object($media['end_time'])){ if ($tmp_medias>1){
$end_time = strtotime($media['end_time']);
$end_time = new UTCDateTime($end_time * 1000);
}
if (!is_object($media['create_time'])){ MediaModel::deleteMediaById($request,$media['_id']);
$create_time = strtotime($media['create_time']);
$create_time = new UTCDateTime($create_time * 1000);
}
var_dump($tmp_medias);
$data = [
'start_time' => $start_time,
'end_time' => $end_time,
'create_time'=> $create_time,
];
$flag = MediaModel::where("_id",$media->_id)->update($data);
if ($flag==0){
throw new \Exception("更新失败");
} }
} }
} }
......
...@@ -6,6 +6,7 @@ use App\Exceptions\DatabaseException; ...@@ -6,6 +6,7 @@ use App\Exceptions\DatabaseException;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Auth\User as Authenticatable; use Jenssegers\Mongodb\Auth\User as Authenticatable;
use MongoDB\BSON\UTCDateTime;
class UserModel extends Authenticatable class UserModel extends Authenticatable
{ {
...@@ -202,5 +203,185 @@ class UserModel extends Authenticatable ...@@ -202,5 +203,185 @@ class UserModel extends Authenticatable
return $flag; return $flag;
} }
/**
* 检测用户权限信息
* @param $request
* @throws DatabaseException
*/
public static function checkUserRole($request){
try{
$password = strtolower($request->password);
$username = $request->username;
/**
* 陈腾飞-ctf37800-ctf88614-89043
* 用户名称是有四部分组成,姓名-新账号-旧账号-mis_uid
*/
$user = UserModel::where('name', 'regexp', '/^.*?'.$username.'/')->first();
if (empty($user)|| !$user['mis_uid']){
/**
* 获取mis用户基本信息
*/
$staffNo = $username;
$url = 'http://api-mis.offcn.com/index.php/applogin/isworking';
$data = sign($staffNo);
$data['type'] = 6;
$data['username'] = $staffNo;
$result = http_request_code($url,
null,
'POST',
$data
);
$userInfo = json_decode($result,true);
if (empty($userInfo['data'])){
LogModel::addlog(["获取用户信息失败",$request->all()]);
throw new \Exception("获取用户信息失败");
}
$userInfo = $userInfo['data'];
$result = self::getUserPrivilege($request,['mis_uid'=>$userInfo['userid']]);
$userid = $userInfo['userid'];
if (empty($user)){
//杨帆-yf18512-yf60144-8086
$data_arr = [
'name' => $userInfo['realname'].'-'.$userInfo['new_username'].'-'.$userInfo['username'].'-'.$userid,
'password' => md5($password),
'organization_id' => 0,
'status' => 0,
'mis_uid' => $userid,
'user_type' => 0,
'create_time' => new UTCDateTime(time() * 1000),
];
$_id = UserModel::insertGetId($data_arr);
if (!$_id){
LogModel::addlog(['添加用户失败'=>json_decode($result)]);
throw new \Exception("添加用户失败");
}
}else{
$_id = $user->_id;
$flag = UserModel::where("_id",$_id)->update(['mis_uid'=>$userid]);
if (!$flag){
LogModel::addlog(['更新用户mis_uid失败'=>json_encode(['mis_uid'=>$userid])]);
throw new \Exception("更新用户mis_uid失败");
}
}
}else{
$result = self::getUserPrivilege($request,['mis_uid'=>$user->mis_uid]);
if ($user['password'] != md5($password)){
throw new \Exception("用户名称或者密码错误");
}
if ($user['status']){
throw new \Exception("用户已被限制,请联系管理员");
}
$_id = $user->_id;
$userid = $user->mis_uid;
}
$token = generateRandomNum();
$tmp['token'] = $newtoken = encrypt($userid."|".$token."|1ZGHd3pCc87AsQFBLLJeMjM4t57cli6Ar78qowe7");
$flag = DB::connection('mongodb')->collection('users')->where('_id',$_id)->update($tmp);
if (!$flag){
throw new DatabaseException('用户名信息更新失败');
}
$tmp['categories'] = $result;
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
}
return $tmp;
}
/**
* 获取用户业务信息
* @param $request
* @param $params
* @return array
* @throws DatabaseException
*/
private static function getUserPrivilege($request,$params){
try{
$request->replace(array_merge($request->all(),$params));
$catgList = MediaCategoryModel::get()->toArray();
$tmp = [];
foreach ($catgList as $key => $catg){
$request->replace(array_merge($request->all(),['category_id'=>$catg['media_category_id']]));
$url = getRbac($request,'api/open/check',$catg['media_category_id']);
$data['appid'] = $catg['media_category_id'];
$data['uid'] = $request->mis_uid;
$data['tree'] = 1;
$result = http_request_code($url,
null,
'GET',
$data
);
$privilege = json_decode($result,true);
if (!empty($privilege['data'])){
array_push($tmp,$catg);
}
}
if (empty($tmp)){
LogModel::addlog(["暂无业务权限,请联系管理员",$request->all()]);
throw new \Exception('暂无业务权限,请联系管理员',403);
}
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
}
return $tmp;
}
} }
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
<input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group"> <div class="form-group">
<label for="username">用户名</label> <label for="username">用户名</label>
<input type="text" class="form-control" id="username" name="name" value="" <input type="text" class="form-control" id="username" name="username" value=""
placeholder="用户名" autocomplete="off"> placeholder="用户名" autocomplete="off">
</div> </div>
<div class="form-group"> <div class="form-group">
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
$("#defaultForm").validate({ $("#defaultForm").validate({
rules: { rules: {
name: { username: {
required: true, required: true,
// minlength: 2 // minlength: 2
}, },
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
}, },
messages: { messages: {
name: { username: {
required: "用户名不能为空", required: "用户名不能为空",
// minlength: "Your username must consist of at least 2 characters" // minlength: "Your username must consist of at least 2 characters"
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment