Пример #1
0
    public static function project_addUser()
    {
        SystemEvent::raise(SystemEvent::DEBUG, "Called.", __METHOD__);
        if (!isset($GLOBALS['project']) || !$GLOBALS['project'] instanceof Project) {
            $msg = 'Invalid request';
            SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
            echo json_encode(array('success' => false, 'error' => $msg));
            exit;
        }
        if (!$GLOBALS['project']->userHasAccessLevel($GLOBALS['user'], Access::WRITE) && !$GLOBALS['user']->hasCos(UserCos::ROOT)) {
            SystemEvent::raise(SystemEvent::INFO, "Not authorized. [USER={$GLOBALS['user']->getUsername()}]", __METHOD__);
            echo json_encode(array('success' => false, 'error' => 'Not authorized'));
            exit;
        }
        $user = User::getByUsername($_GET['username']);
        if (!$user instanceof User) {
            SystemEvent::raise(SystemEvent::INFO, "Username not found. [USERNAME={$_GET['username']}]", __METHOD__);
            echo json_encode(array('success' => false, 'error' => 'Not found'));
            exit;
        }
        $GLOBALS['project']->addToUsers($user);
        $accessLevels = Access::getList();
        $html = <<<EOT
            <li id="{$user->getUsername()}">
              <div class="avatar40"><img src="{$user->getAvatarUrl()}" width="40" height="40"></div>
              <div class="username"><h3>{$user->getUsername()}</h3></div>
              <div class="actionItems">
EOT;
        if (!$GLOBALS['project']->userHasAccessLevel($user, Access::OWNER)) {
            $removeLink = UrlManager::getForAjaxProjectRemoveUser($user->getUsername());
            $html .= <<<EOT
                <div class="remove"><a class="{$user->getUsername()} btn danger" href="{$removeLink}">Remove</a></div>
                <div class="access"><a class="{$user->getUsername()} btn" href="#">Access</a></div>
                <div class="popover-wrapper">
                  <div id="accessLevelPaneLevels_{$user->getUsername()}" class="accessLevelPopover popover above">
                    <div class="arrow"></div>
                    <div class="inner">
                      <h3 class="title">Access level</h3>
                      <div class="content">
                        <ul class="inputs-list">
EOT;
            foreach ($accessLevels as $accessLevel => $accessName) {
                if ($accessLevel !== 0) {
                    $checked = '';
                    if ($GLOBALS['project']->getAccessLevelFromUser($user) == $accessLevel) {
                        $checked = ' checked';
                    }
                    $accessName = ucfirst($accessName);
                    $html .= <<<EOT
                          <li>
                            <input type="radio" value="{$user->getUsername()}_{$accessLevel}" name="accessLevel_{$user->getUsername()}" id="{$accessLevel}"{$checked} />
                            <span>{$accessName}</span>
                          </li>
EOT;
                }
            }
            $html .= "\n                        </ul>\n                      </div>\n                    </div>\n                  </div>\n                </div>";
        } else {
            $html .= '
                <div class="noChanges">Owner (no changes allowed)</div>';
        }
        $html .= "\n              </div>\n            </li>";
        echo json_encode(array('success' => true, 'html' => $html));
        exit;
    }