Location: PHPKode > projects > iF.SVNAdmin > svnadmin/actions/assign_userrole.php
<?php
/**
 * iF.SVNAdmin
 * Copyright (c) 2010 by Manuel Freiholz
 * http://www.insanefactory.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 */
if( !defined('ACTION_HANDLING') ) {
  die("HaHa!");
}

// Check module.
$appEngine->forwardInvalidModule( !$appEngine->isAclManagerActive() );

// Get request vars.
$selroles = get_request_var("selected_assign_role_name");
$selusers = get_request_var("selected_users");

// Fallback to array of roles.
if ($selroles == NULL)
  $selroles = get_request_var("selected_roles");
else
  $selroles = array($selroles);

if (count($selroles) == 1 && empty($selroles[0]))
  $selroles = NULL;

// Validate selection.
if ($selroles == NULL || $selusers == NULL)
{
  $appTemplate->addDefine("WARNING");
  $appTemplate->addReplacement("WARNINGMSG",$appTR->tr("You have to select at least one user and one role."));
}
else
{
  // Count of: All, Done, Failed
  $cntAll = count($selroles) * count($selusers);
  $doneList = array();
  $warnList = array();
  $failedList = array();

  // Iterate all selected users and roles.
  for ($i=0; $i<count($selroles); $i++)
  {
    $oR = new \svnadmin\core\entities\Role;
    $oR->name = $selroles[$i];

    for ($j=0; $j<count($selusers); $j++)
    {
      // Skip * user.
      if ($selusers[$j] == "*")
      {
        $warnList[] = $selusers[$j]." / ".$oR->name;
        continue;
      }

      $oU = new \svnadmin\core\entities\User;
      $oU->name = $selusers[$j];

      if ($appEngine->getAclManager()->assignUserToRole($oU, $oR))
      {
        $doneList[] = $oU->name." / ".$oR->name;
      }
      else
      {
        $failedList[] = $oU->name." / ".$oR->name;
      }
    } //for
  } //for

  $appEngine->getAclManager()->save();

  //////////////////////////////////////////////////////////////////////////////
  // Result
  //////////////////////////////////////////////////////////////////////////////

  if (count($doneList) > 0)
  {
    $msg = tr("The following assignments has been done:");
    $msg.= "<br>";
    $msg.= "<ul>";
    $doneList = array_unique($doneList);
    foreach ($doneList as &$valPath)
    {
      $msg.= "<li>".$valPath."</li>";
    }
    $msg.= "</ul>";
    $appTemplate->addDefine("INFO");
    $appTemplate->addReplacement("INFOMSG", $msg);
  }

  if (count($warnList) > 0)
  {
    $msg = tr("It is not possible to do the following assignments, skipped them:");
    $msg.= "<br>";
    $msg.= "<ul>";
    $warnList = array_unique($warnList);
    foreach ($warnList as &$valPath)
    {
      $msg.= "<li>".$valPath."</li>";
    }
    $msg.= "</ul>";
    $appTemplate->addDefine("WARNING");
    $appTemplate->addReplacement("WARNINGMSG", $msg);
  }

  if (count($failedList) > 0)
  {
    $msg = tr("Due to an unknown error, the following assignments could not been handled:");
    $msg.= "<br>";
    $msg.= "<ul>";
    $failedList = array_unique($failedList);
    foreach ($failedList as &$valPath)
    {
      $msg.= "<li>".$valPath."</li>";
    }
    $msg.= "</ul>";
    $appTemplate->addDefine("ERROR");
    $appTemplate->addReplacement("ERRORMSG", $msg);
  }
}
?>
Return current item: iF.SVNAdmin