Projet

Général

Profil

Paste
Télécharger (5,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / LdapAuthorizationConsumerConf.class.php @ 91af538d

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 bc175c27 Assos Assos
 * Class to encapsulate an ldap entry to authorization consumer ids mapping configuration.
6 85ad3d82 Assos Assos
 *
7 bc175c27 Assos Assos
 * This is the lightweight version of the class for use on logon etc.
8 85ad3d82 Assos Assos
 * the LdapAuthorizationConsumerConfAdmin extends this class and has save,
9
 * iterate, etc methods.
10
 */
11
12
/**
13 bc175c27 Assos Assos
 * LDAP Authorization Consumer Configuration.
14 85ad3d82 Assos Assos
 */
15
class LdapAuthorizationConsumerConf {
16
17
  public $sid = NULL;
18
  public $server;
19
  public $consumerType = NULL;
20
  public $consumerModule = NULL;
21
  public $consumer = NULL;
22
  public $inDatabase = FALSE;
23
  public $numericConsumerConfId = NULL;
24
25
  public $description = NULL;
26
  public $status = NULL;
27
  public $onlyApplyToLdapAuthenticated = TRUE;
28
29
  public $useFirstAttrAsGroupId = FALSE;
30
31 32700c57 Assos Assos
  public $mappings = [];
32 85ad3d82 Assos Assos
  public $useMappingsAsFilter = TRUE;
33
34
  public $synchToLdap = FALSE;
35
36
  public $synchOnLogon = TRUE;
37
38
  public $revokeLdapProvisioned = TRUE;
39
  public $regrantLdapProvisioned = TRUE;
40
  public $createConsumers = TRUE;
41
42
  public $errorMsg = NULL;
43
  public $hasError = FALSE;
44
  public $errorName = NULL;
45
46 bc175c27 Assos Assos
  /**
47
   *
48
   */
49 85ad3d82 Assos Assos
  public function clearError() {
50
    $this->hasError = FALSE;
51
    $this->errorMsg = NULL;
52
    $this->errorName = NULL;
53
  }
54 bc175c27 Assos Assos
55
  /**
56
   * Constructor Method.
57 85ad3d82 Assos Assos
   */
58 bc175c27 Assos Assos
  public function __construct(&$consumer, $_new = FALSE, $_sid = NULL) {
59 85ad3d82 Assos Assos
    $this->consumer = $consumer;
60
    $this->consumerType = $consumer->consumerType;
61
    if ($_new) {
62
      $this->inDatabase = FALSE;
63
    }
64
    else {
65
      $this->inDatabase = TRUE;
66
      $exists = $this->loadFromDb();
67
      if (!$exists) {
68 32700c57 Assos Assos
        watchdog('ldap_authorization', 'failed to load existing %consumer object', ['%consumer' => $consumer->consumerType], WATCHDOG_ERROR);
69 85ad3d82 Assos Assos
      }
70
    }
71 bc175c27 Assos Assos
    // Default value for deriveFromEntryAttrMatchingUserAttr set up this way for backward compatibility in 1.0 branch,
72 85ad3d82 Assos Assos
    // make deriveFromEntryAttrMatchingUserAttr default to dn in 2.0 branch.
73
  }
74
75 bc175c27 Assos Assos
  /**
76
   *
77
   */
78 85ad3d82 Assos Assos
  protected function loadFromDb() {
79
    if (module_exists('ctools')) {
80
      ctools_include('export');
81 32700c57 Assos Assos
      $result = ctools_export_load_object('ldap_authorization', 'names', [$this->consumerType]);
82 85ad3d82 Assos Assos
83
      // @todo, this is technically wrong, but I don't quite grok what we're doing in the non-ctools case - justintime
84
      $server_record = array_pop($result);
85
      // There's no ctools api call to get the reserved properties, so instead of hardcoding a list of them
86
      // here, we just grab everything.  Basically, we sacrifice a few bytes of RAM for forward-compatibility.
87
    }
88
    else {
89
      $select = db_select('ldap_authorization', 'ldap_authorization');
90
      $select->fields('ldap_authorization');
91 bc175c27 Assos Assos
      $select->condition('ldap_authorization.consumer_type', $this->consumerType);
92 85ad3d82 Assos Assos
      $server_record = $select->execute()->fetchObject();
93
    }
94
95
    if (!$server_record) {
96
      $this->inDatabase = FALSE;
97
      return FALSE;
98
    }
99
100 bc175c27 Assos Assos
    foreach ($this->field_to_properties_map() as $db_field_name => $property_name) {
101 85ad3d82 Assos Assos
      if (isset($server_record->$db_field_name)) {
102
        if (in_array($db_field_name, $this->field_to_properties_serialized())) {
103
          $this->{$property_name} = unserialize($server_record->$db_field_name);
104
        }
105
        else {
106
          $this->{$property_name} = $server_record->$db_field_name;
107
        }
108
      }
109
    }
110 bc175c27 Assos Assos
    $this->numericConsumerConfId = isset($server_record->numeric_consumer_conf_id) ? $server_record->numeric_consumer_conf_id : NULL;
111 85ad3d82 Assos Assos
    $this->server = ldap_servers_get_servers($this->sid, NULL, TRUE);
112
    return TRUE;
113
114
  }
115
116 bc175c27 Assos Assos
  /**
117
   * Direct mapping of db to object properties.
118
   */
119 85ad3d82 Assos Assos
  public static function field_to_properties_map() {
120 32700c57 Assos Assos
    return [
121 85ad3d82 Assos Assos
      'sid' => 'sid',
122
      'consumer_type' => 'consumerType',
123 32700c57 Assos Assos
      'numeric_consumer_conf_id'  => 'numericConsumerConfId',
124 85ad3d82 Assos Assos
      'status'  => 'status',
125
      'only_ldap_authenticated'  => 'onlyApplyToLdapAuthenticated',
126
      'use_first_attr_as_groupid'  => 'useFirstAttrAsGroupId',
127
      'mappings'  => 'mappings',
128
      'use_filter'  => 'useMappingsAsFilter',
129
      'synch_to_ldap' => 'synchToLdap',
130
      'synch_on_logon'  => 'synchOnLogon',
131
      'regrant_ldap_provisioned'  => 'regrantLdapProvisioned',
132
      'revoke_ldap_provisioned' => 'revokeLdapProvisioned',
133
      'create_consumers'  => 'createConsumers',
134 32700c57 Assos Assos
    ];
135 85ad3d82 Assos Assos
  }
136
137 bc175c27 Assos Assos
  /**
138
   *
139
   */
140 85ad3d82 Assos Assos
  public static function field_to_properties_serialized() {
141 32700c57 Assos Assos
    return ['mappings'];
142 85ad3d82 Assos Assos
  }
143
144
  /**
145 bc175c27 Assos Assos
   * Destructor Method.
146 85ad3d82 Assos Assos
   */
147 bc175c27 Assos Assos
  public function __destruct() {
148 85ad3d82 Assos Assos
149
  }
150
151
  protected $_sid;
152
  protected $_new;
153
154 bc175c27 Assos Assos
  /**
155
   *
156
   */
157 85ad3d82 Assos Assos
  protected function linesToArray($lines) {
158
    $lines = trim($lines);
159
160
    if ($lines) {
161
      $array = preg_split('/[\n\r]+/', $lines);
162
      foreach ($array as $i => $value) {
163
        $array[$i] = trim($value);
164
      }
165
    }
166
    else {
167 32700c57 Assos Assos
      $array = [];
168 85ad3d82 Assos Assos
    }
169
    return $array;
170
  }
171
172 bc175c27 Assos Assos
  /**
173
   *
174
   */
175 85ad3d82 Assos Assos
  protected function pipeListToArray($mapping_list_txt, $make_item0_lowercase = FALSE) {
176 32700c57 Assos Assos
    $result_array = [];
177 85ad3d82 Assos Assos
    $mappings = preg_split('/[\n\r]+/', $mapping_list_txt);
178
    foreach ($mappings as $line) {
179
      if (count($mapping = explode('|', trim($line))) == 2) {
180
        $item_0 = ($make_item0_lowercase) ? drupal_strtolower(trim($mapping[0])) : trim($mapping[0]);
181 32700c57 Assos Assos
        $result_array[] = [$item_0, trim($mapping[1])];
182 85ad3d82 Assos Assos
      }
183
    }
184
    return $result_array;
185
  }
186 bc175c27 Assos Assos
187 85ad3d82 Assos Assos
}