aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Access/PermissionLimits.php
blob: e0ee88367669546af8a0bef251a561a6641f95a2 (plain) (blame)
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
<?php

namespace Zotlabs\Access;

use Zotlabs\Lib\PConfig;

/**
 * @brief Permission limits.
 *
 * Permission limits are a very high level permission setting. They are hard
 * limits by design.
 * "Who can view my photos (at all)?"
 * "Who can post photos in my albums (at all)?"
 *
 * For viewing permissions we generally set these to 'anybody' and for write
 * permissions we generally set them to 'those I allow', though many people
 * restrict the viewing permissions further for things like 'Can view my connections'.
 *
 * People get confused enough by permissions that we wanted a place to set their
 * privacy expectations once and be done with it.
 *
 * Connection related permissions like "Can Joe view my photos?" are handled by
 * @ref ::Zotlabs::Lib::Permcat "Permcat" and inherit from the channel's Permission
 * limits.
 *
 * @see Permissions
 */
class PermissionLimits {

	/**
	 * @brief Get standard permission limits.
	 *
	 * Viewing permissions and post_comments permission are set to 'anybody',
	 * other permissions are set to 'those I allow'.
	 *
	 * The list of permissions comes from Permissions::Perms().
	 *
	 * @return array
	 */
	static public function Std_Limits() {
		$limits = [];
		$perms = Permissions::Perms();

		$anon_comments = get_config('system','anonymous_comments',true);

		foreach($perms as $k => $v) {
			if(strstr($k, 'view') || ($k === 'post_comments' && $anon_comments))
				$limits[$k] = PERMS_PUBLIC;
			else
				$limits[$k] = PERMS_SPECIFIC;
		}

		return $limits;
	}

	/**
	 * @brief Sets a permission limit for a channel.
	 *
	 * @param int $channel_id
	 * @param string $perm
	 * @param int $perm_limit one of PERMS_* constants
	 */
	static public function Set($channel_id, $perm, $perm_limit) {
		PConfig::Set($channel_id, 'perm_limits', $perm, $perm_limit);
	}

	/**
	 * @brief Get a channel's permission limits.
	 *
	 * Return a channel's permission limits from PConfig. If $perm is set just
	 * return this permission limit, if not set, return an array with all
	 * permission limits.
	 *
	 * @param int $channel_id
	 * @param string $perm (optional)
	 * @return
	 *   * \b false if no perm_limits set for this channel
	 *   * \b int if $perm is set, return one of PERMS_* constants for this permission, default 0
	 *   * \b array with all permission limits, if $perm is not set
	 */
	static public function Get($channel_id, $perm = '') {
		if($perm) {
			return intval(PConfig::Get($channel_id, 'perm_limits', $perm));
		}

		PConfig::Load($channel_id);
		if(array_key_exists($channel_id, \App::$config)
				&& array_key_exists('perm_limits', \App::$config[$channel_id]))
			return \App::$config[$channel_id]['perm_limits'];

		return false;
	}
}