📄 api.php
字号:
{ Piwik::checkUserIsSuperUserOrTheUser($userLogin); self::checkUserIsNotAnonymous( $userLogin ); $userInfo = self::getUser($userLogin); if(empty($password)) { $password = $userInfo['password']; } else { self::checkPassword($password); $password = self::getCleanPassword($password); } if(empty($alias)) { $alias = $userInfo['alias']; } if(empty($email)) { $email = $userInfo['email']; } if($email != $userInfo['email']) { self::checkEmail($email); } $alias = self::getCleanAlias($alias,$userLogin); $token_auth = self::getTokenAuth($userLogin,$password); $db = Zend_Registry::get('db'); $db->update( Piwik::prefixTable("user"), array( 'password' => $password, 'alias' => $alias, 'email' => $email, 'token_auth' => $token_auth, ), "login = '$userLogin'" ); } /** * Delete a user and all its access, given its login. * * @param string the user login. * * @exception if the user doesn't exist * * @return bool true on success */ static public function deleteUser( $userLogin ) { Piwik::checkUserIsSuperUser(); self::checkUserIsNotAnonymous( $userLogin ); if(!self::userExists($userLogin)) { throw new Exception(sprintf(Piwik_TranslateException("UsersManager_ExceptionDeleteDoesNotExist"),$userLogin)); } self::deleteUserOnly( $userLogin ); self::deleteUserAccess( $userLogin ); } /** * Returns true if the given userLogin is known in the database * * @return bool true if the user is known */ static public function userExists( $userLogin ) { Piwik::checkUserHasSomeAdminAccess(); $count = Zend_Registry::get('db')->fetchOne("SELECT count(*) FROM ".Piwik::prefixTable("user"). " WHERE login = ?", $userLogin); return $count != 0; } /** * Returns true if user with given email (userEmail) is known in the database * * @return bool true if the user is known */ static public function userEmailExists( $userEmail ) { Piwik::checkUserHasSomeAdminAccess(); $count = Zend_Registry::get('db')->fetchOne("SELECT count(*) FROM ".Piwik::prefixTable("user"). " WHERE email = ?", $userEmail); return $count != 0; } /** * Set an access level to a given user for a list of websites ID. * * If access = 'noaccess' the current access (if any) will be deleted. * If access = 'view' or 'admin' the current access level is deleted and updated with the new value. * * @param string Access to grant. Must have one of the following value : noaccess, view, admin * @param string The user login * @param int|array The array of idSites on which to apply the access level for the user. * If the value is "all" then we apply the access level to all the websites ID for which the current authentificated user has an 'admin' access. * * @exception if the user doesn't exist * @exception if the access parameter doesn't have a correct value * @exception if any of the given website ID doesn't exist * * @return bool true on success */ static public function setUserAccess( $userLogin, $access, $idSites) { self::checkAccessType( $access ); self::checkUserExists( $userLogin); if($userLogin == 'anonymous' && $access == 'admin') { throw new Exception(Piwik_TranslateException("UsersManager_ExceptionAdminAnonymous")); } // in case idSites is null we grant access to all the websites on which the current connected user // has an 'admin' access if($idSites === 'all') { $idSites = Piwik_SitesManager_API::getSitesIdWithAdminAccess(); } // in case the idSites is an integer we build an array elseif(!is_array($idSites)) { $idSites = Piwik_Site::getIdSitesFromIdSitesString($idSites); } // it is possible to set user access on websites only for the websites admin // basically an admin can give the view or the admin access to any user for the websites he manages Piwik::checkUserHasAdminAccess( $idSites ); self::deleteUserAccess( $userLogin, $idSites); // delete UserAccess $db = Zend_Registry::get('db'); // if the access is noaccess then we don't save it as this is the default value // when no access are specified if($access != 'noaccess') { foreach($idSites as $idsite) { $db->insert( Piwik::prefixTable("access"), array( "idsite" => $idsite, "login" => $userLogin, "access" => $access) ); } } // we reload the access list which doesn't yet take in consideration this new user access Zend_Registry::get('access')->loadAccess(); } /** * Throws an exception is the user login doesn't exist * * @param string user login * @exception if the user doesn't exist */ static private function checkUserExists( $userLogin ) { if(!self::userExists($userLogin)) { throw new Exception(sprintf(Piwik_TranslateException("UsersManager_ExceptionUserDoesNotExist"),$userLogin)); } } /** * Throws an exception is the user email cannot be found * * @param string user email * @exception if the user doesn't exist */ static private function checkUserEmailExists( $userEmail ) { if(!self::userEmailExists($userEmail)) { throw new Exception(sprintf(Piwik_TranslateException("UsersManager_ExceptionUserDoesNotExist"),$userEmail)); } } static private function checkUserIsNotAnonymous( $userLogin ) { if($userLogin == 'anonymous') { throw new Exception(Piwik_TranslateException("UsersManager_ExceptionEditAnonymous")); } } static private function checkAccessType($access) { $accessList = Piwik_Access::getListAccess(); // do not allow to set the superUser access unset($accessList[array_search("superuser", $accessList)]); if(!in_array($access,$accessList)) { throw new Exception(sprintf(Piwik_TranslateException("UsersManager_ExceptionAccessValues"),implode(", ", $accessList))); } } /** * Delete a user given its login. * The user's access are not deleted. * * @param string the user login. * */ static private function deleteUserOnly( $userLogin ) { $db = Zend_Registry::get('db'); $db->query("DELETE FROM ".Piwik::prefixTable("user")." WHERE login = ?", $userLogin); } /** * Delete the user access for the given websites. * The array of idsite must be either null OR the values must have been checked before for their validity! * * @param string the user login * @param array array of idsites on which to delete the access. If null then delete all the access for this user. * * @return bool true on success */ static private function deleteUserAccess( $userLogin, $idSites = null ) { $db = Zend_Registry::get('db'); if(is_null($idSites)) { $db->query( "DELETE FROM ".Piwik::prefixTable("access"). " WHERE login = ?", array( $userLogin) ); } else { foreach($idSites as $idsite) { $db->query( "DELETE FROM ".Piwik::prefixTable("access"). " WHERE idsite = ? AND login = ?", array($idsite, $userLogin) ); } } } /** * Generates a unique MD5 for the given login & password * * @param string Login * @param string Password string, or the MD5ied string of the password */ static public function getTokenAuth($userLogin, $password) { if(strlen($password) != 32) { $password = md5($password); } return md5($userLogin . $password ); } /** * Returns true if the login has a valid format : * - only A-Z a-z and the characters _ . and - * - length between 3 and 26 * * @param string login * @return bool */ static private function isValidLoginString( $input ) { $l = strlen($input); return $l >= 3 && $l <= 26 && (preg_match('/^[A-Za-z0-9\_\.-]*$/', $input) > 0); } /** * Returns true if the password is complex enough (at least 6 characters and max 26 characters) * * @param string email * @return bool */ static private function isValidPasswordString( $input ) { $l = strlen($input); return $l >= 6 && $l <= 26; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -