1: <?php
2:
3: namespace PHPixie\Auth\Domains;
4:
5: class Domain
6: {
7: protected $builder;
8: protected $name;
9: protected $configData;
10:
11: protected $repository;
12: protected $providers = null;
13:
14: public function __construct($builder, $name, $configData)
15: {
16: $this->builder = $builder;
17: $this->name = $name;
18: $this->configData = $configData;
19: }
20:
21: public function repository()
22: {
23: if($this->repository === null) {
24: $repositoryName = $this->configData->getRequired('repository');
25: $repositories = $this->builder->repositories();
26: $this->repository = $repositories->get($repositoryName);
27: }
28:
29: return $this->repository;
30: }
31:
32: public function provider($name)
33: {
34: $this->requireProviders();
35: return $this->providers[$name];
36: }
37:
38: public function providers()
39: {
40: $this->requireProviders();
41: return $this->providers;
42: }
43:
44: public function checkUser()
45: {
46: $this->unsetUser();
47:
48: $user = null;
49: foreach($this->providers() as $provider) {
50: if($provider instanceof \PHPixie\Auth\Providers\Provider\Autologin) {
51: $user = $provider->check();
52: if($user !== null) {
53: break;
54: }
55: }
56: }
57:
58: return $user;
59: }
60:
61: public function forgetUser()
62: {
63: $this->unsetUser();
64:
65: foreach($this->providers() as $provider) {
66: if($provider instanceof \PHPixie\Auth\Providers\Provider\Persistent) {
67: $provider->forget();
68: }
69: }
70: }
71:
72: public function unsetUser()
73: {
74: $this->context()->unsetUser($this->name);
75: }
76:
77: public function setUser($user, $providerName)
78: {
79: $this->context()->setUser($user, $this->name, $providerName);
80: }
81:
82: public function user()
83: {
84: return $this->context()->user($this->name);
85: }
86:
87: public function requireUser()
88: {
89: $user = $this->user();
90: if($user === null) {
91: throw new \PHPixie\Auth\Exception("No user set");
92: }
93:
94: return $user;
95: }
96:
97: public function name()
98: {
99: return $this->name;
100: }
101:
102: protected function context()
103: {
104: $contextContainer = $this->builder->contextContainer();
105: return $contextContainer->authContext();
106: }
107:
108: protected function requireProviders()
109: {
110: if($this->providers !== null) {
111: return;
112: }
113:
114: $providerBuilder = $this->builder->providers();
115:
116: $providers = array();
117: $providersConfig = $this->configData->slice('providers');
118: foreach($providersConfig->keys() as $name) {
119: $providerConfig = $providersConfig->slice($name);
120: $providers[$name] = $providerBuilder->buildFromConfig(
121: $this,
122: $name,
123: $providerConfig
124: );
125: }
126:
127: $this->providers = $providers;
128: }
129: }