This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
ZfcUserZendDb.php
112 lines (94 loc) · 2.42 KB
/
ZfcUserZendDb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace BjyAuthorize\Provider\Identity;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Sql;
use ZfcUser\Service\User;
/**
* Identity provider based on {@see \Zend\Db\Adapter\Adapter}
*
* @author Ben Youngblood <bx.youngblood@gmail.com>
*/
class ZfcUserZendDb implements ProviderInterface
{
/**
* @var User
*/
protected $userService;
/**
* @var string|\Zend\Permissions\Acl\Role\RoleInterface
*/
protected $defaultRole;
/**
* @var string
*/
protected $tableName = 'user_role_linker';
/**
* @param Adapter $adapter
* @param User $userService
*/
public function __construct(Adapter $adapter, User $userService)
{
$this->adapter = $adapter;
$this->userService = $userService;
}
/**
* {@inheritDoc}
*/
public function getIdentityRoles()
{
$authService = $this->userService->getAuthService();
if (!$authService->hasIdentity()) {
// get default/guest role
return $this->getDefaultRole();
} else {
// get roles associated with the logged in user
$sql = new Sql($this->adapter);
$select = $sql->select()->from($this->tableName);
$where = new Where();
$where->equalTo('user_id', $authService->getIdentity()->getId());
$results = $sql->prepareStatementForSqlObject($select->where($where))->execute();
$roles = array();
foreach ($results as $i) {
$roles[] = $i['role_id'];
}
return $roles;
}
}
/**
* {@inheritDoc}
*/
public function getUserService()
{
return $this->userService;
}
/**
* {@inheritDoc}
*/
public function setUserService($userService)
{
$this->userService = $userService;
return $this;
}
/**
* {@inheritDoc}
*/
public function getDefaultRole()
{
return $this->defaultRole;
}
/**
* {@inheritDoc}
*/
public function setDefaultRole($defaultRole)
{
$this->defaultRole = $defaultRole;
return $this;
}
}