123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- defined('GNUSOCIAL') || die;
- abstract class AuthenticationModule extends Module
- {
-
- public $authoritative = false;
-
- public $autoregistration = false;
-
- public $password_changeable = true;
-
- public $provider_name = null;
-
-
- public function checkPassword($username, $password)
- {
- return false;
- }
-
- public function autoRegister($username, $nickname = null)
- {
- if (is_null($nickname)) {
- $nickname = $username;
- }
- $registration_data = [];
- $registration_data['nickname'] = $nickname;
- return User::register($registration_data);
- }
-
- public function changePassword($username, $oldpassword, $newpassword)
- {
- return false;
- }
-
- public function suggestNicknameForUsername($username)
- {
- return common_nicknamize($username);
- }
-
- public function onInitializePlugin()
- {
- if (is_null($this->provider_name)) {
- throw new Exception('must specify a provider_name for this authentication provider');
- }
- }
-
- public function onAutoRegister($nickname, $provider_name, &$user)
- {
- if ($provider_name == $this->provider_name && $this->autoregistration) {
- $suggested_nickname = $this->suggestNicknameForUsername($nickname);
- $test_user = User::getKV('nickname', $suggested_nickname);
- if ($test_user) {
-
- $suggested_nickname = common_nicknamize($nickname);
- }
- $test_user = User::getKV('nickname', $suggested_nickname);
- if ($test_user) {
-
-
- } else {
- $user = $this->autoRegister($nickname, $suggested_nickname);
- if ($user instanceof User) {
- User_username::register($user, $nickname, $this->provider_name);
- return false;
- }
- }
- }
- }
- public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
- {
-
- $user_username = new User_username();
- $user_username->username = $nickname;
- $user_username->provider_name = $this->provider_name;
- if ($user_username->find() && $user_username->fetch()) {
- $authenticated = $this->checkPassword($user_username->username, $password);
- if ($authenticated) {
- $authenticatedUser = User::getKV('id', $user_username->user_id);
- return false;
- }
- } else {
-
-
- $suggested_nickname = $this->suggestNicknameForUsername($nickname);
- $user = User::getKV('nickname', $suggested_nickname);
- if ($user) {
-
- $user_username = new User_username();
- $user_username->user_id = $user->id;
- $we_can_handle = false;
- if ($user_username->find()) {
-
-
- return;
- } else {
-
- $authenticated = $this->checkPassword($nickname, $password);
- if ($authenticated) {
- $authenticatedUser = $user;
- User_username::register($authenticatedUser, $nickname, $this->provider_name);
- return false;
- }
- }
- } else {
- $authenticated = $this->checkPassword($nickname, $password);
- if ($authenticated) {
- if (!Event::handle('AutoRegister', [$nickname, $this->provider_name, &$authenticatedUser])) {
-
-
-
- if ($authenticatedUser) {
- return false;
- }
- }
- }
- }
- }
- if ($this->authoritative) {
- return false;
- } else {
-
- return;
- }
- }
- public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
- {
- if ($this->password_changeable) {
- $user_username = new User_username();
- $user_username->user_id = $target->getID();
- $user_username->provider_name = $this->provider_name;
- if ($user_username->find(true)) {
- $authenticated = $this->checkPassword($user_username->username, $oldpassword);
- if ($authenticated) {
- $result = $this->changePassword($user_username->username, $oldpassword, $newpassword);
- if ($result) {
-
- return false;
- } else {
-
- throw new Exception(_('Password changing failed.'));
- }
- } else {
- if ($this->authoritative) {
-
-
- throw new Exception(_('Password changing failed.'));
- } else {
-
- return null;
- }
- }
- }
- } else {
- if ($this->authoritative) {
-
-
- throw new Exception(_('Password changing is not allowed.'));
- }
- }
- }
- public function onStartAccountSettingsPasswordMenuItem($widget)
- {
- if ($this->authoritative && !$this->password_changeable) {
-
- return false;
- }
- }
- public function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('user_username', User_username::schemaDef());
- return true;
- }
- public function onUserDeleteRelated($user, &$tables)
- {
- $tables[] = 'User_username';
- return true;
- }
- }
|