usermanagement.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 763 行 · 第 1/3 页

PHP
763
字号

        $user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
        $oUser =& User::get($user_id);

        if (PEAR::isError($oUser) || $oUser == false) {
            $this->errorRedirectToMain(_kt('Please select a user first.'));
            exit(0);
        }

        $this->aBreadcrumbs[] = array('name' => $oUser->getName());

        $edit_fields = array();
        $edit_fields[] =  new KTPasswordWidget(_kt('Password'), _kt('Specify an initial password for the user.'), 'new_password', null, $this->oPage, true);
        $edit_fields[] =  new KTPasswordWidget(_kt('Confirm Password'), _kt('Confirm the password specified above.'), 'confirm_password', null, $this->oPage, true);

        $oTemplating =& KTTemplating::getSingleton();
        $oTemplate = $oTemplating->loadTemplate("ktcore/principals/updatepassword");
        $aTemplateData = array(
            "context" => $this,
            "edit_fields" => $edit_fields,
            "edit_user" => $oUser,
            'old_search' => $old_search,
        );
        return $oTemplate->render($aTemplateData);
    }

    function do_updatePassword() {
        $user_id = KTUtil::arrayGet($_REQUEST, 'user_id');

        $old_search = KTUtil::arrayGet($_REQUEST, 'old_search');

        $password = KTUtil::arrayGet($_REQUEST, 'new_password');
        $confirm_password = KTUtil::arrayGet($_REQUEST, 'confirm_password');

   		$KTConfig =& KTConfig::getSingleton();
		$minLength = ((int) $KTConfig->get('user_prefs/passwordLength', 6));
		$restrictAdmin = ((bool) $KTConfig->get('user_prefs/restrictAdminPasswords', false));

        if ($restrictAdmin && (strlen($password) < $minLength)) {
		    $this->errorRedirectToMain(sprintf(_kt("The password must be at least %d characters long."), $minLength));
		} else if (empty($password)) {
            $this->errorRedirectToMain(_kt("You must specify a password for the user."));
        } else if ($password !== $confirm_password) {
            $this->errorRedirectToMain(_kt("The passwords you specified do not match."));
        }
        // FIXME more validation would be useful.
        // validated and ready..
        $this->startTransaction();

        $oUser =& User::get($user_id);
        if (PEAR::isError($oUser) || $oUser == false) {
            $this->errorRedirectToMain(_kt("Please select a user to modify first."));
        }


        // FIXME this almost certainly has side-effects.  do we _really_ want
        $oUser->setPassword(md5($password)); //

        $res = $oUser->update();
        //$res = $oUser->doLimitedUpdate(); // ignores a fix blacklist of items.

        if (PEAR::isError($res) || ($res == false)) {
            $this->errorRedirectoToMain(_kt('Failed to update user.'));
        }

        $this->commitTransaction();
        $this->successRedirectToMain(_kt('User information updated.'));

    }

    function do_editUserSource() {
        $user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
        $oUser =& $this->oValidator->validateUser($user_id);
        $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('User Management'));
        $this->aBreadcrumbs[] = array('name' => $oUser->getName());

        $oAuthenticationSource = KTAuthenticationSource::getForUser($oUser);
        if (is_null($oAuthenticationSource)) {
            $oProvider =& new KTBuiltinAuthenticationProvider;
        } else {
            $sProvider = $oAuthenticationSource->getAuthenticationProvider();
            $oRegistry =& KTAuthenticationProviderRegistry::getSingleton();
            $oProvider = $oRegistry->getAuthenticationProvider($sProvider);
        }

        $oProvider->subDispatch($this);
        exit();
    }

    function do_editgroups() {
        $user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
        $oUser = User::get($user_id);
        $old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
        if ((PEAR::isError($oUser)) || ($oUser === false)) {
            $this->errorRedirectToMain(_kt('No such user.'), sprintf("old_search=%s&do_search=1", $old_search));
        }



        $this->aBreadcrumbs[] = array('name' => $oUser->getName());
        $this->oPage->setBreadcrumbDetails(_kt('edit groups'));
        $this->oPage->setTitle(sprintf(_kt("Edit %s's groups"), $oUser->getName()));
        // generate a list of groups this user is authorised to assign.

        /* FIXME there is a nasty side-effect:  if a user cannot assign a group
        * to a user, and that user _had_ that group pre-edit,
        * then their privileges are revoked.
        * is there _any_ way to fix that?
        */

        $aInitialGroups = GroupUtil::listGroupsForUser($oUser);
        $aAllGroups = GroupUtil::listGroups();

        $aUserGroups = array();
        $aFreeGroups = array();
        foreach ($aInitialGroups as $oGroup) {
            $aUserGroups[$oGroup->getId()] = $oGroup;
        }
        foreach ($aAllGroups as $oGroup) {
            if (!array_key_exists($oGroup->getId(), $aUserGroups)) {
                $aFreeGroups[$oGroup->getId()] = $oGroup;
            }
        }

	$oJSONWidget = new KTJSONLookupWidget(_kt('Groups'),
					      _kt('Select the groups which this user should belong to from the left-hand list and then click the <strong>right pointing arrows</strong>. Once you have added all the groups that you require, press <strong>save changes</strong>.'),
					      'groups', '', $this->oPage, false, null, null,
					      array('action'=>'getGroups',
						    'assigned' => $aUserGroups,
						    'multi'=>'true',
						    'size'=>'8'));

        $oTemplating =& KTTemplating::getSingleton();
        $oTemplate = $oTemplating->loadTemplate("ktcore/principals/usergroups");
        $aTemplateData = array(
            "context" => $this,
            "unused_groups" => $aFreeGroups,
            "user_groups" => $aUserGroups,
            "edit_user" => $oUser,
	    "widget" => $oJSONWidget,
            'old_search' => $old_search,
        );
        return $oTemplate->render($aTemplateData);
    }


    function json_getGroups() {
        $sFilter = KTUtil::arrayGet($_REQUEST, 'filter', false);
        $aGroupList = array('off' => _kt('-- Please filter --'));

        if($sFilter && trim($sFilter)) {
            $aGroups = Group::getList(sprintf('name like "%%%s%%"', $sFilter));
            $aGroupList = array();
            foreach($aGroups as $oGroup) {
                $aGroupList[$oGroup->getId()] = $oGroup->getName();
            }
        }

        return $aGroupList;
    }


    function do_saveUser() {
        $user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
        $old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
        $aErrorOptions = array(
                'redirect_to' => array('editUser', sprintf('user_id=%d&old_search=%s&do_search=1', $user_id, $old_search))
        );
        $aInputKeys = array('newusername', 'name', 'email_address', 'email_notifications', 'mobile_number', 'max_sessions');
        $this->persistParams($aInputKeys);

        $name = $this->oValidator->validateString(
                KTUtil::arrayGet($_REQUEST, 'name'),
                KTUtil::meldOptions($aErrorOptions, array('message' => _kt("You must provide a name")))
        );

        $username = $this->oValidator->validateString(
                KTUtil::arrayGet($_REQUEST, 'newusername'),
                KTUtil::meldOptions($aErrorOptions, array('message' => _kt("You must provide a username")))
        );

        $email_address = KTUtil::arrayGet($_REQUEST, 'email_address');
        if(strlen(trim($email_address))) {
                $email_address = $this->oValidator->validateEmailAddress($email_address, $aErrorOptions);
        }

        $email_notifications = KTUtil::arrayGet($_REQUEST, 'email_notifications', false);
        if ($email_notifications !== false) $email_notifications = true;

        $mobile_number = KTUtil::arrayGet($_REQUEST, 'mobile_number');

        $max_sessions = KTUtil::arrayGet($_REQUEST, 'max_sessions', '3', false);

        // FIXME more validation would be useful.
        // validated and ready..
        $this->startTransaction();

        $oUser =& User::get($user_id);
        if (PEAR::isError($oUser) || $oUser == false) {
            $this->errorRedirectToMain(_kt("Please select a user to modify first."), sprintf("old_search=%s&do_search=1", $old_search));
        }

        $dupUser =& User::getByUserName($username);
        if(!PEAR::isError($dupUser)) {
            if ($dupUser->getId() != $oUser->getId()) {
                $this->errorRedirectTo('addUser', _kt("A user with that username already exists"));
            }
        }

        $oUser->setName($name);
        $oUser->setUsername($username);  // ?
        $oUser->setEmail($email_address);
        $oUser->setEmailNotification($email_notifications);
        $oUser->setMobile($mobile_number);
        $oUser->setMaxSessions($max_sessions);

        // old system used the very evil store.php.
        // here we need to _force_ a limited update of the object, via a db statement.
        //
        $res = $oUser->update();
        // $res = $oUser->doLimitedUpdate(); // ignores a fix blacklist of items.



        if (PEAR::isError($res) || ($res == false)) {
            $this->errorRedirectoToMain(_kt('Failed to update user.'), sprintf("old_search=%s&do_search=1", $old_search));
        }

        $this->commitTransaction();
        $this->successRedirectToMain(_kt('User information updated.'), sprintf("old_search=%s&do_search=1", $old_search));
    }

    function do_createUser() {
        // FIXME generate and pass the error stack to adduser.
        $old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
        $aErrorOptions = array(
                'redirect_to' => array('addUser', sprintf('old_search=%s&do_search=1', $old_search))
        );
        $aInputKeys = array('newusername', 'name', 'email_address', 'email_notifications', 'mobile_number', 'max_sessions');
        $this->persistParams($aInputKeys);

        $username = $this->oValidator->validateString(
                KTUtil::arrayGet($_REQUEST, 'newusername'),
                KTUtil::meldOptions($aErrorOptions, array('message' => _kt("You must specify a new username.")))
        );

        $name = $this->oValidator->validateString(
                KTUtil::arrayGet($_REQUEST, 'name'),
                KTUtil::meldOptions($aErrorOptions, array('message' => _kt("You must provide a name")))
        );


        $email_address = KTUtil::arrayGet($_REQUEST, 'email_address');
        $email_notifications = KTUtil::arrayGet($_REQUEST, 'email_notifications', false);
        if ($email_notifications !== false) $email_notifications = true;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?