Skip to content

Commit 877b03c

Browse files
committed
update
1 parent 0ffe536 commit 877b03c

14 files changed

Lines changed: 355 additions & 87 deletions

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
}
1010
],
1111
"minimum-stability": "dev",
12-
"require": {},
12+
"require": {
13+
"xiaodi-dev/think-jwt": "v3.0.0.beta.6"
14+
},
1315
"autoload": {
1416
"psr-4": {
1517
"think\\User\\": "src/"

example/User.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
include __DIR__ . '/../vendor/autoload.php';
3+
4+
use think\User\AuthorizationUserInterface;
5+
6+
class User implements AuthorizationUserInterface
7+
{
8+
public function hasUserByUserName($username): bool
9+
{
10+
return false;
11+
}
12+
13+
public function getUserByUserName($username): AuthorizationUserInterface
14+
{
15+
return $this;
16+
}
17+
18+
public function verifyPassword($password): bool
19+
{
20+
return false;
21+
}
22+
23+
public function setUserName($username): AuthorizationUserInterface
24+
{
25+
$this->username = $username;
26+
return $this;
27+
}
28+
29+
public function setPassword($password): AuthorizationUserInterface
30+
{
31+
return $this;
32+
}
33+
34+
public function token(): string
35+
{
36+
return '';
37+
}
38+
39+
public function find()
40+
{
41+
return $this;
42+
}
43+
}

example/test.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
include __DIR__ . '/User.php';
5+
6+
use think\User\Config;
7+
use think\User\Auth;
8+
use think\User\Drive\Jwt;
9+
use think\User\Drive\Session;
10+
use think\User\Drive\Cookie;
11+
12+
$options = [
13+
'drive' => Session::class,
14+
'key' => 'uid',
15+
'model' => User::class
16+
];
17+
18+
$config = new Config($options);
19+
20+
$auth = new Auth($config);
21+
22+
var_dump($auth->isLogin());
23+
$auth->user();

src/Auth.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace think\User;
4+
5+
use think\User\Drive\DriveManager;
6+
use think\User\Config;
7+
8+
class Auth
9+
{
10+
/**
11+
* @var Config
12+
*/
13+
protected $config;
14+
15+
/**
16+
* @var DriveManager
17+
*/
18+
protected $manager;
19+
20+
public function __construct(Config $config)
21+
{
22+
$this->config = $config;
23+
24+
$this->init();
25+
}
26+
27+
protected function init()
28+
{
29+
$this->manager = $this->getManager();
30+
}
31+
32+
protected function getConfig()
33+
{
34+
return $this->config;
35+
}
36+
37+
protected function getManager()
38+
{
39+
return new DriveManager($this->config);
40+
}
41+
42+
public function __call($name, $argv)
43+
{
44+
return $this->manager->$name(...$argv);
45+
}
46+
}

src/AuthService.php

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,18 @@
11
<?php
22

3-
/*
4-
* This file is part of TAnt.
5-
* @link https://github.com/edenleung/think-admin
6-
* @document https://www.kancloud.cn/manual/thinkphp6_0
7-
* @contact QQ Group 996887666
8-
* @author Eden Leung [email protected]
9-
* @copyright 2019 Eden Leung
10-
* @license https://github.com/edenleung/think-admin/blob/6.0/LICENSE.txt
11-
*/
12-
133
namespace think\User;
144

15-
use think\User\Exception\Unauthorized;
5+
use think\Service;
166

17-
class AuthService
7+
class AuthService extends Service
188
{
19-
/**
20-
* @var AuthorizationUserInterface
21-
*/
22-
protected $member;
23-
24-
public function __construct(AuthorizationUserInterface $member)
25-
{
26-
$this->member = $member;
27-
}
28-
29-
public function login(string $username, string $password)
30-
{
31-
$row = $this->member->hasUserByUserName($username);
32-
33-
if ($row) {
34-
$user = $this->member->getUserByUserName($username);
35-
if (!$user->verifyPassword($password)) {
36-
throw new Unauthorized('账号密码错误');
37-
}
38-
39-
return $user;
40-
}
41-
42-
throw new Unauthorized('没有此账号');
43-
}
44-
45-
public function register($username, $password, $params = [])
9+
public function register()
4610
{
47-
$row = $this->member->hasUserByUserName($username);
48-
49-
if (!$row) {
50-
$member = $this->member->setUserName($username)->setPassword($password);
51-
52-
foreach ($params as $key => $value) {
53-
$member->$key = $value;
54-
}
55-
56-
$member->save($params);
57-
}
11+
$this->app->bind('auth', function () {
12+
$config = new Config(config('auth'));
13+
$auth = new Auth($config);
5814

59-
throw new Unauthorized('此账号已注册');
15+
return $auth;
16+
});
6017
}
6118
}

src/AuthorizationUserInterface.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
<?php
22

3-
/*
4-
* This file is part of TAnt.
5-
* @link https://github.com/edenleung/think-admin
6-
* @document https://www.kancloud.cn/manual/thinkphp6_0
7-
* @contact QQ Group 996887666
8-
* @author Eden Leung [email protected]
9-
* @copyright 2019 Eden Leung
10-
* @license https://github.com/edenleung/think-admin/blob/6.0/LICENSE.txt
11-
*/
12-
133
namespace think\User;
144

155
interface AuthorizationUserInterface
166
{
7+
public function getUserById($id): AuthorizationUserInterface;
8+
179
public function hasUserByUserName($username): bool;
1810

1911
public function getUserByUserName($username): AuthorizationUserInterface;

src/Config.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace think\User;
4+
5+
class Config
6+
{
7+
protected $drive;
8+
9+
protected $key;
10+
11+
protected $model;
12+
13+
public function __construct($options)
14+
{
15+
foreach ($options as $key => $value) {
16+
$this->$key = $value;
17+
}
18+
}
19+
20+
public function setModel($model)
21+
{
22+
$this->model = $model;
23+
return $this;
24+
}
25+
26+
public function getModel()
27+
{
28+
return $this->model;
29+
}
30+
31+
public function setDrive($name)
32+
{
33+
$this->drive = $name;
34+
return $this;
35+
}
36+
37+
public function getDrive()
38+
{
39+
return $this->drive;
40+
}
41+
42+
public function setKey($key)
43+
{
44+
$this->key = $key;
45+
return $this;
46+
}
47+
48+
public function getKey()
49+
{
50+
return $this->key;
51+
}
52+
}

src/Drive/Cookie.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace think\User\Drive;
4+
5+
use think\User\Drive\DriveInterface;
6+
7+
class Cookie implements DriveInterface
8+
{
9+
public function has($key)
10+
{
11+
return \think\facade\Cookie::has($key);
12+
}
13+
14+
public function get($key)
15+
{
16+
return \think\facade\Cookie::get($key);
17+
}
18+
19+
public function delete($key)
20+
{
21+
}
22+
}

src/Drive/DriveInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace think\User\Drive;
4+
5+
interface DriveInterface
6+
{
7+
public function get($key);
8+
9+
public function has($key);
10+
11+
public function delete($key);
12+
}

src/Drive/DriveManager.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace think\User\Drive;
4+
5+
use think\User\AuthorizationUserInterface;
6+
use think\User\Config;
7+
use think\User\Drive\DriveInterface;
8+
use think\User\Exception\Unauthorized;
9+
10+
class DriveManager
11+
{
12+
/**
13+
* @var Config
14+
*/
15+
protected $config;
16+
17+
/**
18+
* @var DriveInterface
19+
*/
20+
protected $drive;
21+
22+
public function __construct(Config $config)
23+
{
24+
$this->config = $config;
25+
26+
$this->init();
27+
}
28+
29+
protected function init()
30+
{
31+
$class = $this->config->getDrive();
32+
$this->drive = new $class();
33+
}
34+
35+
public function getDrive()
36+
{
37+
return $this->drive;
38+
}
39+
40+
public function isLogin()
41+
{
42+
$key = $this->config->getKey();
43+
44+
return $this->drive->has($key);
45+
}
46+
47+
protected function makeUser()
48+
{
49+
$class = $this->config->getModel();
50+
$key = $this->config->getKey();
51+
$id = $this->drive->get($key);
52+
53+
$model = new $class();
54+
if ($model instanceof AuthorizationUserInterface) {
55+
return $model->find($id);
56+
} else {
57+
throw new Unauthorized('implements ' . AuthorizationUserInterface::class);
58+
}
59+
}
60+
61+
public function user()
62+
{
63+
if ($this->isLogin() !== false) {
64+
return $this->makeUser();
65+
} else {
66+
throw new Unauthorized('未登录');
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)