src/Model/Account.php line 383

Open in your IDE?
  1. <?php
  2. namespace App\Model;
  3. use phpDocumentor\Reflection\Types\Boolean;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use phpDocumentor\Reflection\Types\Array_;
  6. use App\Util\Api;
  7. class Account implements \JsonSerializable
  8. {
  9.     const NUMBER_OF_ITEMS 30;
  10.     const NUMBER_OF_ITEMS_MAX5000;
  11.     const NUMBER_OF_LOGINS 10// number of login attempts shown at once
  12.     const NUMBER_OF_OPERATIONS 50// number of login attempts shown at once
  13.     const PASS_COMPLEXITY_BASIC 1;
  14.     const PASS_COMPLEXITY_STRICT 2;
  15.     const PASS_COMPLEXITY_VERY_STRICT 3;
  16.     const PASS_COMPLEXITY_VERY_STRICT_16 4// very strict with 16 characters
  17.     
  18.     const QUOTA_NO_QUOTA 0;
  19.     const QUOTA_INFINITE = -1;
  20.     const QUOTA_NOT_AVAILABLE = -100;
  21.     
  22.     const MAX_USERNAME_LENGTH 32;
  23.     const MAX_FORWARDS_LIMIT 1000
  24.     
  25.     // for admin charts
  26.     const LOAD_AVG_DEFAULT_RANGE 5;
  27.     
  28.     private $uid=0;
  29.     private $gid=0;
  30.     
  31.     /**
  32.      * @Assert\NotBlank() 
  33.      * @Assert\Regex(
  34.         pattern = "/^[a-z0-9]+[a-z0-9-@_.,]*$/",
  35.         message = "account.username.invalid"
  36.      )
  37.      * @Assert\Length(
  38.         min = 2,
  39.         minMessage = "account.username.minLength"
  40.      )
  41.      */
  42.     private $userName;
  43.     
  44.     /* /^[a-z0-9]+[a-z0-9-_.]*$/", */
  45.     /**
  46.      * @Assert\NotBlank()
  47.      * @Assert\Regex(
  48.          pattern = "/^[a-zA-Z0-9][a-zA-Z0-9.-]{0,61}\.[a-zA-Z]{2,}$/", 
  49.          message = "account.userdomain.invalid"
  50.      )
  51.      * @Assert\Length(
  52.          min = 2,
  53.          minMessage = "account.userdomain.minLength"
  54.      )
  55.      */
  56.     private $userDomain;
  57.     private $usedSpace;
  58.     /**
  59.      * TODO kalma sprawdzić jakie są asserty i ustawić, że ma być int, bo Integer nie działa
  60.      */
  61.     private $description;
  62.     private $password;
  63.     private $clearPassword;
  64.     private $directory="";
  65.     private $shell="";
  66.     
  67.     private $allowedServices;
  68.     private $allowedModules;
  69.     
  70.     private $passComplexity;
  71.     private $role;
  72.     // options
  73.     private $sOptions;
  74.     private $inProgress;
  75.     private $aliases;
  76.     // int options
  77.     //private $iOptions;
  78.     
  79.     const TOKEN_SALT 'MMROn4nCRIY5c7no8x0Jtmxtq4aCKS3y';
  80.     const EMPTY_PASSWORD 'j3DACJttjuG3cxxmhP5YfhVwpUNq2LFa';
  81.     
  82.     
  83.     
  84.     public static function getConstQuotaNoQuota()
  85.     {
  86.         return Account::QUOTA_NO_QUOTA;
  87.     }
  88.     
  89.     public static function getConstQuotaNotAvailable()
  90.     {
  91.         return Account::QUOTA_NOT_AVAILABLE;
  92.     }
  93.     
  94.     public static function getConstQuotaInfinite()
  95.     {
  96.         return Account::QUOTA_INFINITE;
  97.     }
  98.     
  99.     public static function getConstSAMaxScoreLimit()
  100.     {
  101.         return UserPref::SA_MAX_SCORE_LIMIT;
  102.     }
  103.     
  104.     public static function getConstItemsMax()
  105.     {
  106.         return Account::NUMBER_OF_ITEMS_MAX;
  107.     }
  108.     
  109.     public static function getConstOperations()
  110.     {
  111.         return Account::NUMBER_OF_OPERATIONS;
  112.     }
  113.     
  114.     public static function getConstMaxForwardsLimit()
  115.     {
  116.         return Account::MAX_FORWARDS_LIMIT;
  117.     }
  118.     
  119.     
  120.         
  121.     public function getGid(): ?int
  122.     {
  123.         return $this->gid;
  124.     }
  125.     
  126.     public function setGid(int $id): self
  127.     {
  128.         $this->gid $id;
  129.         return $this;
  130.     }
  131.     
  132.     public function getUid(): ?int
  133.     {
  134.         return $this->uid;
  135.     }
  136.     
  137.     public function setUid(int $id): self
  138.     {
  139.         $this->uid $id;
  140.         return $this;
  141.     }
  142.     
  143.     public function getUserName(): ?string
  144.     {
  145.         return $this->userName;
  146.     }
  147.     
  148.     public function setUserName(string $username): self
  149.     {
  150.         $this->userName $username;
  151.         
  152.         return $this;
  153.     }
  154.     
  155.     public function getUserDomain(): ?string
  156.     {
  157.         return $this->userDomain;
  158.     }
  159.     
  160.     public function setUserDomain(string $userDomain): self
  161.     {
  162.         $this->userDomain $userDomain;
  163.         
  164.         return $this;
  165.     }
  166.     
  167.     public function getUsedSpace(): ?int
  168.     {
  169.         return $this->usedSpace;
  170.     }
  171.     
  172.     public function setUsedSpace(?int $usedSpace): self
  173.     {
  174.         $this->usedSpace $usedSpace;
  175.         
  176.         return $this;
  177.     }
  178.     
  179.     public function getQuota(): ?int
  180.     {
  181.         return (int)$this->quota;
  182.     }
  183.         
  184.     public function setQuota(?int $quota): self
  185.     {
  186.         $this->quota $quota;
  187.         
  188.         return $this;
  189.     }
  190.     
  191.     public function getDescription(): ?string
  192.     {
  193.         return $this->description;
  194.     }
  195.     
  196.     public function setDescription(string $description): self
  197.     {
  198.         $this->description $description;
  199.         
  200.         return $this;
  201.     }
  202.     
  203.     public function getRole(): ?string
  204.     {
  205.         return $this->role;
  206.     }
  207.     
  208.     public function setRole(string $role): self
  209.     {
  210.         if($role == '') {
  211.             $this->role null;
  212.         }
  213.         else {
  214.             $this->role $role;
  215.         }
  216.         
  217.         return $this;
  218.     }
  219.     
  220.     public function getSOptions(): ?array
  221.     {
  222.         return $this->sOptions;
  223.     }
  224.     
  225.     public function setSOptions(array $options): self
  226.     {
  227.         $this->sOptions $options;
  228.         
  229.         return $this;
  230.     }
  231.     
  232.     /**
  233.      * @return mixed
  234.      */
  235.     public function getAliases()
  236.     {
  237.         return $this->aliases;
  238.     }
  239.     
  240.     /**
  241.      * @param mixed $aliases
  242.      */
  243.     public function setAliases($aliases)
  244.     {
  245.         $this->aliases $aliases;
  246.     }
  247.     /*public function getIOptions(): ?array
  248.     {
  249.         return $this->iOptions;
  250.     }
  251.     
  252.     public function setIOptions(array $options): self
  253.     {
  254.         $this->iOptions = $options;
  255.         
  256.         return $this;
  257.     }*/
  258.     
  259.     public function getPassword(): ?string
  260.     {
  261.         return $this->password;
  262.     }
  263.     
  264.     public function setPassword(string $password): self
  265.     {
  266.         $this->password $password;
  267.         
  268.         return $this;
  269.     }
  270.     
  271.     public function getClearPassword(): ?string
  272.     {
  273.         return $this->clearPassword;
  274.     }
  275.     
  276.     public function setClearPassword(string $password): self
  277.     {
  278.         $this->clearPassword $password;
  279.         
  280.         return $this;
  281.     }
  282.     
  283.     public function getDirectory(): ?string
  284.     {
  285.         return $this->directory;
  286.     }
  287.     
  288.     public function setDirectory(string $directory): self
  289.     {
  290.         $this->directory $directory;
  291.         
  292.         return $this;
  293.     }
  294.     
  295.     public function getShell(): ?string
  296.     {
  297.         return $this->shell;
  298.     }
  299.     
  300.     public function setShell(string $shell): self
  301.     {
  302.         $this->shell $shell;
  303.         
  304.         return $this;
  305.     }
  306.     
  307.     public function getPassComplexity(): ?int
  308.     {
  309.         if($this->passComplexity != 0) {
  310.             return $this->passComplexity;
  311.         }
  312.         else {
  313.             return Account::PASS_COMPLEXITY_STRICT;
  314.         }
  315.     }
  316.     
  317.     public function setPassComplexity(int $passComplexity): self
  318.     {
  319.         $this->passComplexity $passComplexity;
  320.         
  321.         return $this;
  322.     }
  323.     
  324.     public function getAllowedServices(): Array
  325.     {
  326.         return $this->allowedServices;
  327.     }
  328.     
  329.     /**
  330.      *
  331.      * @param array $services - what services are allowed for this user
  332.      * @return self
  333.      */
  334.     public function setAllowedServices(Array $services) : self
  335.     {
  336.         $this->allowedServices $services;
  337.         return $this;
  338.     }
  339.     
  340.     /**
  341.      * what kind of system services can be set as allowed / not allowed
  342.      * remember to change it in qpanel-server as well
  343.      * @return array
  344.      */
  345.     public static function getAllowableServices() : Array
  346.     {
  347.         return ['Pop3''Imap4''Smtp''Webmail'];
  348.     }
  349.     
  350.     public function getAllowedModules(): Array
  351.     {
  352.         return $this->allowedModules;
  353.     }
  354.     /**
  355.      * 
  356.      * @param array $modules - what modules are allowed for this user
  357.      * @return self
  358.      */
  359.     public function setAllowedModules(Array $modules): self
  360.     {
  361.         $this->allowedModules $modules;
  362.         
  363.         return $this;
  364.     }
  365.     /**
  366.      * what kind of modules for user can be set as allowed / not allowed
  367.      * remember to change it in qpanel-server as well
  368.      * @return array
  369.      */
  370.     public static function getAllowableModules() : Array
  371.     {
  372.         return ['PasswordChange''Forward''Autoresponder''SpamAssassin''ImapDirs''ProcMail''NoticeSms'];
  373.     }
  374.     
  375.     public function jsonSerialize()
  376.     {
  377.         $vars get_object_vars($this);
  378.         return $vars;
  379.     }
  380.     
  381.     /**
  382.      * return address of the account in format userName@userDomain
  383.      * @return string - user address
  384.      */
  385.     public function userAddress() : string
  386.     {
  387.         return $this->userName '@' $this->userDomain;
  388.     }
  389.     
  390.     public function setUserAddress($userAddress) : bool
  391.     {
  392.         $arr explode('@'$userAddress);
  393.         if(count($arr) != 2) {
  394.             return false;
  395.         }
  396.         
  397.         $this->userName $arr[0];
  398.         $this->userDomain $arr[1];
  399.         
  400.         return true;
  401.     }
  402.     /**
  403.      * token crated from fields userName and userDomain to prevent URL manipulations
  404.      * @return string token
  405.      */
  406.     public function token() :string
  407.     {
  408.         return md5($this->userName Account::TOKEN_SALT $this->userDomain);
  409.     }
  410.     
  411.     /**
  412.      * lock account
  413.      * @return TRUE - success, FALSE - failure
  414.      */
  415.     public function lock() : bool
  416.     {
  417.         return Api::lockAccount($this);
  418.     }
  419.     
  420.     /**
  421.      * unlock account
  422.      * @return TRUE - success, FALSE - failure
  423.      */
  424.     public function unlock() : bool
  425.     {
  426.         return Api::unlockAccount($this);
  427.     }
  428.     
  429.     /**
  430.      * if account is locked
  431.      * @return bool
  432.      */
  433.     public function isLocked() : bool
  434.     {
  435.         $ret_val preg_match('/^\$A\$/'$this->getPassword());
  436.         
  437.         if ($ret_val == 0)
  438.             $ret_val preg_match('/^\$B\$/'$this->getPassword());
  439.         
  440.         if ($ret_val == 0)
  441.             $ret_val preg_match('/^\$C\$/'$this->getPassword());
  442.         return $ret_val;
  443.     }
  444.     
  445.     /**
  446.      * delete account
  447.      * @return TRUE - success, FALSE - failure
  448.      */
  449.     public function delete() : bool
  450.     {
  451.         return Api::deleteAccount($this);
  452.     }
  453.     
  454.     public function md5() : String
  455.     {
  456.         return md5($this->userName '@' $this->userDomain);
  457.     }
  458.     
  459.     /**
  460.      * returns domains accessible for this account (makes sens for roles POSTMASTER_MANY, POSTMASTER and GROUPMASTER)
  461.      * @return array
  462.      */
  463.     public function roleDomains() : array
  464.     {
  465.         if($this->getRole() == 'ROLE_ADMIN') {
  466.             try {
  467.                 $domains Api::listDomains();
  468.                 
  469.                 if(count($domains) == 0) {
  470.                     return [$this->getuserDomain()];
  471.                 }
  472.                 else {
  473.                     return $domains;
  474.                 }
  475.             }
  476.             catch(\Exception $e) {
  477.                 return [];
  478.             }
  479.         }
  480.         else {
  481.             try {
  482.                 $domains Api::getRoleDomains($this->getUserName(), $this->getuserDomain());
  483.                 
  484.                 if(count($domains) == 0) {
  485.                     return [$this->getuserDomain()];
  486.                 }
  487.                 else {
  488.                     return $domains;
  489.                 }
  490.             }
  491.             catch(\Exception $e) {
  492.                 return [];
  493.             }
  494.         }
  495.     }
  496.     
  497.     /**
  498.      * returns value of the option or throws exception if option not present
  499.      * @param string $name
  500.      * @throws \Exception
  501.      */
  502.     /*public function findIOption(string $name) : int
  503.     {
  504.         if(array_key_exists($name, $this->iOptions)) {
  505.             return $this->iOptions[$name];
  506.         }
  507.         else {
  508.             throw new \Exception('option not found');
  509.         }
  510.     }*/
  511.     
  512.     /**
  513.      * returns value of the option or return null if not found
  514.      * @param string $name or null
  515.      */
  516.     public function findSOption(string $name)
  517.     {
  518.         if(array_key_exists($name$this->sOptions)) {
  519.             return $this->sOptions[$name];
  520.         }
  521.         else {
  522.             return null;
  523.         }
  524.     }
  525.     
  526.     /**
  527.      * is option 'name' set to true / yes 1
  528.      * mainly useful in twig templates
  529.      * @return boolean
  530.      */
  531.     public function isOptionSet($name)
  532.     {
  533.         $val $this->findSOption($name);
  534.         
  535.         if($val == null) {
  536.             return false;
  537.         }
  538.         
  539.         if($val == || $val == 'yes' || $val == 'true') {
  540.             return true;
  541.         }
  542.         
  543.         return false;
  544.     }
  545.     
  546.     public function deleteOption(string $name)
  547.     {
  548.         /*if(array_key_exists($name, $this->iOptions)) {
  549.             unset($this->iOptions[$name]);
  550.         }*/
  551.         
  552.         if(array_key_exists($name$this->sOptions)) {
  553.             unset($this->sOptions[$name]);
  554.         }
  555.     }
  556.     
  557.     /*public function setIOption(string $name, int $val)
  558.     {
  559.         $this->iOptions[$name] = $val;
  560.         return $this;
  561.     }*/
  562.     
  563.     public function setSOption(string $namestring $val)
  564.     {
  565.         $this->sOptions[$name] = $val;
  566.         return $this;
  567.     }
  568.     
  569.     // get state of the infoboxes
  570.     public function getInfoBoxCollapsed() : array
  571.     {
  572.         $ibc $this->findSOption('infoBoxCollapsed') == null '[]' $this->findSOption('infoBoxCollapsed');
  573.         return json_decode($ibctrue);
  574.     }
  575.     
  576.     /**
  577.      * @return bool
  578.      */
  579.     public function getInProgress() : bool
  580.     {
  581.         return $this->inProgress;
  582.     }
  583.     
  584.     /**
  585.      * @param bool $inProgress
  586.      */
  587.     public function setInProgress($inProgress)
  588.     {
  589.         $this->inProgress $inProgress;
  590.     }
  591.     
  592.     // if this account is in progress of adding / deleting
  593.     public function isInProgress() : bool
  594.     {
  595.         // always get fresh, current state
  596.         if($this->userAddress() != '') {
  597.             return Api::isAccountInProgress($this);
  598.         }
  599.         else {
  600.             return false;
  601.         }
  602.     }
  603. }