Skip to content

Commit

Permalink
Merge pull request #330 from orvice/dev
Browse files Browse the repository at this point in the history
add api
  • Loading branch information
orvice committed Feb 10, 2016
2 parents 694fad9 + e108c81 commit 7da1e45
Show file tree
Hide file tree
Showing 28 changed files with 580 additions and 50 deletions.
86 changes: 84 additions & 2 deletions app/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,96 @@
namespace App\Controllers;

use App\Models\InviteCode;
use App\Models\Node;

use App\Models\Node,App\Models\User;
use App\Services\Factory;
use App\Utils\Tools,App\Utils\Hash,App\Utils\Helper;
/**
* ApiController
*/

class ApiController extends BaseController
{

public function index(){

}

public function token($request, $response, $args){
$accessToken = $id = $args['token'];
$storage = Factory::createTokenStorage();
$token = $storage->get($accessToken);
if ($token==null){
$res['ret'] = 0;
$res['msg'] = "token is null";
return $this->echoJson($response,$res);
}
$res['ret'] = 1;
$res['msg'] = "ok";
$res['data'] = $token;
return $this->echoJson($response,$res);
}

public function newToken($request, $response, $args){
// $data = $request->post('sdf');
$email = $request->getParam('email');
$email = strtolower($email);
$passwd = $request->getParam('passwd');

// Handle Login
$user = User::where('email','=',$email)->first();

if ($user == null){
$res['ret'] = 0;
$res['msg'] = "401 邮箱或者密码错误";
return $this->echoJson($response,$res);
}

if (!Hash::checkPassword($user->pass,$passwd)){
$res['ret'] = 0;
$res['msg'] = "402 邮箱或者密码错误";
return $this->echoJson($response,$res);
}
$tokenStr = Tools::genToken();
$storage = Factory::createTokenStorage();
$expireTime = time() + 3600*24*7;
if($storage->store($tokenStr,$user,$expireTime)){
$res['ret'] = 1;
$res['msg'] = "ok";
$res['data']['token'] = $tokenStr;
$res['data']['user_id'] = $user->id;
return $this->echoJson($response,$res);
}
$res['ret'] = 0;
$res['msg'] = "system error";
return $this->echoJson($response,$res);
}

public function node($request, $response, $args){
$nodes = Node::where('type',1)->orderBy('sort')->get();
$res['ret'] = 1;
$res['msg'] = "ok";
$res['data'] = $nodes;
return $this->echoJson($response,$res);
}

public function userInfo($request, $response, $args){
$id = $args['id'];
$accessToken = Helper::getTokenFromReq($request);
$storage = Factory::createTokenStorage();
$token = $storage->get($accessToken);
if($id != $token->userId){
$res['ret'] = 0;
$res['msg'] = "access denied";
return $this->echoJson($response,$res);
}
$user = User::find($token->userId);
$user->pass = null;
$data = $user;
$res['ret'] = 1;
$res['msg'] = "ok";
$res['data'] = $data;
return $this->echoJson($response,$res);

}

}
6 changes: 3 additions & 3 deletions app/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function login()
return $this->view()->display('auth/login.tpl');
}

public function loginHandle($request, $response, $next)
public function loginHandle($request, $response, $args)
{
// $data = $request->post('sdf');
$email = $request->getParam('email');
Expand All @@ -42,7 +42,7 @@ public function loginHandle($request, $response, $next)
return $response->getBody()->write(json_encode($rs));
}

if ($user->pass != Hash::passwordHash($passwd)){
if (!Hash::checkPassword($user->pass,$passwd)){
$rs['ret'] = 0;
$rs['msg'] = "402 邮箱或者密码错误";
return $response->getBody()->write(json_encode($rs));
Expand All @@ -60,7 +60,7 @@ public function loginHandle($request, $response, $next)

public function register($request, $response, $next)
{
$ary = $request->getQueryParams();
$ary = $request->getQueryParams();
$code = "";
if(isset($ary['code'])){
$code = $ary['code'];
Expand Down
9 changes: 7 additions & 2 deletions app/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public function view(){
return $this->smarty();
}

public function echoJson(){

/**
* @param $response
* @param $res
* @return mixed
*/
public function echoJson($response,$res){
return $response->getBody()->write(json_encode($res));
}
}
31 changes: 24 additions & 7 deletions app/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\InviteCode;
use App\Services\Auth;
use App\Models\User;
use App\Models\Node;
use App\Services\Config;
use App\Utils\Hash;
Expand Down Expand Up @@ -86,12 +85,11 @@ public function doInvite($request, $response, $args){
$this->user->invite_num = 0;
$this->user->save();
$res['ret'] = 1;
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response,$res);
}

public function sys(){
return $this->view()->assign('ana',"")->display('user/sys.tpl');

}

public function updatePassword($request, $response, $args){
Expand Down Expand Up @@ -121,15 +119,15 @@ public function updatePassword($request, $response, $args){

$res['ret'] = 1;
$res['msg'] = "ok";
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response,$res);
}

public function updateSsPwd($request, $response, $args){
$user = Auth::getUser();
$pwd = $request->getParam('sspwd');
$user->updateSsPwd($pwd);
$res['ret'] = 1;
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response,$res);
}

public function updateMethod($request, $response, $args){
Expand All @@ -138,7 +136,7 @@ public function updateMethod($request, $response, $args){
$method = strtolower($method);
$user->updateMethod($method);
$res['ret'] = 1;
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response,$res);
}

public function logout($request, $response, $args){
Expand All @@ -159,8 +157,27 @@ public function doCheckIn($request, $response, $args){
$this->user->save();
$res['msg'] = sprintf("获得了 %u MB流量.",$traffic);
$res['ret'] = 1;
return $response->getBody()->write(json_encode($res));
return $this->echoJson($response,$res);
}

public function kill($request, $response, $args){
return $this->view()->display('user/kill.tpl');
}

public function handleKill($request, $response, $args){
$user = Auth::getUser();
$passwd = $request->getParam('passwd');
// check passwd
$res = array();
if (!Hash::checkPassword($user->pass,$passwd)){
$res['ret'] = 0;
$res['msg'] = " 密码错误";
return $this->echoJson($response,$res);
}
Auth::logout();
$user->delete();
$res['ret'] = 1;
$res['msg'] = "GG!您的帐号已经从我们的系统中删除.";
return $this->echoJson($response,$res);
}
}
31 changes: 23 additions & 8 deletions app/Middleware/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use App\Services\Auth as AuthService;
use App\Services\Factory;
use App\Utils\Helper;

class Api{
class Api
{

public function __invoke(ServerRequestInterface $request,ResponseInterface $response, $next)
{
//$response->getBody()->write('BEFORE');
$user = AuthService::getUser();
if(!$user->isLogin){
// @TODO no login action
$response->getBody()->write('Access Denied');
$accessToken = Helper::getTokenFromReq($request);
if ($accessToken==null){
$res['ret'] = 0;
$res['msg'] = "token is null";
$response->getBody()->write(json_encode($res));
return $response;
}
$storage = Factory::createTokenStorage();
$token = $storage->get($accessToken);
if ($token==null){
$res['ret'] = 0;
$res['msg'] = "token is null";
$response->getBody()->write(json_encode($res));
return $response;
}
if ($token->expireTime < time()){
$res['ret'] = 0;
$res['msg'] = "token is expire";
$response->getBody()->write(json_encode($res));
return $response;
}
$response = $next($request, $response);
//$response->getBody()->write('AFTER');
return $response;
}
}
9 changes: 9 additions & 0 deletions app/Models/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Models;


class Token extends Model
{
protected $table = "user_token";
}
18 changes: 2 additions & 16 deletions app/Services/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace App\Services;

use App\Services\Auth\Cookie;
use App\Services\Auth\Redis;
use App\Services\Auth\File;

class Auth
{
Expand All @@ -14,19 +11,8 @@ public function __construct(){

}

public static function getDriver(){

$method = Config::get('authDriver');

switch($method){
case 'cookie':
return new Cookie();
break;
case 'redis':
return new Redis();
break;
}
return new Redis();
private static function getDriver(){
return Factory::createAuth();
}

public static function login($uid,$time){
Expand Down
11 changes: 11 additions & 0 deletions app/Services/Auth/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Services\Auth;


abstract class Base
{
abstract public function login($uid,$time);
abstract public function logout();
abstract public function getUser();
}
3 changes: 2 additions & 1 deletion app/Services/Auth/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Utils\Hash;


class Cookie
class Cookie extends Base
{
public function login($uid,$time){
$user = User::find($uid);
Expand All @@ -30,6 +30,7 @@ public function getUser(){

$user = User::find($uid);
if ($user == null){
$user = new User();
$user->isLogin = false;
return $user;
}
Expand Down
37 changes: 37 additions & 0 deletions app/Services/Auth/JwtToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Services\Auth;

use App\Utils;
use App\Services\Jwt;

class JwtToken extends Base
{
public function login($uid, $time)
{
$expireTime = time() + $time;
$ary = [
"uid" => $uid,
"expire_time" => $expireTime
];
$decode = Jwt::encode($ary);
Utils\Cookie::set([
//"uid" => $uid,
"token" => $decode
],$expireTime);
}

public function logout()
{
Utils\Cookie::set([
//"uid" => $uid,
"token" => ""
],time()-3600);
}

public function getUser()
{
$token = Utils\Cookie::get('token');
$tokenInfo = Jwt::decodeArray($token);
}
}
3 changes: 2 additions & 1 deletion app/Services/Auth/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Utils\Tools;
use App\Utils\Cookie;

class Redis
class Redis extends Base
{
private $client;

Expand Down Expand Up @@ -46,6 +46,7 @@ public function getUser(){
$uid = $value;
$user = User::find($uid);
if($user == null ){
$user = new User();
$user->isLogin = false;
return $user;
}
Expand Down
Loading

0 comments on commit 7da1e45

Please sign in to comment.