vendor/contao/core-bundle/src/Resources/contao/dca/tl_member.php line 448

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. use Contao\Backend;
  10. use Contao\BackendUser;
  11. use Contao\Config;
  12. use Contao\CoreBundle\EventListener\Widget\HttpUrlListener;
  13. use Contao\DataContainer;
  14. use Contao\DC_Table;
  15. use Contao\FrontendUser;
  16. use Contao\Image;
  17. use Contao\MemberGroupModel;
  18. use Contao\MemberModel;
  19. use Contao\StringUtil;
  20. use Contao\System;
  21. $GLOBALS['TL_DCA']['tl_member'] = array
  22. (
  23.     // Config
  24.     'config' => array
  25.     (
  26.         'dataContainer'               => DC_Table::class,
  27.         'enableVersioning'            => true,
  28.         'onsubmit_callback' => array
  29.         (
  30.             array('tl_member', 'storeDateAdded')
  31.         ),
  32.         'sql' => array
  33.         (
  34.             'keys' => array
  35.             (
  36.                 'id' => 'primary',
  37.                 'username' => 'unique',
  38.                 'email' => 'index',
  39.                 'login,disable,start,stop' => 'index'
  40.             )
  41.         )
  42.     ),
  43.     // List
  44.     'list' => array
  45.     (
  46.         'sorting' => array
  47.         (
  48.             'mode'                    => DataContainer::MODE_SORTABLE,
  49.             'fields'                  => array('dateAdded'),
  50.             'panelLayout'             => 'filter;sort,search,limit'
  51.         ),
  52.         'label' => array
  53.         (
  54.             'fields'                  => array('', 'firstname', 'lastname', 'username', 'dateAdded'),
  55.             'showColumns'             => true,
  56.             'label_callback'          => array('tl_member', 'addIcon')
  57.         ),
  58.         'global_operations' => array
  59.         (
  60.             'all' => array
  61.             (
  62.                 'href'                => 'act=select',
  63.                 'class'               => 'header_edit_all',
  64.                 'attributes'          => 'onclick="Backend.getScrollOffset()" accesskey="e"'
  65.             )
  66.         ),
  67.         'operations' => array
  68.         (
  69.             'edit' => array
  70.             (
  71.                 'href'                => 'act=edit',
  72.                 'icon'                => 'edit.svg'
  73.             ),
  74.             'copy' => array
  75.             (
  76.                 'href'                => 'act=copy',
  77.                 'icon'                => 'copy.svg'
  78.             ),
  79.             'delete' => array
  80.             (
  81.                 'href'                => 'act=delete',
  82.                 'icon'                => 'delete.svg',
  83.                 'attributes'          => 'onclick="if(!confirm(\'' . ($GLOBALS['TL_LANG']['MSC']['deleteConfirm'] ?? null) . '\'))return false;Backend.getScrollOffset()"'
  84.             ),
  85.             'toggle' => array
  86.             (
  87.                 'href'                => 'act=toggle&amp;field=disable',
  88.                 'icon'                => 'visible.svg',
  89.                 'reverse'             => true
  90.             ),
  91.             'show' => array
  92.             (
  93.                 'href'                => 'act=show',
  94.                 'icon'                => 'show.svg'
  95.             ),
  96.             'su' => array
  97.             (
  98.                 'href'                => 'key=su',
  99.                 'icon'                => 'su.svg',
  100.                 'button_callback'     => array('tl_member', 'switchUser')
  101.             )
  102.         )
  103.     ),
  104.     // Palettes
  105.     'palettes' => array
  106.     (
  107.         '__selector__'                => array('login', 'assignDir'),
  108.         'default'                     => '{personal_legend},firstname,lastname,dateOfBirth,gender;{address_legend:hide},company,street,postal,city,state,country;{contact_legend},phone,mobile,fax,email,website,language;{groups_legend},groups;{login_legend},login;{homedir_legend:hide},assignDir;{account_legend},disable,start,stop',
  109.     ),
  110.     // Subpalettes
  111.     'subpalettes' => array
  112.     (
  113.         'login'                       => 'username,password',
  114.         'assignDir'                   => 'homeDir'
  115.     ),
  116.     // Fields
  117.     'fields' => array
  118.     (
  119.         'id' => array
  120.         (
  121.             'sql'                     => "int(10) unsigned NOT NULL auto_increment",
  122.             'search'                  => true
  123.         ),
  124.         'tstamp' => array
  125.         (
  126.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  127.         ),
  128.         'firstname' => array
  129.         (
  130.             'exclude'                 => true,
  131.             'search'                  => true,
  132.             'sorting'                 => true,
  133.             'flag'                    => DataContainer::SORT_INITIAL_LETTER_ASC,
  134.             'inputType'               => 'text',
  135.             'eval'                    => array('mandatory'=>true, 'maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'tl_class'=>'w50'),
  136.             'sql'                     => "varchar(255) NOT NULL default ''"
  137.         ),
  138.         'lastname' => array
  139.         (
  140.             'exclude'                 => true,
  141.             'search'                  => true,
  142.             'sorting'                 => true,
  143.             'flag'                    => DataContainer::SORT_INITIAL_LETTER_ASC,
  144.             'inputType'               => 'text',
  145.             'eval'                    => array('mandatory'=>true, 'maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'tl_class'=>'w50'),
  146.             'sql'                     => "varchar(255) NOT NULL default ''"
  147.         ),
  148.         'dateOfBirth' => array
  149.         (
  150.             'exclude'                 => true,
  151.             'inputType'               => 'text',
  152.             'eval'                    => array('rgxp'=>'date', 'datepicker'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'tl_class'=>'w50 wizard'),
  153.             'sql'                     => "varchar(11) NOT NULL default ''"
  154.         ),
  155.         'gender' => array
  156.         (
  157.             'exclude'                 => true,
  158.             'inputType'               => 'select',
  159.             'options'                 => array('male', 'female', 'other'),
  160.             'reference'               => &$GLOBALS['TL_LANG']['MSC'],
  161.             'eval'                    => array('includeBlankOption'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'tl_class'=>'w50'),
  162.             'sql'                     => "varchar(32) NOT NULL default ''"
  163.         ),
  164.         'company' => array
  165.         (
  166.             'exclude'                 => true,
  167.             'search'                  => true,
  168.             'sorting'                 => true,
  169.             'flag'                    => DataContainer::SORT_INITIAL_LETTER_ASC,
  170.             'inputType'               => 'text',
  171.             'eval'                    => array('maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  172.             'sql'                     => "varchar(255) NOT NULL default ''"
  173.         ),
  174.         'street' => array
  175.         (
  176.             'exclude'                 => true,
  177.             'search'                  => true,
  178.             'inputType'               => 'text',
  179.             'eval'                    => array('maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  180.             'sql'                     => "varchar(255) NOT NULL default ''"
  181.         ),
  182.         'postal' => array
  183.         (
  184.             'exclude'                 => true,
  185.             'search'                  => true,
  186.             'inputType'               => 'text',
  187.             'eval'                    => array('maxlength'=>32, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  188.             'sql'                     => "varchar(32) NOT NULL default ''"
  189.         ),
  190.         'city' => array
  191.         (
  192.             'exclude'                 => true,
  193.             'filter'                  => true,
  194.             'search'                  => true,
  195.             'sorting'                 => true,
  196.             'inputType'               => 'text',
  197.             'eval'                    => array('maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  198.             'sql'                     => "varchar(255) NOT NULL default ''"
  199.         ),
  200.         'state' => array
  201.         (
  202.             'exclude'                 => true,
  203.             'sorting'                 => true,
  204.             'inputType'               => 'text',
  205.             'eval'                    => array('maxlength'=>64, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  206.             'sql'                     => "varchar(64) NOT NULL default ''"
  207.         ),
  208.         'country' => array
  209.         (
  210.             'exclude'                 => true,
  211.             'filter'                  => true,
  212.             'sorting'                 => true,
  213.             'inputType'               => 'select',
  214.             'eval'                    => array('includeBlankOption'=>true, 'chosen'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50'),
  215.             'options_callback' => static function ()
  216.             {
  217.                 $countries = System::getContainer()->get('contao.intl.countries')->getCountries();
  218.                 // Convert to lower case for backwards compatibility, to be changed in Contao 5.0
  219.                 return array_combine(array_map('strtolower', array_keys($countries)), $countries);
  220.             },
  221.             'sql'                     => "varchar(2) NOT NULL default ''"
  222.         ),
  223.         'phone' => array
  224.         (
  225.             'exclude'                 => true,
  226.             'search'                  => true,
  227.             'inputType'               => 'text',
  228.             'eval'                    => array('maxlength'=>64, 'rgxp'=>'phone', 'decodeEntities'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact', 'tl_class'=>'w50'),
  229.             'sql'                     => "varchar(64) NOT NULL default ''"
  230.         ),
  231.         'mobile' => array
  232.         (
  233.             'exclude'                 => true,
  234.             'search'                  => true,
  235.             'inputType'               => 'text',
  236.             'eval'                    => array('maxlength'=>64, 'rgxp'=>'phone', 'decodeEntities'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact', 'tl_class'=>'w50'),
  237.             'sql'                     => "varchar(64) NOT NULL default ''"
  238.         ),
  239.         'fax' => array
  240.         (
  241.             'exclude'                 => true,
  242.             'search'                  => true,
  243.             'inputType'               => 'text',
  244.             'eval'                    => array('maxlength'=>64, 'rgxp'=>'phone', 'decodeEntities'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact', 'tl_class'=>'w50'),
  245.             'sql'                     => "varchar(64) NOT NULL default ''"
  246.         ),
  247.         'email' => array
  248.         (
  249.             'exclude'                 => true,
  250.             'search'                  => true,
  251.             'inputType'               => 'text',
  252.             'eval'                    => array('mandatory'=>true, 'maxlength'=>255, 'rgxp'=>'email', 'unique'=>true, 'decodeEntities'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact', 'tl_class'=>'w50'),
  253.             'sql'                     => "varchar(255) NOT NULL default ''"
  254.         ),
  255.         'website' => array
  256.         (
  257.             'exclude'                 => true,
  258.             'search'                  => true,
  259.             'inputType'               => 'text',
  260.             'eval'                    => array('rgxp'=>HttpUrlListener::RGXP_NAME, 'maxlength'=>255, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact', 'tl_class'=>'w50'),
  261.             'sql'                     => "varchar(255) NOT NULL default ''"
  262.         ),
  263.         'language' => array
  264.         (
  265.             'exclude'                 => true,
  266.             'filter'                  => true,
  267.             'inputType'               => 'select',
  268.             'eval'                    => array('includeBlankOption'=>true, 'chosen'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'personal', 'tl_class'=>'w50'),
  269.             'options_callback' => static function ()
  270.             {
  271.                 return System::getContainer()->get('contao.intl.locales')->getLocales(null, false);
  272.             },
  273.             'sql'                     => "varchar(64) NOT NULL default ''"
  274.         ),
  275.         'groups' => array
  276.         (
  277.             'exclude'                 => true,
  278.             'filter'                  => true,
  279.             'inputType'               => 'checkboxWizard',
  280.             'foreignKey'              => 'tl_member_group.name',
  281.             'eval'                    => array('multiple'=>true, 'feEditable'=>true, 'feGroup'=>'login'),
  282.             'sql'                     => "blob NULL",
  283.             'relation'                => array('type'=>'belongsToMany', 'load'=>'lazy')
  284.         ),
  285.         'login' => array
  286.         (
  287.             'exclude'                 => true,
  288.             'filter'                  => true,
  289.             'inputType'               => 'checkbox',
  290.             'eval'                    => array('submitOnChange'=>true),
  291.             'sql'                     => "char(1) NOT NULL default ''"
  292.         ),
  293.         'username' => array
  294.         (
  295.             'exclude'                 => true,
  296.             'search'                  => true,
  297.             'sorting'                 => true,
  298.             'flag'                    => DataContainer::SORT_INITIAL_LETTER_ASC,
  299.             'inputType'               => 'text',
  300.             'eval'                    => array('mandatory'=>true, 'unique'=>true, 'rgxp'=>'extnd', 'nospace'=>true, 'maxlength'=>64, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'login', 'tl_class'=>'w50', 'autocapitalize'=>'off', 'autocomplete'=>'username'),
  301.             'sql'                     => 'varchar(64) BINARY NULL'
  302.         ),
  303.         'password' => array
  304.         (
  305.             'label'                   => &$GLOBALS['TL_LANG']['MSC']['password'],
  306.             'exclude'                 => true,
  307.             'inputType'               => 'password',
  308.             'eval'                    => array('mandatory'=>true, 'preserveTags'=>true, 'minlength'=>Config::get('minPasswordLength'), 'feEditable'=>true, 'feGroup'=>'login', 'tl_class'=>'w50'),
  309.             'save_callback' => array
  310.             (
  311.                 array('tl_member', 'setNewPassword')
  312.             ),
  313.             'sql'                     => "varchar(255) NOT NULL default ''"
  314.         ),
  315.         'assignDir' => array
  316.         (
  317.             'exclude'                 => true,
  318.             'inputType'               => 'checkbox',
  319.             'eval'                    => array('submitOnChange'=>true),
  320.             'sql'                     => "char(1) NOT NULL default ''"
  321.         ),
  322.         'homeDir' => array
  323.         (
  324.             'exclude'                 => true,
  325.             'inputType'               => 'fileTree',
  326.             'eval'                    => array('fieldType'=>'radio', 'tl_class'=>'clr'),
  327.             'sql'                     => "binary(16) NULL"
  328.         ),
  329.         'disable' => array
  330.         (
  331.             'exclude'                 => true,
  332.             'toggle'                  => true,
  333.             'filter'                  => true,
  334.             'inputType'               => 'checkbox',
  335.             'sql'                     => "char(1) NOT NULL default ''"
  336.         ),
  337.         'start' => array
  338.         (
  339.             'exclude'                 => true,
  340.             'inputType'               => 'text',
  341.             'eval'                    => array('rgxp'=>'datim', 'datepicker'=>true, 'tl_class'=>'w50 wizard'),
  342.             'sql'                     => "varchar(10) NOT NULL default ''"
  343.         ),
  344.         'stop' => array
  345.         (
  346.             'exclude'                 => true,
  347.             'inputType'               => 'text',
  348.             'eval'                    => array('rgxp'=>'datim', 'datepicker'=>true, 'tl_class'=>'w50 wizard'),
  349.             'sql'                     => "varchar(10) NOT NULL default ''"
  350.         ),
  351.         'dateAdded' => array
  352.         (
  353.             'label'                   => &$GLOBALS['TL_LANG']['MSC']['dateAdded'],
  354.             'default'                 => time(),
  355.             'sorting'                 => true,
  356.             'flag'                    => DataContainer::SORT_DAY_DESC,
  357.             'eval'                    => array('rgxp'=>'datim', 'doNotCopy'=>true),
  358.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  359.         ),
  360.         'lastLogin' => array
  361.         (
  362.             'label'                   => &$GLOBALS['TL_LANG']['MSC']['lastLogin'],
  363.             'eval'                    => array('rgxp'=>'datim', 'doNotCopy'=>true),
  364.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  365.         ),
  366.         'currentLogin' => array
  367.         (
  368.             'label'                   => &$GLOBALS['TL_LANG']['MSC']['currentLogin'],
  369.             'sorting'                 => true,
  370.             'flag'                    => DataContainer::SORT_DAY_DESC,
  371.             'eval'                    => array('rgxp'=>'datim', 'doNotCopy'=>true),
  372.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  373.         ),
  374.         'loginAttempts' => array
  375.         (
  376.             'eval'                    => array('doNotCopy'=>true),
  377.             'sql'                     => "smallint(5) unsigned NOT NULL default 0"
  378.         ),
  379.         'locked' => array
  380.         (
  381.             'eval'                    => array('rgxp'=>'datim', 'doNotCopy'=>true),
  382.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  383.         ),
  384.         'session' => array
  385.         (
  386.             'eval'                    => array('doNotShow'=>true, 'doNotCopy'=>true),
  387.             'sql'                     => "blob NULL"
  388.         ),
  389.         'secret' => array
  390.         (
  391.             'eval'                    => array('doNotShow'=>true, 'doNotCopy'=>true),
  392.             'sql'                     => "binary(128) NULL default NULL"
  393.         ),
  394.         'useTwoFactor' => array
  395.         (
  396.             'eval'                    => array('isBoolean'=>true, 'doNotCopy'=>true, 'tl_class'=>'w50 m12'),
  397.             'sql'                     => "char(1) NOT NULL default ''"
  398.         ),
  399.         'backupCodes' => array
  400.         (
  401.             'eval'                    => array('doNotCopy'=>true, 'doNotShow'=>true),
  402.             'sql'                     => "text NULL"
  403.         ),
  404.         'trustedTokenVersion' => array
  405.         (
  406.             'eval'                    => array('doNotCopy'=>true, 'doNotShow'=>true),
  407.             'sql'                     => "int(10) unsigned NOT NULL default 0"
  408.         )
  409.     )
  410. );
  411. // Filter disabled groups in the front end (see #6757)
  412. if (defined('TL_MODE') && TL_MODE == 'FE')
  413. {
  414.     $GLOBALS['TL_DCA']['tl_member']['fields']['groups']['options_callback'] = array('tl_member', 'getActiveGroups');
  415. }
  416. /**
  417.  * Provide miscellaneous methods that are used by the data configuration array.
  418.  */
  419. class tl_member extends Backend
  420. {
  421.     /**
  422.      * Import the back end user object
  423.      */
  424.     public function __construct()
  425.     {
  426.         parent::__construct();
  427.         $this->import(BackendUser::class, 'User');
  428.     }
  429.     /**
  430.      * Filter disabled groups
  431.      *
  432.      * @return array
  433.      */
  434.     public function getActiveGroups()
  435.     {
  436.         $arrGroups = array();
  437.         $objGroup = MemberGroupModel::findAllActive();
  438.         if ($objGroup !== null)
  439.         {
  440.             while ($objGroup->next())
  441.             {
  442.                 $arrGroups[$objGroup->id] = $objGroup->name;
  443.             }
  444.         }
  445.         return $arrGroups;
  446.     }
  447.     /**
  448.      * Add an image to each record
  449.      *
  450.      * @param array         $row
  451.      * @param string        $label
  452.      * @param DataContainer $dc
  453.      * @param array         $args
  454.      *
  455.      * @return array
  456.      */
  457.     public function addIcon($row, $label, DataContainer $dc, $args)
  458.     {
  459.         $image = 'member';
  460.         $disabled = ($row['start'] !== '' && $row['start'] > time()) || ($row['stop'] !== '' && $row['stop'] <= time());
  461.         if ($row['useTwoFactor'])
  462.         {
  463.             $image .= '_two_factor';
  464.         }
  465.         if ($disabled || $row['disable'])
  466.         {
  467.             $image .= '_';
  468.         }
  469.         $args[0] = sprintf(
  470.             '<div class="list_icon_new" style="background-image:url(\'%s\')" data-icon="%s" data-icon-disabled="%s">&nbsp;</div>',
  471.             Image::getPath($image),
  472.             Image::getPath($disabled ? $image : rtrim($image, '_')),
  473.             Image::getPath(rtrim($image, '_') . '_')
  474.         );
  475.         return $args;
  476.     }
  477.     /**
  478.      * Generate a "switch account" button and return it as string
  479.      *
  480.      * @param array  $row
  481.      * @param string $href
  482.      * @param string $label
  483.      * @param string $title
  484.      * @param string $icon
  485.      *
  486.      * @return string
  487.      */
  488.     public function switchUser($row, $href, $label, $title, $icon)
  489.     {
  490.         $blnCanSwitchUser = ($this->User->isAdmin || (!empty($this->User->amg) && is_array($this->User->amg)));
  491.         if (!$blnCanSwitchUser)
  492.         {
  493.             return '';
  494.         }
  495.         if (!$row['login'] || !$row['username'] || (!$this->User->isAdmin && count(array_intersect(StringUtil::deserialize($row['groups'], true), $this->User->amg)) < 1))
  496.         {
  497.             return Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)) . ' ';
  498.         }
  499.         $url = System::getContainer()->get('router')->generate('contao_backend_preview', array('user'=>$row['username']));
  500.         return '<a href="' . StringUtil::specialcharsUrl($url) . '" title="' . StringUtil::specialchars($title) . '" target="_blank">' . Image::getHtml($icon, $label) . '</a> ';
  501.     }
  502.     /**
  503.      * Call the "setNewPassword" callback
  504.      *
  505.      * @param string                    $strPassword
  506.      * @param DataContainer|MemberModel $user
  507.      *
  508.      * @return string
  509.      */
  510.     public function setNewPassword($strPassword, $user)
  511.     {
  512.         // Return if there is no user (e.g. upon registration)
  513.         if (!$user)
  514.         {
  515.             return $strPassword;
  516.         }
  517.         $objUser = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")
  518.                                   ->limit(1)
  519.                                   ->execute($user->id);
  520.         // HOOK: set new password callback
  521.         if ($objUser->numRows && isset($GLOBALS['TL_HOOKS']['setNewPassword']) && is_array($GLOBALS['TL_HOOKS']['setNewPassword']))
  522.         {
  523.             foreach ($GLOBALS['TL_HOOKS']['setNewPassword'] as $callback)
  524.             {
  525.                 $this->import($callback[0]);
  526.                 $this->{$callback[0]}->{$callback[1]}($objUser, $strPassword);
  527.             }
  528.         }
  529.         return $strPassword;
  530.     }
  531.     /**
  532.      * Store the date when the account has been added
  533.      *
  534.      * @param DataContainer|FrontendUser $dc
  535.      */
  536.     public function storeDateAdded($dc)
  537.     {
  538.         // Front end call
  539.         if (!$dc instanceof DataContainer)
  540.         {
  541.             return;
  542.         }
  543.         // Return if there is no active record (override all)
  544.         if (!$dc->activeRecord || $dc->activeRecord->dateAdded > 0)
  545.         {
  546.             return;
  547.         }
  548.         // Fallback solution for existing accounts
  549.         if ($dc->activeRecord->lastLogin > 0)
  550.         {
  551.             $time = $dc->activeRecord->lastLogin;
  552.         }
  553.         else
  554.         {
  555.             $time = time();
  556.         }
  557.         $this->Database->prepare("UPDATE tl_member SET dateAdded=? WHERE id=?")
  558.                        ->execute($time, $dc->id);
  559.     }
  560. }