PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/install/couchdb/migrate/convert_user.php

https://bitbucket.org/speedealing/speedealing
PHP | 274 lines | 177 code | 48 blank | 49 comment | 30 complexity | c8278821a453bfc58d0f59ce31c10947 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2012 Patrick Mary <laube@hotmail.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/comm/serverprocess.php
  19. * \ingroup commercial societe
  20. * \brief load data to display
  21. * \version $Id: serverprocess.php,v 1.6 2012/01/27 16:15:05 synry63 Exp $
  22. */
  23. require_once("../../../main.inc.php");
  24. require_once(DOL_DOCUMENT_ROOT . "/core/class/html.formother.class.php");
  25. require_once(DOL_DOCUMENT_ROOT . "/core/db/couchdb/lib/couchAdmin.php");
  26. $langs->load("companies");
  27. $langs->load("customers");
  28. $langs->load("suppliers");
  29. $langs->load("commercial");
  30. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  31. * you want to insert a non-database field (for example a counter or static image)
  32. */
  33. //print substr($conf->couchdb->host, 7);exit;
  34. $couchdb = clone $couch;
  35. $couchdbuser = clone $couch;
  36. $couchdbuser->useDatabase("_users");
  37. /*
  38. * Paging
  39. */
  40. //$sLimit = " LIMIT 3";
  41. /* search basic */
  42. $sWhere = "";
  43. // If the user must only see his prospect, force searching by him
  44. if (!$user->rights->societe->client->voir && !$socid) {
  45. $search_sale = $user->id;
  46. }
  47. $entity = $conf->Couchdb->name;
  48. $flush = $_GET["flush"];
  49. if ($flush) {
  50. // reset old value
  51. //$couchdb->setQueryParameters(array("key" => $entity));
  52. $result = $couchdb->limit(50000)->getView('User', 'target_id');
  53. $couchdbuser->setQueryParameters(array("key" => $entity));
  54. $result2 = $couchdbuser->limit(50000)->getView('UserAdmin', 'target_id');
  55. $i = 0;
  56. if (count($result->rows) == 0 && count($result2->rows) == 0) {
  57. print "Effacement terminé";
  58. exit;
  59. }
  60. foreach ($result->rows AS $aRow) {
  61. $obj[$i]->_id = $aRow->value->_id;
  62. $obj[$i]->_rev = $aRow->value->_rev;
  63. $i++;
  64. }
  65. $i = 0;
  66. foreach ($result2->rows AS $aRow) {
  67. $obj2[$i]->_id = $aRow->value->_id;
  68. $obj2[$i]->_rev = $aRow->value->_rev;
  69. $i++;
  70. }
  71. try {
  72. if (count($obj))
  73. $couchdb->deleteDocs($obj);
  74. if (count($obj2))
  75. $couchdbuser->deleteDocs($obj2);
  76. } catch (Exception $e) {
  77. echo "Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  78. exit(1);
  79. }
  80. print "Effacement en cours";
  81. exit;
  82. }
  83. /* basic companies request query */
  84. $sql = "SELECT u.*";
  85. $sql .= " FROM " . MAIN_DB_PREFIX . "user as u";
  86. /* get the total of entries */
  87. $resultTotal = $db->query($sql);
  88. $iTotal = $db->num_rows($resultTotal);
  89. $sql.= $sWhere;
  90. /* usefull to regroup by the sale needed */
  91. if ($search_sale || $_GET['sSearch_7'] != "") {
  92. $sql.= " GROUP BY s.rowid";
  93. }
  94. $sql.= $sOrder;
  95. $sql.= $sLimit;
  96. $result = $db->query($sql);
  97. //print $sql;
  98. $couchAdmin = new couchAdmin($couchdb);
  99. $object = new User($db);
  100. $i = 0;
  101. while ($aRow = $db->fetch_object($result)) {
  102. $aRow->login = strtolower(dol_delaccents($aRow->login)); // supprime les accents
  103. try {
  104. $object->load("user:" . $aRow->login);
  105. $col[$aRow->rowid]->_rev = $object->_rev;
  106. } catch (Exception $e) {
  107. print $e->getMessage();
  108. print " <b>Not exist</b><br>";
  109. }
  110. // Verification email sinon construction automatique
  111. if (empty($aRow->email)) {
  112. $aRow->email = $aRow->login . '@' . $entity . '.fr';
  113. if ($aRow->login == "admin")
  114. $aRow->email = "admin@speedealing.com";
  115. }
  116. try {
  117. if ($user->superadmin) {
  118. $colAdmin[$aRow->rowid] = $couchAdmin->getUser($aRow->email);
  119. $exist = true;
  120. }
  121. } catch (Exception $e) {
  122. $couchAdmin->createUser($aRow->email, $aRow->pass);
  123. $colAdmin[$aRow->rowid] = $couchAdmin->getUser($aRow->email);
  124. $exist = false;
  125. }
  126. $col[$aRow->rowid]->_id = "user:" . $aRow->login;
  127. $col[$aRow->rowid]->name = $aRow->login;
  128. $col[$aRow->rowid]->tms = $db->jdate($aRow->tms);
  129. $col[$aRow->rowid]->Lastname = $aRow->name;
  130. $col[$aRow->rowid]->Firstname = $aRow->firstname;
  131. $col[$aRow->rowid]->PhonePro = $aRow->office_phone;
  132. $col[$aRow->rowid]->Fax = $aRow->office_fax;
  133. $col[$aRow->rowid]->PhoneMobile = $aRow->user_mobile;
  134. $col[$aRow->rowid]->email = $aRow->email;
  135. $col[$aRow->rowid]->rowid = $aRow->rowid;
  136. $col[$aRow->rowid]->signature = $aRow->signature;
  137. $col[$aRow->rowid]->Status = (bool) $aRow->statut;
  138. if ($col[$aRow->rowid]->Status)
  139. $col[$aRow->rowid]->Status = "ENABLE";
  140. else
  141. $col[$aRow->rowid]->Status = "DISABLE";
  142. $col[$aRow->rowid]->Lang = $aRow->lang;
  143. $col[$aRow->rowid]->class = "User";
  144. $col[$aRow->rowid]->group = array();
  145. if ($exist) {
  146. if (!in_array($entity, $colAdmin[$aRow->rowid]->entityList)) // Not already in the list
  147. $colAdmin[$aRow->rowid]->entityList[] = $entity;
  148. } else {
  149. $colAdmin[$aRow->rowid]->Status = $col[$aRow->rowid]->Status;
  150. $colAdmin[$aRow->rowid]->Lastname = $aRow->name;
  151. $colAdmin[$aRow->rowid]->Firstname = $aRow->firstname;
  152. $colAdmin[$aRow->rowid]->entity = $entity;
  153. $colAdmin[$aRow->rowid]->CreateDate = dol_now();
  154. $colAdmin[$aRow->rowid]->entityList[] = $entity;
  155. }
  156. if ($aRow->email != "admin@speedealing.com") { // Already a super admin
  157. if ($aRow->admin)
  158. $couchAdmin->addDatabaseAdminUser($aRow->email);
  159. else
  160. $couchAdmin->addDatabaseReaderUser($aRow->email); // Add database reader role for user
  161. }
  162. //print_r($col[$aRow->rowid]);
  163. //exit;
  164. $i++;
  165. }
  166. //print_r($colAdmin);
  167. $db->free($result);
  168. unset($result);
  169. /* sql query get groups */
  170. $sql = "SELECT g.rowid, g.nom, ug.fk_user";
  171. $sql.= " FROM " . MAIN_DB_PREFIX . "usergroup as g,";
  172. $sql.= " " . MAIN_DB_PREFIX . "usergroup_user as ug";
  173. $sql.= " WHERE ug.fk_usergroup = g.rowid";
  174. $result = $db->query($sql);
  175. /* create group */
  176. while ($aRow = $db->fetch_object($result)) {
  177. if (!empty($col[$aRow->fk_user]->rowid)) {
  178. $group[$aRow->rowid]->_id = 'group:' . strtolower($aRow->nom);
  179. $group[$aRow->rowid]->name = strtolower($aRow->nom);
  180. $group[$aRow->rowid]->rowid = $aRow->rowid;
  181. $group[$aRow->rowid]->class = "UserGroup";
  182. $group[$aRow->rowid]->right = new stdClass();
  183. $col[$aRow->fk_user]->group[] = strtolower($aRow->nom);
  184. //$couchAdmin->addRoleToUser($col[$aRow->fk_user]->name, $aRow->nom);
  185. }
  186. }
  187. $db->free($result);
  188. unset($result);
  189. // Get right
  190. $sql = "SELECT *";
  191. $sql.= " FROM " . MAIN_DB_PREFIX . "user_rights";
  192. $result = $db->query($sql);
  193. /* user right */
  194. while ($aRow = $db->fetch_object($result)) {
  195. if (!empty($col[$aRow->fk_user]->rowid)) {
  196. $id = $aRow->fk_id;
  197. $col[$aRow->fk_user]->own_rights->$id = true;
  198. }
  199. }
  200. $db->free($result);
  201. unset($result);
  202. // Get right
  203. $sql = "SELECT *";
  204. $sql.= " FROM " . MAIN_DB_PREFIX . "usergroup_rights";
  205. $result = $db->query($sql);
  206. /* group right */
  207. while ($aRow = $db->fetch_object($result)) {
  208. if (!empty($group[$aRow->fk_usergroup]->rowid)) {
  209. $id = $aRow->fk_id;
  210. $group[$aRow->fk_usergroup]->rights->$id = true;
  211. }
  212. }
  213. $db->free($result);
  214. unset($result);
  215. //print_r($colAdmin);exit;
  216. try {
  217. $result = $couchdb->storeDocs($col, false);
  218. $result = $couchdb->storeDocs($group, false);
  219. $result = $couchdbuser->storeDocs($colAdmin, false);
  220. } catch (Exception $e) {
  221. echo "Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  222. exit(1);
  223. }
  224. print_r($result);
  225. print "Import user terminée : " . count($col);
  226. ?>