1: <?php
2: /**
3: * Authentification provisionning class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @subpackage auth
16: * @since 2.0
17: * @author Pierre-Eric MENUET <pemphp@free.fr>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: /**
22: *
23: * @package kernel
24: * @subpackage auth
25: * @description Authentification provisionning class. This class is responsible to
26: * provide synchronisation method to Xoops User Database
27: * @author Pierre-Eric MENUET <pemphp@free.fr>
28: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
29: */
30: class XoopsAuthProvisionning
31: {
32: protected $_auth_instance;
33:
34: /**
35: * XoopsAuthProvisionning::getInstance()
36: *
37: * @param mixed $auth_instance
38: *
39: * @return \XoopsAuthProvisionning
40: */
41: public static function getInstance(XoopsAuth $auth_instance = null)
42: {
43: static $provis_instance;
44: if (!isset($provis_instance)) {
45: $provis_instance = new XoopsAuthProvisionning($auth_instance);
46: }
47:
48: return $provis_instance;
49: }
50:
51: /**
52: * Authentication Service constructor
53: * @param XoopsAuth $auth_instance
54: */
55: public function __construct(XoopsAuth $auth_instance = null)
56: {
57: $this->_auth_instance = $auth_instance;
58: /** @var XoopsConfigHandler $config_handler */
59: $config_handler = xoops_getHandler('config');
60: $config = $config_handler->getConfigsByCat(XOOPS_CONF_AUTH);
61: foreach ($config as $key => $val) {
62: $this->$key = $val;
63: }
64: $config_gen = $config_handler->getConfigsByCat(XOOPS_CONF);
65: $this->default_TZ = $config_gen['default_TZ'];
66: $this->theme_set = $config_gen['theme_set'];
67: $this->com_mode = $config_gen['com_mode'];
68: $this->com_order = $config_gen['com_order'];
69: }
70:
71: /**
72: * Return a Xoops User Object
73: *
74: * @param $uname
75: * @return XoopsUser or false
76: */
77: public function getXoopsUser($uname)
78: {
79: /** @var XoopsMemberHandler $member_handler */
80: $member_handler = xoops_getHandler('member');
81: $criteria = new Criteria('uname', $uname);
82: $getuser = $member_handler->getUsers($criteria);
83: if (count($getuser) == 1) {
84: return $getuser[0];
85: } else {
86: return false;
87: }
88: }
89:
90: /**
91: * Launch the synchronisation process
92: *
93: * @param $datas
94: * @param $uname
95: * @param null $pwd
96: * @return bool
97: */
98: public function sync($datas, $uname, $pwd = null)
99: {
100: $xoopsUser = $this->getXoopsUser($uname);
101: if (!$xoopsUser) { // Xoops User Database not exists
102: if ($this->ldap_provisionning) {
103: $xoopsUser = $this->add($datas, $uname, $pwd);
104: } else {
105: $this->_auth_instance->setErrors(0, sprintf(_AUTH_LDAP_XOOPS_USER_NOTFOUND, $uname));
106: }
107: } else { // Xoops User Database exists
108: if ($this->ldap_provisionning && $this->ldap_provisionning_upd) {
109: $xoopsUser = $this->change($xoopsUser, $datas, $uname, $pwd);
110: }
111: }
112:
113: return $xoopsUser;
114: }
115:
116: /**
117: * Add a new user to the system
118: *
119: * @param $datas
120: * @param $uname
121: * @param null $pwd
122: * @return bool
123: */
124: public function add($datas, $uname, $pwd = null)
125: {
126: $ret = false;
127: /** @var XoopsMemberHandler $member_handler */
128: $member_handler = xoops_getHandler('member');
129: // Create XOOPS Database User
130: $newuser = $member_handler->createUser();
131: $newuser->setVar('uname', $uname);
132: $newuser->setVar('pass', password_hash(stripslashes($pwd), PASSWORD_DEFAULT));
133: $newuser->setVar('rank', 0);
134: $newuser->setVar('level', 1);
135: $newuser->setVar('timezone_offset', $this->default_TZ);
136: $newuser->setVar('theme', $this->theme_set);
137: $newuser->setVar('umode', $this->com_mode);
138: $newuser->setVar('uorder', $this->com_order);
139: $tab_mapping = explode('|', $this->ldap_field_mapping);
140: foreach ($tab_mapping as $mapping) {
141: $fields = explode('=', trim($mapping));
142: if ($fields[0] && $fields[1]) {
143: $newuser->setVar(trim($fields[0]), xoops_utf8_decode($datas[trim($fields[1])][0]));
144: }
145: }
146: if ($member_handler->insertUser($newuser)) {
147: foreach ($this->ldap_provisionning_group as $groupid) {
148: $member_handler->addUserToGroup($groupid, $newuser->getVar('uid'));
149: }
150: $newuser->unsetNew();
151:
152: return $newuser;
153: } else {
154: redirect_header(XOOPS_URL . '/user.php', 5, $newuser->getHtmlErrors());
155: }
156:
157: return $ret;
158: }
159:
160: /**
161: * Modify user information
162: *
163: * @param $xoopsUser
164: * @param $datas
165: * @param $uname
166: * @param null $pwd
167: * @return bool
168: */
169: public function change(&$xoopsUser, $datas, $uname, $pwd = null)
170: {
171: $ret = false;
172: /** @var XoopsMemberHandler $member_handler */
173: $member_handler = xoops_getHandler('member');
174: $xoopsUser->setVar('pass', password_hash(stripcslashes($pwd), PASSWORD_DEFAULT));
175: $tab_mapping = explode('|', $this->ldap_field_mapping);
176: foreach ($tab_mapping as $mapping) {
177: $fields = explode('=', trim($mapping));
178: if ($fields[0] && $fields[1]) {
179: $xoopsUser->setVar(trim($fields[0]), xoops_utf8_decode($datas[trim($fields[1])][0]));
180: }
181: }
182: if ($member_handler->insertUser($xoopsUser)) {
183: return $xoopsUser;
184: } else {
185: redirect_header(XOOPS_URL . '/user.php', 5, $xoopsUser->getHtmlErrors());
186: }
187:
188: return $ret;
189: }
190:
191: /**
192: * Modify a user
193: *
194: * @return bool
195: */
196: public function delete()
197: {
198: }
199:
200: /**
201: * Suspend a user
202: *
203: * @return bool
204: */
205: public function suspend()
206: {
207: }
208:
209: /**
210: * Restore a user
211: *
212: * @return bool
213: */
214: public function restore()
215: {
216: }
217:
218: /**
219: * Add a new user to the system
220: *
221: * @return bool
222: */
223: public function resetpwd()
224: {
225: }
226: } // end class
227:
228: