22
33/**
44 * @author 3liz
5- * @copyright 2019 3liz
5+ * @copyright 2019-2026 3liz
66 *
77 * @see http://3liz.com
88 *
1111
1212namespace Lizmap \Users ;
1313
14- class MigratorFromSqlite
15- {
16- public function __construct () {}
17-
18- public const MIGRATE_RES_OK = 1 ;
19- public const MIGRATE_RES_ALREADY_MIGRATED = 2 ;
14+ use Lizmap \App \AbstractMigratorFromSqlite ;
2015
21- public function migrateUsersAndRights ($ resetBefore = false )
16+ class MigratorFromSqlite extends AbstractMigratorFromSqlite
17+ {
18+ public function migrateUsersAndRights ($ resetBefore = false , $ forceMigration = false )
2219 {
2320 $ sqliteFile = \jApp::varPath ('db/jauth.db ' );
2421 if (!file_exists ($ sqliteFile )) {
@@ -50,55 +47,39 @@ public function migrateUsersAndRights($resetBefore = false)
5047 $ db ->exec ('DELETE FROM ' .$ db ->prefixTable ('jacl2_group ' ));
5148 $ table = $ daoUsersNew ->getTables ()[$ daoUsersNew ->getPrimaryTable ()]['realname ' ];
5249 $ db ->exec ('DELETE FROM ' .$ db ->prefixTable ($ table ));
53- } elseif ($ daoUsersNew ->countAll () > 0 || $ daoGeoBkmNew ->countAll () > 0 ) {
50+ } elseif (! $ forceMigration && $ daoUsersNew ->countAll () > 0 || $ daoGeoBkmNew ->countAll () > 0 ) {
5451 return self ::MIGRATE_RES_ALREADY_MIGRATED ;
5552 }
5653
57- $ this ->copyTable ($ daoUserSelector , 'oldjauth ' , $ profile );
58- $ this ->copyTable ('jacl2db~jacl2group ' , 'oldjauth ' , $ profile , false );
59- $ this ->copyTable ('jacl2db~jacl2subjectgroup ' , 'oldjauth ' , $ profile , false );
60- $ this ->copyTable ('jacl2db~jacl2subject ' , 'oldjauth ' , $ profile , false );
61- $ this ->copyTable ('jacl2db~jacl2usergroup ' , 'oldjauth ' , $ profile , false );
62- $ this ->copyTable ('jacl2db~jacl2rights ' , 'oldjauth ' , $ profile , false );
63- $ this ->copyTable ('lizmap~geobookmark ' , 'oldjauth ' , $ profile , true );
54+ $ this ->prepareTablesCopy ('oldjauth ' , $ profile , $ resetBefore );
55+ $ this ->copyTable ($ daoUserSelector , true , array ('create_date ' ), true );
56+ $ this ->copyTable ('jacl2db~jacl2group ' , false );
57+ $ this ->copyTable ('jacl2db~jacl2subjectgroup ' , false );
58+ $ this ->copyTable ('jacl2db~jacl2subject ' , false );
59+
60+ if (!$ resetBefore ) {
61+ $ this ->deleteExistingRightsForImportedGroups ();
62+ }
63+ $ this ->copyTable ('jacl2db~jacl2usergroup ' , false );
64+ $ this ->copyTable ('jacl2db~jacl2rights ' , false );
65+ $ this ->copyTable ('lizmap~geobookmark ' , true );
6466
6567 return self ::MIGRATE_RES_OK ;
6668 }
6769
68- protected function copyTable ( $ daoSelector , $ oldProfile , $ newProfile , $ updateSequence = true )
70+ protected function deleteExistingRightsForImportedGroups ( )
6971 {
70- $ daoNew = \jDao::get ($ daoSelector , $ newProfile );
71- $ daoSqlite = \jDao::create ($ daoSelector , $ oldProfile );
72- $ properties = array_keys ($ daoSqlite ->getProperties ());
73- foreach ($ daoSqlite ->findAll () as $ rec ) {
74- $ daoRec = \jDao::createRecord ($ daoSelector , $ newProfile );
75- foreach ($ properties as $ prop ) {
76- $ daoRec ->{$ prop } = $ rec ->{$ prop };
77- }
78-
79- try {
80- $ daoNew ->insert ($ daoRec );
81- } catch (\Exception $ e ) {
82- echo '*** Insert ERROR for the record ' ;
83- var_export ($ rec ->getPk ());
84- echo "\nError is: " .$ e ->getMessage ()."\n" ;
85- }
72+ $ oldDb = \jDb::getConnection ($ this ->oldProfile );
73+ $ newDb = \jDb::getConnection ($ this ->newProfile );
74+
75+ $ rs = $ oldDb ->query ('SELECT distinct(id_aclgrp) as id_aclgrp2 FROM ' .$ oldDb ->prefixTable ('jacl2_rights ' ));
76+ foreach ($ rs as $ rec ) {
77+ $ newDb ->exec ('DELETE FROM ' .$ newDb ->prefixTable ('jacl2_rights ' ).' WHERE id_aclgrp = ' .$ newDb ->quote ($ rec ->id_aclgrp2 ));
8678 }
8779
88- if ($ updateSequence ) {
89- $ idField = $ daoNew ->getProperties ()[$ daoNew ->getPrimaryKeyNames ()[0 ]]['fieldName ' ];
90- $ table = $ daoNew ->getTables ()[$ daoNew ->getPrimaryTable ()]['realname ' ];
91-
92- $ conn = \jDb::getConnection ($ newProfile );
93- $ rs = $ conn ->query ('SELECT pg_get_serial_sequence( ' .$ conn ->quote ($ table ).', ' .$ conn ->quote ($ idField ).') as sequence_name ' );
94- if ($ rs && ($ rec = $ rs ->fetch ())) {
95- $ sequence = $ rec ->sequence_name ;
96- if ($ sequence ) {
97- $ conn ->query ('SELECT setval( ' .$ conn ->quote ($ sequence ).',
98- (SELECT max( ' .$ conn ->encloseName ($ idField ).')
99- FROM ' .$ conn ->encloseName ($ table ).')) ' );
100- }
101- }
80+ $ rs = $ oldDb ->query ('SELECT distinct(login) as login2 FROM ' .$ oldDb ->prefixTable ('jacl2_user_group ' ));
81+ foreach ($ rs as $ rec ) {
82+ $ newDb ->exec ('DELETE FROM ' .$ newDb ->prefixTable ('jacl2_user_group ' ).' WHERE login = ' .$ newDb ->quote ($ rec ->login2 ));
10283 }
10384 }
10485
@@ -118,7 +99,7 @@ protected function createUsersTables()
11899
119100 $ profile = \jProfiles::get ('jdb ' , $ profileName );
120101 if (!$ profile ) {
121- throw new \UnexpectedValueException ("No {$ profile } profile defined into profiles.ini.php " , 1 );
102+ throw new \UnexpectedValueException ("No {$ profileName } profile defined into profiles.ini.php " , 1 );
122103 }
123104
124105 if ($ profile ['driver ' ] == 'sqlite3 ' ) {
@@ -133,7 +114,7 @@ protected function createUsersTables()
133114 return array ($ daoSelector , $ profileName );
134115 }
135116
136- // the table does not exists , let's create it
117+ // the table does not exist , let's create it
137118 $ mapper = new \jDaoDbMapper ($ profileName );
138119 $ mapper ->createTableFromDao ($ daoSelector );
139120
0 commit comments