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
<?php
2

    
3
/**
4
 * @file
5
 * Class to encapsulate an ldap entry to authorization consumer ids mapping configuration.
6
 *
7
 * This is the lightweight version of the class for use on logon etc.
8
 * the LdapAuthorizationConsumerConfAdmin extends this class and has save,
9
 * iterate, etc methods.
10
 */
11

    
12
/**
13
 * LDAP Authorization Consumer Configuration.
14
 */
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
  public $mappings = [];
32
  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
  /**
47
   *
48
   */
49
  public function clearError() {
50
    $this->hasError = FALSE;
51
    $this->errorMsg = NULL;
52
    $this->errorName = NULL;
53
  }
54

    
55
  /**
56
   * Constructor Method.
57
   */
58
  public function __construct(&$consumer, $_new = FALSE, $_sid = NULL) {
59
    $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
        watchdog('ldap_authorization', 'failed to load existing %consumer object', ['%consumer' => $consumer->consumerType], WATCHDOG_ERROR);
69
      }
70
    }
71
    // Default value for deriveFromEntryAttrMatchingUserAttr set up this way for backward compatibility in 1.0 branch,
72
    // make deriveFromEntryAttrMatchingUserAttr default to dn in 2.0 branch.
73
  }
74

    
75
  /**
76
   *
77
   */
78
  protected function loadFromDb() {
79
    if (module_exists('ctools')) {
80
      ctools_include('export');
81
      $result = ctools_export_load_object('ldap_authorization', 'names', [$this->consumerType]);
82

    
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
      $select->condition('ldap_authorization.consumer_type', $this->consumerType);
92
      $server_record = $select->execute()->fetchObject();
93
    }
94

    
95
    if (!$server_record) {
96
      $this->inDatabase = FALSE;
97
      return FALSE;
98
    }
99

    
100
    foreach ($this->field_to_properties_map() as $db_field_name => $property_name) {
101
      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
    $this->numericConsumerConfId = isset($server_record->numeric_consumer_conf_id) ? $server_record->numeric_consumer_conf_id : NULL;
111
    $this->server = ldap_servers_get_servers($this->sid, NULL, TRUE);
112
    return TRUE;
113

    
114
  }
115

    
116
  /**
117
   * Direct mapping of db to object properties.
118
   */
119
  public static function field_to_properties_map() {
120
    return [
121
      'sid' => 'sid',
122
      'consumer_type' => 'consumerType',
123
      'numeric_consumer_conf_id'  => 'numericConsumerConfId',
124
      '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
    ];
135
  }
136

    
137
  /**
138
   *
139
   */
140
  public static function field_to_properties_serialized() {
141
    return ['mappings'];
142
  }
143

    
144
  /**
145
   * Destructor Method.
146
   */
147
  public function __destruct() {
148

    
149
  }
150

    
151
  protected $_sid;
152
  protected $_new;
153

    
154
  /**
155
   *
156
   */
157
  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
      $array = [];
168
    }
169
    return $array;
170
  }
171

    
172
  /**
173
   *
174
   */
175
  protected function pipeListToArray($mapping_list_txt, $make_item0_lowercase = FALSE) {
176
    $result_array = [];
177
    $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
        $result_array[] = [$item_0, trim($mapping[1])];
182
      }
183
    }
184
    return $result_array;
185
  }
186

    
187
}