Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class LDAPMemberExtension extends DataExtension |
||
|
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private static $db = array( |
||
| 13 | // Unique user identifier, same field is used by SAMLMemberExtension |
||
| 14 | 'GUID' => 'Varchar(50)', |
||
| 15 | 'Username' => 'Varchar(64)', |
||
| 16 | 'IsImportedFromLDAP' => 'Boolean', |
||
| 17 | 'IsExpired' => 'Boolean', |
||
| 18 | 'LastSynced' => 'SS_Datetime', |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * These fields are used by {@link LDAPMemberSync} to map specific AD attributes |
||
| 23 | * to {@link Member} fields. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | * @config |
||
| 27 | */ |
||
| 28 | private static $ldap_field_mappings = array( |
||
| 29 | 'givenname' => 'FirstName', |
||
| 30 | 'samaccountname' => 'Username', |
||
| 31 | 'sn' => 'Surname', |
||
| 32 | 'mail' => 'Email', |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The location (relative to /assets) where to save thumbnailphoto data. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | * @config |
||
| 40 | */ |
||
| 41 | private static $ldap_thumbnail_path = 'Uploads'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * When enabled, LDAP managed Member records (IsImportedFromLDAP flag) |
||
| 45 | * have their data written back to LDAP on write. |
||
| 46 | * |
||
| 47 | * This requires setting write permissions on the user configured in the LDAP |
||
| 48 | * credentials, which is why this is disabled by default. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | * @config |
||
| 52 | */ |
||
| 53 | private static $update_ldap_from_local = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If enabled, Member records with a Username field have the user created in LDAP |
||
| 57 | * on write. |
||
| 58 | * |
||
| 59 | * This requires setting write permissions on the user configured in the LDAP |
||
| 60 | * credentials, which is why this is disabled by default. |
||
| 61 | * |
||
| 62 | * Note that some constants must be configured in your environment file: |
||
| 63 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | * @config |
||
| 67 | */ |
||
| 68 | private static $create_users_in_ldap = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
| 72 | * |
||
| 73 | * This requires setting write permissions on the user configured in the LDAP |
||
| 74 | * credentials, which is why this is disabled by default. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | * @config |
||
| 78 | */ |
||
| 79 | private static $delete_users_in_ldap = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param FieldList $fields |
||
| 83 | */ |
||
| 84 | public function updateCMSFields(FieldList $fields) |
||
| 85 | { |
||
| 86 | // Redo LDAP metadata fields as read-only and move to LDAP tab. |
||
| 87 | $ldapMetadata = array(); |
||
| 88 | $fields->replaceField('IsImportedFromLDAP', $ldapMetadata[] = new ReadonlyField( |
||
| 89 | 'IsImportedFromLDAP', |
||
| 90 | _t('LDAPMemberExtension.ISIMPORTEDFROMLDAP', 'Is user imported from LDAP/AD?') |
||
| 91 | )); |
||
| 92 | $fields->replaceField('GUID', $ldapMetadata[] = new ReadonlyField('GUID')); |
||
| 93 | $fields->replaceField('IsExpired', $ldapMetadata[] = new ReadonlyField( |
||
| 94 | 'IsExpired', |
||
| 95 | _t('LDAPMemberExtension.ISEXPIRED', 'Has user\'s LDAP/AD login expired?')) |
||
| 96 | ); |
||
| 97 | $fields->replaceField('LastSynced', $ldapMetadata[] = new ReadonlyField( |
||
| 98 | 'LastSynced', |
||
| 99 | _t('LDAPMemberExtension.LASTSYNCED', 'Last synced')) |
||
| 100 | ); |
||
| 101 | $fields->addFieldsToTab('Root.LDAP', $ldapMetadata); |
||
| 102 | |||
| 103 | $message = ''; |
||
| 104 | if ($this->owner->IsImportedFromLDAP && $this->owner->config()->update_ldap_from_local) { |
||
| 105 | $message = _t('LDAPMemberExtension.CHANGEFIELDSUPDATELDAP', 'Changing fields here will update them in LDAP.'); |
||
| 106 | } elseif ($this->owner->IsImportedFromLDAP && !$this->owner->config()->update_ldap_from_local) { |
||
| 107 | // Transform the automatically mapped fields into read-only. This doesn't |
||
| 108 | // apply if updating LDAP from local is enabled, as changing data locally can be written back. |
||
| 109 | foreach ($this->owner->config()->ldap_field_mappings as $name) { |
||
| 110 | $field = $fields->dataFieldByName($name); |
||
| 111 | if (!empty($field)) { |
||
| 112 | // Set to readonly, but not disabled so that the data is still sent to the |
||
| 113 | // server and doesn't break Member_Validator |
||
| 114 | $field->setReadonly(true); |
||
| 115 | $field->setTitle($field->Title()._t('LDAPMemberExtension.IMPORTEDFIELD', ' (imported)')); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $message = _t( |
||
| 119 | 'LDAPMemberExtension.INFOIMPORTED', |
||
| 120 | 'This user is automatically imported from LDAP. '. |
||
| 121 | 'Manual changes to imported fields will be removed upon sync.' |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | if ($message) { |
||
| 125 | $fields->addFieldToTab( |
||
| 126 | 'Root.Main', |
||
| 127 | new LiteralField( |
||
| 128 | 'Info', |
||
| 129 | sprintf('<p class="message warning">%s</p>', $message) |
||
| 130 | ), |
||
| 131 | 'FirstName' |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function validate(ValidationResult $validationResult) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Create the user in LDAP, provided this configuration is enabled |
||
| 160 | * and a username was passed to a new Member record. |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function onBeforeWrite() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Update the local data with LDAP, and ensure local membership is also set in |
||
| 178 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function onAfterWrite() |
|
| 194 | |||
| 195 | View Code Duplication | public function onAfterDelete() { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
| 210 | * this will update the Member record from AD data. |
||
| 211 | */ |
||
| 212 | public function memberLoggedIn() |
||
| 218 | } |
||
| 219 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.