Merge pull request #16671 from eileenmcnaughton/acl
[civicrm-core.git] / CRM / Core / BAO / OpenID.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class contains function for Open Id
22 */
23 class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID {
24
25 /**
26 * Takes an associative array and adds OpenID.
27 *
28 * @param array $params
29 * (reference ) an assoc array of name/value pairs.
30 *
31 * @return object
32 * CRM_Core_BAO_OpenID object on success, null otherwise
33 */
34 public static function add(&$params) {
35 $hook = empty($params['id']) ? 'create' : 'edit';
36 CRM_Utils_Hook::pre($hook, 'OpenID', CRM_Utils_Array::value('id', $params), $params);
37
38 $openId = new CRM_Core_DAO_OpenID();
39 $openId->copyValues($params);
40 $openId->save();
41
42 CRM_Utils_Hook::post($hook, 'OpenID', $openId->id, $openId);
43 return $openId;
44 }
45
46 /**
47 * Given the list of params in the params array, fetch the object
48 * and store the values in the values array
49 *
50 * @param array $entityBlock
51 * Input parameters to find object.
52 *
53 * @return mixed
54 */
55 public static function &getValues($entityBlock) {
56 return CRM_Core_BAO_Block::getValues('openid', $entityBlock);
57 }
58
59 /**
60 * Returns whether or not this OpenID is allowed to login.
61 *
62 * @param string $identity_url
63 * The OpenID to check.
64 *
65 * @return bool
66 */
67 public static function isAllowedToLogin($identity_url) {
68 $openId = new CRM_Core_DAO_OpenID();
69 $openId->openid = $identity_url;
70 if ($openId->find(TRUE)) {
71 return $openId->allowed_to_login == 1;
72 }
73 return FALSE;
74 }
75
76 /**
77 * Get all the openids for a specified contact_id, with the primary openid being first
78 *
79 * @param int $id
80 * The contact id.
81 *
82 * @param bool $updateBlankLocInfo
83 *
84 * @return array
85 * the array of openid's
86 */
87 public static function allOpenIDs($id, $updateBlankLocInfo = FALSE) {
88 if (!$id) {
89 return NULL;
90 }
91
92 $query = "
93 SELECT civicrm_openid.openid, civicrm_location_type.name as locationType, civicrm_openid.is_primary as is_primary,
94 civicrm_openid.allowed_to_login as allowed_to_login, civicrm_openid.id as openid_id,
95 civicrm_openid.location_type_id as locationTypeId
96 FROM civicrm_contact
97 LEFT JOIN civicrm_openid ON ( civicrm_openid.contact_id = civicrm_contact.id )
98 LEFT JOIN civicrm_location_type ON ( civicrm_openid.location_type_id = civicrm_location_type.id )
99 WHERE
100 civicrm_contact.id = %1
101 ORDER BY
102 civicrm_openid.is_primary DESC, openid_id ASC ";
103 $params = [1 => [$id, 'Integer']];
104
105 $openids = $values = [];
106 $dao = CRM_Core_DAO::executeQuery($query, $params);
107 $count = 1;
108 while ($dao->fetch()) {
109 $values = [
110 'locationType' => $dao->locationType,
111 'is_primary' => $dao->is_primary,
112 'id' => $dao->openid_id,
113 'openid' => $dao->openid,
114 'locationTypeId' => $dao->locationTypeId,
115 'allowed_to_login' => $dao->allowed_to_login,
116 ];
117
118 if ($updateBlankLocInfo) {
119 $openids[$count++] = $values;
120 }
121 else {
122 $openids[$dao->openid_id] = $values;
123 }
124 }
125 return $openids;
126 }
127
128 /**
129 * Call common delete function.
130 *
131 * @param int $id
132 *
133 * @return bool
134 */
135 public static function del($id) {
136 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('OpenID', $id);
137 }
138
139 }