Commit | Line | Data |
---|---|---|
3ee128fb TO |
1 | <?php |
2 | ||
3 | namespace Civi\Api4; | |
4 | ||
5 | use Civi\Core\Event\GenericHookEvent; | |
6 | use Civi\OAuth\CiviGenericProvider; | |
7 | ||
8 | class OAuthProvider extends Generic\AbstractEntity { | |
9 | ||
10 | const TTL = 600; | |
11 | ||
12 | /** | |
13 | * @param bool $checkPermissions | |
14 | * @return Generic\BasicGetAction | |
15 | */ | |
16 | public static function get($checkPermissions = TRUE) { | |
17 | $action = new Generic\BasicGetAction('OAuthProvider', __FUNCTION__, function () { | |
2752428e | 18 | $cache = \Civi::cache('long'); |
3ee128fb TO |
19 | if (!$cache->has('OAuthProvider_list')) { |
20 | $providers = []; | |
21 | $event = GenericHookEvent::create([ | |
22 | 'providers' => &$providers, | |
23 | ]); | |
24 | \Civi::dispatcher()->dispatch('hook_civicrm_oauthProviders', $event); | |
25 | ||
26 | foreach ($providers as $name => &$provider) { | |
27 | if ($provider['name'] !== $name) { | |
28 | throw new \API_Exception(sprintf("Mismatched OAuth provider names: \"%s\" vs \"%s\"", | |
29 | $provider['name'], $name)); | |
30 | } | |
31 | if (!isset($provider['class'])) { | |
32 | $provider['class'] = CiviGenericProvider::class; | |
33 | } | |
34 | } | |
35 | ||
36 | $cache->set('OAuthProvider_list', $providers, self::TTL); | |
37 | } | |
38 | return $cache->get('OAuthProvider_list'); | |
39 | }); | |
40 | return $action->setCheckPermissions($checkPermissions); | |
41 | } | |
42 | ||
43 | /** | |
44 | * @param bool $checkPermissions | |
45 | * @return Generic\BasicGetFieldsAction | |
46 | */ | |
47 | public static function getFields($checkPermissions = TRUE) { | |
48 | $action = new Generic\BasicGetFieldsAction('OAuthProvider', __FUNCTION__, function () { | |
49 | return [ | |
50 | [ | |
51 | 'name' => 'name', | |
52 | ], | |
53 | [ | |
54 | 'name' => 'title', | |
55 | ], | |
56 | [ | |
57 | 'name' => 'class', | |
58 | ], | |
59 | [ | |
60 | 'name' => 'options', | |
61 | ], | |
60a62215 CW |
62 | [ |
63 | 'name' => 'contactTemplate', | |
64 | ], | |
3ee128fb TO |
65 | ]; |
66 | }); | |
67 | return $action->setCheckPermissions($checkPermissions); | |
68 | } | |
69 | ||
70 | /** | |
71 | * @return array | |
72 | */ | |
73 | public static function permissions() { | |
74 | return [ | |
75 | "meta" => ["access CiviCRM"], | |
76 | "get" => ["access CiviCRM"], | |
77 | "default" => ["administer CiviCRM"], | |
78 | ]; | |
79 | } | |
80 | ||
81 | } |