9
9
* if(!$page->viewable()) { echo "sorry you can't view this"; }
10
10
* ...and so on...
11
11
*
12
- * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
12
+ * ProcessWire 3.x, Copyright 2025 by Ryan Cramer
13
13
* https://processwire.com
14
14
*
15
15
* Optional special permissions that are optional (by default, not installed):
@@ -221,7 +221,18 @@ class PagePermissions extends WireData implements Module {
221
221
if($this->hasPagePublish === null) {
222
222
$this->hasPagePublish = $this->wire()->permissions->get('page-publish')->id > 0;
223
223
}
224
-
224
+
225
+ /*
226
+ * Potential addition (cc:@adrianbj)
227
+ * cancel editable if page in trash, not restorable, and trash page not listable?
228
+ if($page->isTrash() && !$page->restorable()) {
229
+ $trashId = $this->wire()->config->trashPageID;
230
+ if($page->id == $trashId) return false;
231
+ $trashPage = $this->wire()->pages->get($trashId);
232
+ if(!$trashPage->listable()) return false;
233
+ }
234
+ */
235
+
225
236
if($this->hasPagePublish) {
226
237
227
238
// if user has the page-publish permission here, then we're good
@@ -243,6 +254,7 @@ class PagePermissions extends WireData implements Module {
243
254
* @param Page $page
244
255
* @param array $options
245
256
* - `viewable` (bool): Specify true if only a viewable check is needed (default=false)
257
+ * - `processName` (string): Name of Process module to consider for context. 3.0.244+ (default='')
246
258
* @return bool
247
259
*
248
260
*/
@@ -252,16 +264,17 @@ class PagePermissions extends WireData implements Module {
252
264
253
265
$user = $this->wire()->user;
254
266
$process = $this->wire()->process;
255
- $processName = (string) $process;
256
267
$config = $this->wire()->config;
257
268
$guestRoleID = (int) $config->guestUserRolePageID;
258
269
$permissions = $this->wire()->permissions;
259
270
260
271
$defaults = array(
261
272
'viewable' => false, // specify true if method is being used to determine viewable state
273
+ 'processName' => '',
262
274
);
263
275
264
276
$options = count($options) ? array_merge($defaults, $options) : $defaults;
277
+ $processName = $options['processName'] ? $options['processName'] : (string) $process;
265
278
266
279
if(!$page->id) return false;
267
280
@@ -380,7 +393,8 @@ class PagePermissions extends WireData implements Module {
380
393
* Returns whether the given page ($page) is viewable by the current user
381
394
*
382
395
* @param Page $page
383
- * @param array $options
396
+ * @param array $options
397
+ * - `processName` (string): Name of Process module to consider for context. 3.0.244+ (default='')
384
398
* @return bool
385
399
* @throws WireException
386
400
*
@@ -565,13 +579,23 @@ class PagePermissions extends WireData implements Module {
565
579
*
566
580
*/
567
581
protected function fileViewable(Page $page, $pagefile) {
568
- if($this->wire()->user->isSuperuser()) return true;
582
+ $user = $this->wire()->user;
583
+ if($user->isSuperuser()) return true;
569
584
if(!$pagefile instanceof Pagefile) {
585
+ $of = $page->of();
586
+ if($of) $page->of(false); // false so that temp files are considered
570
587
$pagefile = $page->hasFile(basename($pagefile), array('getPagefile' => true));
588
+ if($of) $page->of(true);
571
589
if(!$pagefile) return null;
572
590
}
573
591
$field = $pagefile->field;
574
592
if(!$field) return null;
593
+ if($pagefile->isTemp()) {
594
+ // temporary file, just uploaded but not yet saved
595
+ if(!$user->isLoggedin()) return false;
596
+ if($page->id === $user->id) return $this->userFieldEditable($field, $user);
597
+ return $this->fieldEditable($page, $field);
598
+ }
575
599
return $this->fieldViewable($page, $field, false);
576
600
}
577
601
@@ -606,6 +630,7 @@ class PagePermissions extends WireData implements Module {
606
630
* in that language (requires LanguageSupportPageNames module).
607
631
* - Optionally specify boolean false as first or second argument to bypass template filename check.
608
632
* - Optionally specify a Pagefile object or file basename to check if file is viewable. (3.0.166+)
633
+ * - Optionally specify array as first or second argument for options (3.0.244+ internal use)
609
634
*
610
635
* Returns boolean true or false. If given a Pagefile or file basename, it can also return null if
611
636
* the Page itself is viewable but the file did not map to something we recognize as access controlled,
@@ -626,6 +651,7 @@ class PagePermissions extends WireData implements Module {
626
651
$checkTemplateFile = true; // return false if template filename doesn't exist
627
652
$pagefile = null;
628
653
$status = $page->status;
654
+ $options = array();
629
655
630
656
// allow specifying User instance as argument 0
631
657
// this gives you a "viewable to user" capability
@@ -642,9 +668,15 @@ class PagePermissions extends WireData implements Module {
642
668
// @todo: prevent possible collision of field name and language name
643
669
$field = $arg0;
644
670
$checkTemplateFile = false;
645
- }
671
+ } else if(is_array($arg0)) {
672
+ $options = $arg0;
673
+ }
646
674
}
647
675
676
+ if($arg1) {
677
+ if(is_array($arg1) && empty($options)) $options = $arg1;
678
+ }
679
+
648
680
if($arg0 === false || $arg1 === false) {
649
681
// bypass template filename check
650
682
$checkTemplateFile = false;
@@ -668,7 +700,7 @@ class PagePermissions extends WireData implements Module {
668
700
$viewable = $this->processViewable($page);
669
701
} else if($page instanceof User) { // && !$user->isGuest() && ($user->hasPermission('user-admin') || $page->id === $user->id)) {
670
702
// user administrator or user viewing themself
671
- $viewable = $this->userViewable($page);
703
+ $viewable = $this->userViewable($page, $options );
672
704
} else if(!$user->hasPermission("page-view", $page)) {
673
705
// user lacks basic view permission to page
674
706
$viewable = false;
0 commit comments