Skip to content
Snippets Groups Projects
Select Git revision
  • 5106c84bc3dde6d44dfe7a152ec24a9c39e93054
  • master default protected
  • v1.5
  • v1.4.7
  • v1.4.6
  • v1.4.5
  • v1.4.4
  • v1.4.3
  • v1.4.2
  • v1.4.1
  • v1.4
  • v1.3
  • v1.2.1
  • v1.2
  • v1.1.2
  • v1.1.1
  • v1.0
17 results

MatrixAccount.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MatrixAccount.php 3.43 KiB
    <?php
    
    /**
     * MatrixAccount.php
     * model class for mapping between Matrix and Stud.IP accounts.
     *
     * 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; either version 2 of
     * the License, or (at your option) any later version.
     *
     * @author      Thomas Hackl <hackl@data-quest.de>
     * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
     * @category    Matrix
     *
     * @property string user_id database column
     * @property string matrix_account_id database column
     * @property string mkdate database column
     * @property string chdate database column
     */
    
    class MatrixAccount extends SimpleORMap
    {
    
        protected static function configure($config = [])
        {
            $config['db_table'] = 'matrix_accounts';
            $config['belongs_to']['user'] = [
                'class_name' => 'User',
                'foreign_key' => 'user_id'
            ];
    
            parent::configure($config);
        }
    
        public function getLinkedAccount()
        {
            $account = new \Patrix\Account($this->matrix_account_id, $this->matrix_password);
            if (Config::get()->MATRIX_AUTO_CREATE_ACCOUNTS) {
                MatrixClient::get()->login($account);
            }
            return $account;
        }
    
        public static function randomPassword( $length = 14 ) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
            $password = substr( str_shuffle( $chars ), 0, $length );
    
            return $password;
        }
    
        /**
         * Create or get the current system Matrix account.
         * Its data is not stored in the regular database table, but in the global config.
         * If no data is found, a new account is automatically registered.
         */
        public static function requireSystemAccount()
        {
            if (trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_USERNAME) != '' &&
                    (trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_PASSWORD) != '' ||
                        trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_ACCESS_TOKEN) != '')) {
    
                // Access token specified, use this (and skip login because the token can be used directly).
                if (trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_ACCESS_TOKEN) != '') {
                    $account = new Patrix\Account(trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_USERNAME), '');
                    $account->setUserId('@' . Config::get()->MATRIX_SYSTEM_ACCOUNT_USERNAME .
                        ':' . Config::get()->MATRIX_ACCOUNTS_LOCAL_PART);
                    $account->setAccessData(trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_ACCESS_TOKEN), 'Stud.IP');
                // No token, login by username and password.
                } else {
                    $account = new Patrix\Account(trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_USERNAME),
                        trim(Config::get()->MATRIX_SYSTEM_ACCOUNT_PASSWORD));
                    MatrixClient::get()->login($account);
                }
                return $account;
    
            } else {
    
                $username = 'studip';
                $password = self::randomPassword();
                $sysaccount = new \Patrix\Account($username, $password);
                if (MatrixClient::get()->registerAccount($sysaccount)) {
                    Config::get()->store('MATRIX_SYSTEM_ACCOUNT_USERNAME', $username);
                    Config::get()->store('MATRIX_SYSTEM_ACCOUNT_PASSWORD', $password);
                    return $sysaccount;
                } else {
                    return null;
                }
    
            }
        }
    
    }