sintonia/legacy/application/models/User.php

415 lines
9.9 KiB
PHP
Raw Normal View History

<?php
class Application_Model_User
{
private $_userInstance;
public function __construct($userId)
{
if (empty($userId)) {
2011-02-09 20:09:42 +01:00
$this->_userInstance = $this->createUser();
} else {
2011-02-09 20:09:42 +01:00
$this->_userInstance = CcSubjsQuery::create()->findPK($userId);
2012-01-16 09:56:57 +01:00
if (is_null($this->_userInstance)) {
2012-01-16 09:56:57 +01:00
throw new Exception();
}
}
}
public function getId()
{
2011-02-09 19:03:46 +01:00
return $this->_userInstance->getDbId();
}
public function isGuest()
{
return $this->getType() == UTYPE_GUEST;
}
public function isHostOfShow($showId)
{
$userId = $this->_userInstance->getDbId();
2021-10-11 16:10:47 +02:00
return CcShowHostsQuery::create()
->filterByDbShow($showId)
->filterByDbHost($userId)->count() > 0;
}
public function isHost()
{
return $this->isUserType(UTYPE_HOST);
}
public function isPM()
{
return $this->isUserType(UTYPE_PROGRAM_MANAGER);
}
public function isAdmin()
{
return $this->isUserType(UTYPE_ADMIN);
}
2021-10-11 16:10:47 +02:00
public function isSuperAdmin()
{
return $this->isUserType(UTYPE_SUPERADMIN);
}
2021-10-11 16:10:47 +02:00
public function canSchedule($p_showId)
{
$type = $this->getType();
$result = false;
2022-07-07 20:01:15 +02:00
if (
$this->isAdmin()
2021-10-11 16:10:47 +02:00
|| $this->isSuperAdmin()
|| $this->isPM()
2022-07-07 20:01:15 +02:00
|| self::isHostOfShow($p_showId)
) {
$result = true;
}
2021-10-11 16:10:47 +02:00
return $result;
}
public function isSourcefabricAdmin()
{
$username = $this->getLogin();
2021-10-11 16:10:47 +02:00
if ($username == 'sourcefabric_admin') {
return true;
}
2021-10-11 16:10:47 +02:00
return false;
}
2012-09-14 22:50:28 +02:00
// TODO : refactor code to only accept arrays for isUserType and
// simplify code even further
public function isUserType($type)
{
2012-09-14 22:47:55 +02:00
if (!is_array($type)) {
2021-10-11 16:10:47 +02:00
$type = [$type];
}
2012-09-14 22:47:55 +02:00
$real_type = $this->_userInstance->getDbType();
2021-10-11 16:10:47 +02:00
2012-09-14 22:47:55 +02:00
return in_array($real_type, $type);
}
public function setLogin($login)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbLogin($login);
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function setPassword($password)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbPass(md5($password));
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function setFirstName($firstName)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbFirstName($firstName);
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function setLastName($lastName)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbLastName($lastName);
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function setType($type)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbType($type);
2011-02-09 19:03:46 +01:00
}
public function setEmail($email)
{
$user = $this->_userInstance;
$user->setDbEmail(strtolower($email));
}
public function setCellPhone($cellPhone)
{
$user = $this->_userInstance;
$user->setDbCellPhone($cellPhone);
}
public function setSkype($skype)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbSkypeContact($skype);
}
public function setJabber($jabber)
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
$user->setDbJabberContact($jabber);
}
2012-01-16 09:56:57 +01:00
public function getLogin()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbLogin();
}
public function getPassword()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbPass();
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function getFirstName()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbFirstName();
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function getLastName()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbLastName();
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function getType()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbType();
2011-02-09 19:03:46 +01:00
}
public function getEmail()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbEmail();
}
public function getCellPhone()
{
$user = $this->_userInstance;
return $user->getDbCellPhone();
}
public function getSkype()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbSkypeContact();
}
public function getJabber()
{
$user = $this->_userInstance;
2012-01-16 09:56:57 +01:00
return $user->getDbJabberContact();
}
2012-01-16 09:56:57 +01:00
public function save()
{
2011-02-09 20:09:42 +01:00
$this->_userInstance->save();
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public function delete()
{
if (!$this->_userInstance->isDeleted()) {
2011-02-09 20:09:42 +01:00
$this->_userInstance->delete();
}
2011-02-09 19:03:46 +01:00
}
2021-10-11 16:10:47 +02:00
public function getOwnedFiles()
{
$user = $this->_userInstance;
2012-08-28 23:24:55 +02:00
// do we need a find call at the end here?
return $user->getCcFilessRelatedByDbOwnerId();
}
2012-09-18 21:26:43 +02:00
public function donateFilesTo($user) // $user is object not user id
{
$my_files = $this->getOwnedFiles();
foreach ($my_files as $file) {
$file->reassignTo($user);
}
}
2012-08-28 23:24:55 +02:00
public function deleteAllFiles()
{
$my_files = $this->getOwnedFiles();
foreach ($my_files as $file) {
$file->delete();
}
2012-08-28 23:24:55 +02:00
}
private function createUser()
{
2021-10-11 16:10:47 +02:00
return new CcSubjs();
}
public static function getUsersOfType($type)
{
return CcSubjsQuery::create()->filterByDbType($type)->find();
}
2012-09-18 21:26:43 +02:00
/**
2021-10-11 16:10:47 +02:00
* Get the first admin user from the database.
*
* This function gets used in UserController in the delete action. The controller
* uses it to figure out who to reassign the deleted users files to.
*
* @param $ignoreUser String optional userid of a user that shall be ignored when
fix(deps): update dependency friendsofphp/php-cs-fixer to <3.43.2 (main) (#2848) [![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [friendsofphp/php-cs-fixer](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer) | `<3.42.1` -> `<3.43.2` | [![age](https://developer.mend.io/api/mc/badges/age/packagist/friendsofphp%2fphp-cs-fixer/3.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/packagist/friendsofphp%2fphp-cs-fixer/3.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/packagist/friendsofphp%2fphp-cs-fixer/3.42.0/3.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/packagist/friendsofphp%2fphp-cs-fixer/3.42.0/3.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>PHP-CS-Fixer/PHP-CS-Fixer (friendsofphp/php-cs-fixer)</summary> ### [`v3.43.1`](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/HEAD/CHANGELOG.md#Changelog-for-v3431) [Compare Source](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/compare/v3.43.0...v3.43.1) - fix: Import only unique symbols' short names ([#&#8203;7635](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7635)) ### [`v3.43.0`](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/HEAD/CHANGELOG.md#Changelog-for-v3430) [Compare Source](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/compare/v3.42.0...v3.43.0) - chore: change base of `@Symfony` set to `@PER-CS2.0` ([#&#8203;7627](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7627)) - chore: PHPUnit - allow for v10 ([#&#8203;7606](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7606)) - chore: Preg - rework catching the error ([#&#8203;7616](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7616)) - chore: Revert unneeded peer-dep-pin and re-gen lock file ([#&#8203;7618](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7618)) - docs: drop extra note about 8.0.0 bug in README.md ([#&#8203;7614](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7614)) - feat: add cast_spaces into `@PER-CS2.0` ([#&#8203;7625](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7625)) - feat: Configurable phpDoc tags for FQCN processing ([#&#8203;7628](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7628)) - feat: StatementIndentationFixer - introduce stick_comment_to_next_continuous_control_statement config ([#&#8203;7624](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7624)) - feat: UnaryOperatorSpacesFixer - introduce only_dec_inc config ([#&#8203;7626](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7626)) - fix: FullyQualifiedStrictTypesFixer - better support annotations in inline {} ([#&#8203;7633](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7633)) - fix: Improve how FQCN is handled in phpDoc ([#&#8203;7622](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7622)) - fix: phpdoc_align - fix multiline tag alignment issue ([#&#8203;7630](https://togithub.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7630)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/libretime/libretime). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMDMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjEwMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
2023-12-29 15:28:57 +01:00
* when looking for the "first" admin
*
2021-10-11 16:10:47 +02:00
* @return null|CcSubj
*/
2021-10-11 16:10:47 +02:00
public static function getFirstAdmin($ignoreUser = null)
{
$superAdmins = Application_Model_User::getUsersOfType('S');
if (count($superAdmins) > 0) { // found superadmin => pick first one
return $superAdmins[0];
2012-09-18 21:26:43 +02:00
}
2021-10-11 16:10:47 +02:00
// get all admin users
$query = CcSubjsQuery::create()->filterByDbType('A');
// ignore current user if one was specified
if ($ignoreUser !== null) {
$query->filterByDbId($ignoreUser, Criteria::NOT_EQUAL);
}
$admins = $query->find();
if (count($admins) > 0) { // found admin => pick first one
return $admins[0];
}
Logging::warn('Warning. no admins found in database');
return null;
2012-09-18 21:26:43 +02:00
}
2021-10-11 16:10:47 +02:00
public static function getUsers(array $type, $search = null)
{
2021-10-11 16:10:47 +02:00
$con = Propel::getConnection();
2021-10-11 16:10:47 +02:00
$sql_gen = 'SELECT login AS value, login AS label, id as index FROM cc_subjs ';
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
$types = [];
$params = [];
for ($i = 0; $i < count($type); ++$i) {
$p = ":type{$i}";
2021-10-11 16:10:47 +02:00
$types[] = "type = {$p}";
$params[$p] = $type[$i];
}
2012-08-23 20:44:14 +02:00
$sql_type = implode(' OR ', $types);
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
$sql = $sql_gen . ' WHERE (' . $sql_type . ') ';
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
$sql .= ' AND login ILIKE :search';
$params[':search'] = "%{$search}%";
$sql .= ' ORDER BY login';
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
return Application_Common_Database::prepareAndExecute($sql, $params, 'all');
}
2012-01-16 09:56:57 +01:00
2012-09-06 18:23:53 +02:00
public static function getUserCount()
{
2021-10-11 16:10:47 +02:00
$sql_gen = 'SELECT count(*) AS cnt FROM cc_subjs';
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
$query = Application_Common_Database::prepareAndExecute(
$sql_gen,
[],
Application_Common_Database::COLUMN
);
return ($query !== false) ? $query : null;
}
2021-10-11 16:10:47 +02:00
public static function getHosts($search = null)
{
2021-10-11 16:10:47 +02:00
return Application_Model_User::getUsers(['H'], $search);
}
2012-01-16 09:56:57 +01:00
2021-10-11 16:10:47 +02:00
public static function getNonGuestUsers($search = null)
{
2021-10-11 16:10:47 +02:00
return Application_Model_User::getUsers(['H', 'A', 'S', 'P'], $search);
}
public static function getUsersDataTablesInfo($datatables)
{
$con = Propel::getConnection(CcSubjsPeer::DATABASE_NAME);
2021-10-11 16:10:47 +02:00
$displayColumns = ['id', 'login', 'first_name', 'last_name', 'type'];
$fromTable = 'cc_subjs';
2012-01-16 09:56:57 +01:00
// get current user
2021-10-11 16:10:47 +02:00
$username = '';
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$username = $auth->getIdentity()->login;
}
2012-01-16 09:56:57 +01:00
$res = Application_Model_Datatables::findEntries($con, $displayColumns, $fromTable, $datatables);
// mark record which is for the current user
2021-10-11 16:10:47 +02:00
foreach ($res['aaData'] as $key => &$record) {
if ($record['login'] == $username) {
2021-10-11 16:10:47 +02:00
$record['delete'] = 'self';
} else {
2021-10-11 16:10:47 +02:00
$record['delete'] = '';
}
2021-10-11 16:10:47 +02:00
if ($record['login'] == 'sourcefabric_admin') {
// arrays in PHP are basically associative arrays that can be iterated in order.
// Deleting an earlier element does not change the keys of elements that come after it. --MK
2012-10-24 00:47:15 +02:00
unset($res['aaData'][$key]);
2021-10-11 16:10:47 +02:00
--$res['iTotalDisplayRecords'];
--$res['iTotalRecords'];
2012-10-24 00:47:15 +02:00
}
$record = array_map('htmlspecialchars', $record);
}
2012-01-16 09:56:57 +01:00
2012-10-24 00:47:15 +02:00
$res['aaData'] = array_values($res['aaData']);
2021-10-11 16:10:47 +02:00
return $res;
}
2012-01-16 09:56:57 +01:00
public static function getUserData($id)
{
2021-10-11 16:10:47 +02:00
$sql = <<<'SQL'
2012-09-14 23:09:05 +02:00
SELECT login, first_name, last_name, type, id, email, cell_phone, skype_contact,
jabber_contact
FROM cc_subjs
WHERE id = :id
SQL;
2021-10-11 16:10:47 +02:00
return Application_Common_Database::prepareAndExecute($sql, [
2022-07-07 20:01:15 +02:00
':id' => $id,
], 'single');
2011-02-09 19:03:46 +01:00
}
2012-01-16 09:56:57 +01:00
public static function getCurrentUser()
{
$userinfo = Zend_Auth::getInstance()->getStorage()->read();
if (is_null($userinfo)) {
return null;
}
2021-10-11 16:10:47 +02:00
try {
return new self($userinfo->id);
} catch (Exception $e) {
// we get here if $userinfo->id is defined, but doesn't exist
// in the database anymore.
Zend_Auth::getInstance()->clearIdentity();
return null;
}
}
}