foldershare-8.x-1.2/src/Entity/FolderShareTraits/OperationShareTrait.php

src/Entity/FolderShareTraits/OperationShareTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
 
namespace Drupal\foldershare\Entity\FolderShareTraits;
 
use Drupal\user\Entity\User;
 
use Drupal\foldershare\ManageLog;
use Drupal\foldershare\Entity\Exception\LockException;
 
/**
 * Share FolderShare entities.
 *
 * This trait includes methods to share FolderShare entities.
 *
 * <B>Internal trait</B>
 * This trait is internal to the FolderShare module and used to define
 * features of the FolderShare entity class. It is a mechanism to group
 * functionality to improve code management.
 *
 * @ingroup foldershare
 */
trait OperationShareTrait {
 
  /*---------------------------------------------------------------------
   *
   * Share FolderShare entity.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function share(array $grants) {
    if ($this->isRootItem() === FALSE) {
      // Not a root. Only roots have access grants.
      return;
    }
 
    //
    // Lock root folder tree.
    // ----------------------
    // Lock the folder tree rooted on this item. This prevents other operations
    // from modifying this item or its folder tree until grants are updated.
    //
    // LOCK ROOT FOLDER TREE.
    if (self::acquireRootOperationLock($this->id()) === FALSE) {
      throw new LockException(
        self::getStandardLockExceptionMessage(t('updated'), $this->getName()));
    }
 
    //
    // Update access grants.
    // ---------------------
    // Set the grants and save.
    $oldGrants = $this->getAccessGrants();
 
    $this->setAccessGrants($grants);
    $this->save();
 
    //
    // Unlock root folder tree.
    // ------------------------
    // After access grants are set, we're done.
    //
    // UNLOCK ROOT FOLDER TREE.
    self::releaseRootOperationLock($this->id());
 
    //
    // Hook & log.
    // -----------
    // Announce the change.
    $requestingUid = self::getCurrentUserId()[0];
    self::postOperationHook(
      'share',
      [
        $this,
        $oldGrants,
        $grants,
        $requestingUid,
      ]);
    ManageLog::activity(
      "Changed sharing for top-level @kind '@name' (# @id).\nOld grants: @oldGrants.\nNew grants: @newGrants.",
      [
        '@id'        => $this->id(),
        '@kind'      => $this->getKind(),
        '@name'      => $this->getName(),
        '@oldGrants' => self::accessGrantsToString($oldGrants),
        '@newGrants' => self::accessGrantsToString($grants),
        'entity'     => $this,
        'uid'        => $requestingUid,
      ]);
  }
 
  /*---------------------------------------------------------------------
   *
   * Unshare FolderShare entity.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function unshare(int $uid, string $access = '') {
    if ($this->isRootItem() === FALSE) {
      // Not a root. Only roots have access grants.
      return;
    }
 
    //
    // Lock root folder tree.
    // ----------------------
    // Lock the folder tree rooted on this item. This prevents other operations
    // from modifying this item or its folder tree until grants are updated.
    //
    // LOCK ROOT FOLDER TREE.
    if (self::acquireRootOperationLock($this->id()) === FALSE) {
      throw new LockException(
        self::getStandardLockExceptionMessage(t('updated'), $this->getName()));
    }
 
    //
    // Update access grants.
    // ---------------------
    // Remove the indicated user from the access grants.
    $oldGrants = $this->getAccessGrants();
 
    if (empty($access) === TRUE) {
      $this->deleteAccessGrant($uid, 'view');
      $this->deleteAccessGrant($uid, 'author');
      $access = 'view & author';
    }
    else {
      $this->deleteAccessGrant($uid, $access);
    }
 
    $newGrants = $this->getAccessGrants();
    $this->save();
 
    //
    // Unlock root folder tree.
    // ------------------------
    // After access grants are set, we're done.
    //
    // UNLOCK ROOT FOLDER TREE.
    self::releaseRootOperationLock($this->id());
 
    //
    // Hook & log.
    // -----------
    // Announce the change.
    $requestingUid = self::getCurrentUserId()[0];
    self::postOperationHook(
      'share',
      [
        $this,
        $oldGrants,
        $newGrants,
        $requestingUid,
      ]);
    if (ManageLog::isActivityLoggingEnabled() === TRUE) {
      $user = User::load($uid);
      $userName = ($user === NULL) ? 'Unknown' : $user->getDisplayName();
 
      ManageLog::activity(
        "Release sharing for top-level @kind '@name' (# @id) for user %userName (# @uid) to @access.\nOld grants: @oldGrants.\nNew grants: @newGrants.",
        [
          '@id'        => $this->id(),
          '@kind'      => $this->getKind(),
          '@name'      => $this->getName(),
          '%userName'  => $userName,
          '@uid'       => $uid,
          '@access'    => $access,
          '@oldGrants' => self::accessGrantsToString($oldGrants),
          '@newGrants' => self::accessGrantsToString($newGrants),
          'entity'     => $this,
          'uid'        => $requestingUid,
        ]);
    }
  }
 
  /**
   * Unshares multiple items.
   *
   * Each of the indicated items has its access grants adjusted to remove
   * the indicated user for shared access. The $access argument may be
   * 'view' or 'author', or left empty to unshare for both.
   *
   * <B>Process locks</B>
   * This method locks each item's root folder tree for the duration of the
   * update. This will prevent any other edit operation from being performed
   * on the same folder tree until the update is done.
   *
   * <B>Post-operation hooks</B>
   * This method calls the "hook_foldershare_post_operation_share" hook
   * after each item is updated.
   *
   * <B>Activity log</B>
   * This method posts a log message after each item has been updated.
   *
   * @param int[] $ids
   *   An array of integer FolderShare entity IDs to unshare. Invalid IDs
   *   are silently skipped.
   * @param int $uid
   *   The user ID of the user to unshare for.
   * @param string $access
   *   The access grant to unshare. One of 'view' or 'author'. An empty
   *   string unshares for 'view' AND 'author'.
   *
   * @throws \Drupal\foldershare\Entity\Exception\LockException
   *   Throws an exception if this item cannot be locked for exclusive use.
   *
   * @see ::unshare()
   */
  public static function unshareMultiple(
    array $ids,
    int $uid,
    string $access = '') {
 
    $nLockExceptions = 0;
    foreach ($ids as $id) {
      $item = self::load($id);
      if ($item !== NULL) {
        try {
          $item->unshare($uid, $access);
        }
        catch (LockException $e) {
          ++$nLockExceptions;
        }
      }
    }
 
    if ($nLockExceptions !== 0) {
      throw new LockException(
        self::getStandardLockExceptionMessage(t('updated'), NULL));
    }
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc