ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilUserPasswordEncoderFactory.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/User/exceptions/class.ilUserException.php';
5 
12 {
16  protected $default_encoder;
17 
21  protected $encoders = array();
22 
26  public function __construct(array $config = array())
27  {
28  if(!empty($config))
29  {
30  foreach($config as $key => $value)
31  {
32  switch(strtolower($key))
33  {
34  case 'default_password_encoder':
35  $this->setDefaultEncoder($value);
36  break;
37  }
38  }
39  }
40 
41  $this->initEncoders($config);
42  }
43 
47  protected function initEncoders(array $config)
48  {
49  $encoder_directory = 'Services/Password/classes/encoders';
50  foreach(new DirectoryIterator($encoder_directory) as $file)
51  {
55  if($file->isDir())
56  {
57  continue;
58  }
59 
60  require_once $file->getPathname();
61  $class_name = preg_replace('/(class\.)(.*?)(\.php)/', '$2', $file->getBasename());
62  if(class_exists($class_name))
63  {
64  $reflection = new ReflectionClass($class_name);
65  $encoder = $reflection->newInstanceArgs(array($config));
66  if($encoder instanceof ilPasswordEncoder && $encoder->isSupportedByRuntime())
67  {
68  $this->encoders[$encoder->getName()] = $encoder;
69  }
70  }
71  }
72  }
73 
77  public function getDefaultEncoder()
78  {
80  }
81 
86  {
87  $this->default_encoder = $default_encoder;
88  }
89 
93  public function getEncoders()
94  {
95  return $this->encoders;
96  }
97 
102  public function setEncoders(array $encoders)
103  {
104  $this->encoders = array();
105  foreach($encoders as $encoder)
106  {
107  if(!($encoder instanceof ilPasswordEncoder))
108  {
109  throw new ilUserException(sprintf('One of the passed encoders is not valid: %s.', json_encode($encoder)));
110  }
111  $this->encoders[$encoder->getName()] = $encoder;
112  }
113  }
114 
118  public function getSupportedEncoderNames()
119  {
120  return array_keys($this->getEncoders());
121  }
122 
129  public function getEncoderByName($name, $get_default_on_mismatch = false)
130  {
131  if(!isset($this->encoders[$name]))
132  {
133  if(!$get_default_on_mismatch)
134  {
135  throw new ilUserException(sprintf('The encoder "%s" was not configured.', $name));
136  }
137  else if(!$this->getDefaultEncoder())
138  {
139  throw new ilUserException('No default encoder specified, fallback not possible.');
140  }
141  else if(!isset($this->encoders[$this->getDefaultEncoder()]))
142  {
143  throw new ilUserException("No default encoder found for name: '{$this->getDefaultEncoder()}'.");
144  }
145 
146  return $this->encoders[$this->getDefaultEncoder()];
147  }
148 
149  return $this->encoders[$name];
150  }
151 }
Class for user related exception handling in ILIAS.
isSupportedByRuntime()
Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)
Create styles array
The data for the language used.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
getEncoderByName($name, $get_default_on_mismatch=false)