PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/concrete/core/controllers/single_pages/dashboard/users/search.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 413 lines | 328 code | 70 blank | 15 comment | 96 complexity | 1360d3fdcaf4cb49af7751e6d57d3a3b MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. class Concrete5_Controller_Dashboard_Users_Search extends Controller {
  4. public function on_start(){
  5. $this->error = Loader::helper('validation/error');
  6. }
  7. public function view() {
  8. // this is hacky as hell, we need to make this page MVC
  9. if ($_REQUEST['task'] != 'edit' && !$_REQUEST['uID']) {
  10. $this->addHeaderItem('<script type="text/javascript">$(function() { ccm_setupAdvancedSearch(\'user\'); });</script>');
  11. $userList = $this->getRequestedSearchResults();
  12. $users = $userList->getPage();
  13. $this->set('userList', $userList);
  14. $this->set('users', $users);
  15. $this->set('pagination', $userList->getPagination());
  16. }
  17. $form = Loader::helper('form');
  18. $this->set('form', $form);
  19. if($_POST['edit']) {
  20. $this->validate_user();
  21. }
  22. if ($_REQUEST['deactivated']) {
  23. $this->set('message', t('User deactivated.'));
  24. }
  25. if ($_REQUEST['activated']) {
  26. $this->set('message', t('User activated.'));
  27. }
  28. if ($_REQUEST['validated']) {
  29. $this->set('message', t('Email marked as valid.'));
  30. }
  31. if ($_REQUEST['user_created']) {
  32. $this->set('message', t('User created.'));
  33. }
  34. }
  35. public function validate_user() {
  36. $pke = PermissionKey::getByHandle('edit_user_properties');
  37. if (!$pke->validate()) {
  38. return false;
  39. }
  40. $assignment = $pke->getMyAssignment();
  41. $vals = Loader::helper('validation/strings');
  42. $valt = Loader::helper('validation/token');
  43. $valc = Loader::helper('concrete/validation');
  44. $uo = UserInfo::getByID(intval($_GET['uID']));
  45. $username = trim($_POST['uName']);
  46. $username = preg_replace("/\s+/", " ", $username);
  47. if ($assignment->allowEditPassword()) {
  48. $password = $_POST['uPassword'];
  49. $passwordConfirm = $_POST['uPasswordConfirm'];
  50. if ($password) {
  51. if ((strlen($password) < USER_PASSWORD_MINIMUM) || (strlen($password) > USER_PASSWORD_MAXIMUM)) {
  52. $this->error->add( t('A password must be between %s and %s characters',USER_PASSWORD_MINIMUM,USER_PASSWORD_MAXIMUM));
  53. }
  54. }
  55. }
  56. if ($assignment->allowEditEmail()) {
  57. if (!$vals->email($_POST['uEmail'])) {
  58. $this->error->add(t('Invalid email address provided.'));
  59. } else if (!$valc->isUniqueEmail($_POST['uEmail']) && $uo->getUserEmail() != $_POST['uEmail']) {
  60. $this->error->add(t("The email address '%s' is already in use. Please choose another.",$_POST['uEmail']));
  61. }
  62. }
  63. if ($assignment->allowEditUserName()) {
  64. $_POST['uName'] = $username;
  65. if (USER_REGISTRATION_WITH_EMAIL_ADDRESS == false) {
  66. if (strlen($username) < USER_USERNAME_MINIMUM) {
  67. $this->error->add(t('A username must be at least %s characters long.',USER_USERNAME_MINIMUM));
  68. }
  69. if (strlen($username) > USER_USERNAME_MAXIMUM) {
  70. $this->error->add(t('A username cannot be more than %s characters long.',USER_USERNAME_MAXIMUM));
  71. }
  72. /*
  73. if (strlen($username) >= USER_USERNAME_MINIMUM && !$vals->alphanum($username,USER_USERNAME_ALLOW_SPACES)) {
  74. if(USER_USERNAME_ALLOW_SPACES) {
  75. $e->add(t('A username may only contain letters, numbers and spaces.'));
  76. } else {
  77. $e->add(t('A username may only contain letters or numbers.'));
  78. }
  79. }
  80. */
  81. if (strlen($username) >= USER_USERNAME_MINIMUM && !$valc->username($username)) {
  82. if(USER_USERNAME_ALLOW_SPACES) {
  83. $this->error->add(t('A username may only contain letters, numbers and spaces.'));
  84. } else {
  85. $this->error->add(t('A username may only contain letters or numbers.'));
  86. }
  87. }
  88. if (!$valc->isUniqueUsername($username) && $uo->getUserName() != $username) {
  89. $this->error->add(t("The username '%s' already exists. Please choose another",$username));
  90. }
  91. }
  92. }
  93. if ($assignment->allowEditPassword()) {
  94. if (strlen($password) >= USER_PASSWORD_MINIMUM && !$valc->password($password)) {
  95. $this->error->add(t('A password may not contain ", \', >, <, or any spaces.'));
  96. }
  97. if ($password) {
  98. if ($password != $passwordConfirm) {
  99. $this->error->add(t('The two passwords provided do not match.'));
  100. }
  101. }
  102. }
  103. if (!$valt->validate('update_account_' . intval($_GET['uID']) )) {
  104. $this->error->add($valt->getErrorMessage());
  105. }
  106. if (!$this->error->has()) {
  107. // do the registration
  108. $data = array();
  109. if ($assignment->allowEditUserName()) {
  110. $data['uName'] = $_POST['uName'];
  111. }
  112. if ($assignment->allowEditEmail()) {
  113. $data['uEmail'] = $_POST['uEmail'];
  114. }
  115. if ($assignment->allowEditPassword()) {
  116. $data['uPassword'] = $_POST['uPassword'];
  117. $data['uPasswordConfirm'] = $_POST['uPasswordConfirm'];
  118. }
  119. if ($assignment->allowEditTimezone()) {
  120. $data['uTimezone'] = $_POST['uTimezone'];
  121. }
  122. if ($assignment->allowEditDefaultLanguage()) {
  123. $data['uDefaultLanguage'] = $_POST['uDefaultLanguage'];
  124. }
  125. $process = $uo->update($data);
  126. //$db = Loader::db();
  127. if ($process) {
  128. if ($assignment->allowEditAvatar()) {
  129. $av = Loader::helper('concrete/avatar');
  130. if ( is_uploaded_file($_FILES['uAvatar']['tmp_name']) ) {
  131. $uHasAvatar = $av->updateUserAvatar($_FILES['uAvatar']['tmp_name'], $uo->getUserID());
  132. }
  133. }
  134. $gak = PermissionKey::getByHandle('assign_user_groups');
  135. $gIDs = array();
  136. if (is_array($_POST['gID'])) {
  137. foreach($_POST['gID'] as $gID) {
  138. if ($gak->validate($gID)) {
  139. $gIDs[] = intval($gID);
  140. }
  141. }
  142. }
  143. $gIDs = array_unique($gIDs);
  144. $uo->updateGroups($gIDs);
  145. $message = t("User updated successfully. ");
  146. if ($password) {
  147. $message .= t("Password changed.");
  148. }
  149. $editComplete = true;
  150. // reload user object
  151. $uo = UserInfo::getByID(intval($_GET['uID']));
  152. $this->set('message', $message);
  153. } else {
  154. $db = Loader::db();
  155. $this->error->add($db->ErrorMsg());
  156. $this->set('error',$this->error);
  157. }
  158. }else{
  159. $this->set('error',$this->error);
  160. }
  161. }
  162. public function getRequestedSearchResults() {
  163. $userList = new UserList();
  164. $userList->sortBy('uDateAdded', 'desc');
  165. $userList->showInactiveUsers = true;
  166. $userList->showInvalidatedUsers = true;
  167. $columns = UserSearchColumnSet::getCurrent();
  168. $this->set('columns', $columns);
  169. if ($_GET['keywords'] != '') {
  170. $userList->filterByKeywords($_GET['keywords']);
  171. }
  172. if ($_REQUEST['numResults'] && Loader::helper('validation/numbers')->integer($_REQUEST['numResults'])) {
  173. $userList->setItemsPerPage($_REQUEST['numResults']);
  174. }
  175. $pk = PermissionKey::getByHandle('access_user_search');
  176. $asl = $pk->getMyAssignment();
  177. $p = new Permissions();
  178. $filterGIDs = array();
  179. if ($asl->getGroupsAllowedPermission() == 'C') {
  180. $userList->filter('u.uID', USER_SUPER_ID, '<>');
  181. $userList->addToQuery("left join UserGroups ugRequired on ugRequired.uID = u.uID ");
  182. if (in_array(REGISTERED_GROUP_ID, $asl->getGroupsAllowedArray())) {
  183. $userList->filter(false, '(ugRequired.gID in (' . implode(',', $asl->getGroupsAllowedArray()) . ') or ugRequired.gID is null)');
  184. } else {
  185. $userList->filter('ugRequired.gID', $asl->getGroupsAllowedArray(), 'in');
  186. }
  187. }
  188. if (isset($_REQUEST['gID']) && is_array($_REQUEST['gID'])) {
  189. foreach($_REQUEST['gID'] as $gID) {
  190. $g = Group::getByID($gID);
  191. if (is_object($g)) {
  192. if ($pk->validate($g) && (!in_array($g->getGroupID(), $filterGIDs))) {
  193. $filterGIDs[] = $g->getGroupID();
  194. }
  195. }
  196. }
  197. }
  198. foreach($filterGIDs as $gID) {
  199. $userList->filterByGroupID($gID);
  200. }
  201. if (is_array($_REQUEST['selectedSearchField'])) {
  202. foreach($_REQUEST['selectedSearchField'] as $i => $item) {
  203. // due to the way the form is setup, index will always be one more than the arrays
  204. if ($item != '') {
  205. switch($item) {
  206. case 'is_active':
  207. if ($_GET['active'] === '0') {
  208. $userList->filterByIsActive(0);
  209. } else if ($_GET['active'] === '1') {
  210. $userList->filterByIsActive(1);
  211. }
  212. break;
  213. case "date_added":
  214. $dateFrom = $_REQUEST['date_from'];
  215. $dateTo = $_REQUEST['date_to'];
  216. if ($dateFrom != '') {
  217. $dateFrom = date('Y-m-d', strtotime($dateFrom));
  218. $userList->filterByDateAdded($dateFrom, '>=');
  219. $dateFrom .= ' 00:00:00';
  220. }
  221. if ($dateTo != '') {
  222. $dateTo = date('Y-m-d', strtotime($dateTo));
  223. $dateTo .= ' 23:59:59';
  224. $userList->filterByDateAdded($dateTo, '<=');
  225. }
  226. break;
  227. case "group_set":
  228. $gsID = $_REQUEST['gsID'];
  229. $gs = GroupSet::getByID($gsID);
  230. $groupsetids = array(-1);
  231. if (is_object($gs)) {
  232. $groups = $gs->getGroups();
  233. }
  234. $userList->addToQuery('left join UserGroups ugs on u.uID = ugs.uID');
  235. foreach($groups as $g) {
  236. if ($pk->validate($g) && (!in_array($g->getGroupID(), $groupsetids))) {
  237. $groupsetids[] = $g->getGroupID();
  238. }
  239. }
  240. $instr = 'ugs.gID in (' . implode(',', $groupsetids) . ')';
  241. $userList->filter(false, $instr);
  242. break;
  243. default:
  244. $akID = $item;
  245. $fak = UserAttributeKey::get($akID);
  246. $type = $fak->getAttributeType();
  247. $cnt = $type->getController();
  248. $cnt->setAttributeKey($fak);
  249. $cnt->searchForm($userList);
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. return $userList;
  256. }
  257. public function sign_in_as_user($uID, $token = null) {
  258. try {
  259. $u = new User();
  260. $tp = new TaskPermission();
  261. if (!$tp->canSudo()) {
  262. throw new Exception(t('You do not have permission to perform this action.'));
  263. }
  264. $ui = UserInfo::getByID($uID);
  265. if(!($ui instanceof UserInfo)) {
  266. throw new Exception(t('Invalid user ID.'));
  267. }
  268. $pk = PermissionKey::getByHandle('access_user_search');
  269. if ($pk->validate($ui)) {
  270. $valt = Loader::helper('validation/token');
  271. if (!$valt->validate('sudo', $token)) {
  272. throw new Exception($valt->getErrorMessage());
  273. }
  274. User::loginByUserID($uID);
  275. $this->redirect('/');
  276. }
  277. } catch(Exception $e) {
  278. $this->set('error', $e);
  279. $this->view();
  280. }
  281. }
  282. public function edit_attribute() {
  283. $uo = UserInfo::getByID($_POST['uID']);
  284. $u = new User();
  285. if ($uo->getUserID() == USER_SUPER_ID && (!$u->isSuperUser())) {
  286. throw new Exception(t('Only the super user may edit this account.'));
  287. }
  288. $assignment = PermissionKey::getByHandle('edit_user_properties')->getMyAssignment();
  289. $akID = $_REQUEST['uakID'];
  290. if (!in_array($akID, $assignment->getAttributesAllowedArray())) {
  291. throw new Exception(t('You do not have permission to modify this attribute.'));
  292. }
  293. $ak = UserAttributeKey::get($akID);
  294. if ($_POST['task'] == 'update_extended_attribute') {
  295. $ak->saveAttributeForm($uo);
  296. $val = $uo->getAttributeValueObject($ak);
  297. print $val->getValue('displaySanitized','display');
  298. exit;
  299. }
  300. if ($_POST['task'] == 'clear_extended_attribute') {
  301. $uo->clearAttribute($ak);
  302. $val = $uo->getAttributeValueObject($ak);
  303. print '<div class="ccm-attribute-field-none">' . t('None') . '</div>';
  304. exit;
  305. }
  306. }
  307. public function delete($delUserId, $token = null){
  308. $u=new User();
  309. try {
  310. $delUI=UserInfo::getByID($delUserId);
  311. if(!($delUI instanceof UserInfo)) {
  312. throw new Exception(t('Invalid user ID.'));
  313. }
  314. if (!PermissionKey::getByHandle('access_user_search')->validate($delUI)) {
  315. throw new Exception(t('Access Denied.'));
  316. }
  317. $tp = new TaskPermission();
  318. if (!$tp->canDeleteUser()) {
  319. throw new Exception(t('You do not have permission to perform this action.'));
  320. }
  321. if ($delUserId == USER_SUPER_ID) {
  322. throw new Exception(t('You may not remove the super user account.'));
  323. }
  324. if($delUserId==$u->getUserID()) {
  325. throw new Exception(t('You cannot delete your own user account.'));
  326. }
  327. $valt = Loader::helper('validation/token');
  328. if (!$valt->validate('delete_account', $token)) {
  329. throw new Exception($valt->getErrorMessage());
  330. }
  331. $delUI->delete();
  332. $resultMsg=t('User deleted successfully.');
  333. $_REQUEST=array();
  334. $_GET=array();
  335. $_POST=array();
  336. $this->set('message', $resultMsg);
  337. } catch (Exception $e) {
  338. $this->set('error', $e);
  339. }
  340. $this->view();
  341. }
  342. }