Skip to content

Commit

Permalink
Merge pull request #8194 from hregis/6.0_multicompany
Browse files Browse the repository at this point in the history
Fix: Update entity field in llx_user_rights and llx_usergroup_rights
  • Loading branch information
eldy committed Feb 14, 2018
2 parents cf5c972 + fa2fddc commit 147b35d
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 9 deletions.
4 changes: 3 additions & 1 deletion ChangeLog
Expand Up @@ -287,7 +287,9 @@ Following changes may create regression for some external modules, but were nece
exists, but if an external module need action on it, it must provides itself its trigger file.
* Use $conf->global->MULTICOMPANY_TRANSVERSE_MODE instead $conf->multicompany->transverse_mode. So, if you set var
$multicompany_transverse_mode to 1 into your conf file, you must remove this line and a new key into
the Home - setup - other admin page.
the Home - setup - other admin page.
* If you use Multicompany transverse mode, it will be necessary to check the activation of the modules in the children
entities and to review completely the rights of the groups and the users.
* Use getEntity('xxx') instead getEntity('xxx', 1) and use getEntity('xxx', 0) instead getEntity('xxx')
* Some other change were done in the way we read permission of a user when module multicompany is enabled. You can
retreive the old behavior by adding constant MULTICOMPANY_BACKWARD_COMPATIBILITY to 1.
Expand Down
181 changes: 173 additions & 8 deletions htdocs/install/upgrade2.php
Expand Up @@ -366,14 +366,27 @@
migrate_remise_except_entity($db,$langs,$conf);
}

// Scripts for last version
$afterversionarray=explode('.','5.0.9');
$beforeversionarray=explode('.','6.0.9');
if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
{
// No particular code
}
}
// Scripts for last version
$afterversionarray=explode('.','5.0.9');
$beforeversionarray=explode('.','6.0.9');
if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
{
if (! empty($conf->multicompany->enabled))
{
global $multicompany_transverse_mode;

// Only if the transverse mode is not used
if (empty($multicompany_transverse_mode))
{
// Migrate to add entity value into llx_user_rights
migrate_user_rights_entity($db, $langs, $conf);

// Migrate to add entity value into llx_usergroup_rights
migrate_usergroup_rights_entity($db, $langs, $conf);
}
}
}
}

// Code executed only if migrate is LAST ONE. Must always be done.
if (versioncompare($versiontoarray,$versionranarray) >= 0 || versioncompare($versiontoarray,$versionranarray) <= -3)
Expand Down Expand Up @@ -3967,6 +3980,158 @@ function migrate_remise_except_entity($db,$langs,$conf)
print '</td></tr>';
}

/**
* Migrate to add entity value into llx_user_rights
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_user_rights_entity($db,$langs,$conf)
{
print '<tr><td colspan="4">';

print '<b>'.$langs->trans('MigrationUserRightsEntity')."</b><br>\n";

$error = 0;

dolibarr_install_syslog("upgrade2::migrate_user_rights_entity");

$db->begin();

$sqlSelect = "SELECT u.rowid, u.entity";
$sqlSelect.= " FROM ".MAIN_DB_PREFIX."user as u";
$sqlSelect.= " WHERE u.entity > 1";
//print $sqlSelect;

$resql = $db->query($sqlSelect);
if ($resql)
{
$i = 0;
$num = $db->num_rows($resql);

if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);

$sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."user_rights SET";
$sqlUpdate.= " entity = " . $obj->entity;
$sqlUpdate.= " WHERE fk_user = " . $obj->rowid;

$result=$db->query($sqlUpdate);
if (! $result)
{
$error++;
dol_print_error($db);
}

print ". ";
$i++;
}
}
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}

if (! $error)
{
$db->commit();
}
else
{
$db->rollback();
}
}
else
{
dol_print_error($db);
$db->rollback();
}


print '</td></tr>';
}

/**
* Migrate to add entity value into llx_usergroup_rights
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_usergroup_rights_entity($db,$langs,$conf)
{
print '<tr><td colspan="4">';

print '<b>'.$langs->trans('MigrationUserGroupRightsEntity')."</b><br>\n";

$error = 0;

dolibarr_install_syslog("upgrade2::migrate_usergroup_rights_entity");

$db->begin();

$sqlSelect = "SELECT u.rowid, u.entity";
$sqlSelect.= " FROM ".MAIN_DB_PREFIX."usergroup as u";
$sqlSelect.= " WHERE u.entity > 1";
//print $sqlSelect;

$resql = $db->query($sqlSelect);
if ($resql)
{
$i = 0;
$num = $db->num_rows($resql);

if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);

$sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."usergroup_rights SET";
$sqlUpdate.= " entity = " . $obj->entity;
$sqlUpdate.= " WHERE fk_usergroup = " . $obj->rowid;

$result=$db->query($sqlUpdate);
if (! $result)
{
$error++;
dol_print_error($db);
}

print ". ";
$i++;
}
}
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}

if (! $error)
{
$db->commit();
}
else
{
$db->rollback();
}
}
else
{
dol_print_error($db);
$db->rollback();
}


print '</td></tr>';
}

/**
* Migration directory
*
Expand Down
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/install.lang
Expand Up @@ -193,6 +193,8 @@ MigrationCategorieAssociation=Migration of categories
MigrationEvents=Migration of events to add event owner into assignement table
MigrationRemiseEntity=Update entity field value of llx_societe_remise
MigrationRemiseExceptEntity=Update entity field value of llx_societe_remise_except
MigrationUserRightsEntity=Update entity field value of llx_user_rights
MigrationUserGroupRightsEntity=Update entity field value of llx_usergroup_rights
MigrationReloadModule=Reload module %s
ShowNotAvailableOptions=Show not available options
HideNotAvailableOptions=Hide not available options
Expand Down

0 comments on commit 147b35d

Please sign in to comment.