Merge pull request #17252 from civicrm/5.25
[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 * Create or update OpenID record.
27 *
28 * @param array $params
29 * @return CRM_Core_DAO_OpenID
30 */
31 public static function add($params) {
32 return self::writeRecord($params);
33 }
34
35 /**
36 * Given the list of params in the params array, fetch the object
37 * and store the values in the values array
38 *
39 * @param array $entityBlock
40 * Input parameters to find object.
41 *
42 * @return mixed
43 */
44 public static function &getValues($entityBlock) {
45 return CRM_Core_BAO_Block::getValues('openid', $entityBlock);
46 }
47
48 /**
49 * Returns whether or not this OpenID is allowed to login.
50 *
51 * @param string $identity_url
52 * The OpenID to check.
53 *
54 * @return bool
55 */
56 public static function isAllowedToLogin($identity_url) {
57 $openId = new CRM_Core_DAO_OpenID();
58 $openId->openid = $identity_url;
59 if ($openId->find(TRUE)) {
60 return $openId->allowed_to_login == 1;
61 }
62 return FALSE;
63 }
64
65 /**
66 * Get all the openids for a specified contact_id, with the primary openid being first
67 *
68 * @param int $id
69 * The contact id.
70 *
71 * @param bool $updateBlankLocInfo
72 *
73 * @return array
74 * the array of openid's
75 */
76 public static function allOpenIDs($id, $updateBlankLocInfo = FALSE) {
77 if (!$id) {
78 return NULL;
79 }
80
81 $query = "
82 SELECT civicrm_openid.openid, civicrm_location_type.name as locationType, civicrm_openid.is_primary as is_primary,
83 civicrm_openid.allowed_to_login as allowed_to_login, civicrm_openid.id as openid_id,
84 civicrm_openid.location_type_id as locationTypeId
85 FROM civicrm_contact
86 LEFT JOIN civicrm_openid ON ( civicrm_openid.contact_id = civicrm_contact.id )
87 LEFT JOIN civicrm_location_type ON ( civicrm_openid.location_type_id = civicrm_location_type.id )
88 WHERE
89 civicrm_contact.id = %1
90 ORDER BY
91 civicrm_openid.is_primary DESC, openid_id ASC ";
92 $params = [1 => [$id, 'Integer']];
93
94 $openids = $values = [];
95 $dao = CRM_Core_DAO::executeQuery($query, $params);
96 $count = 1;
97 while ($dao->fetch()) {
98 $values = [
99 'locationType' => $dao->locationType,
100 'is_primary' => $dao->is_primary,
101 'id' => $dao->openid_id,
102 'openid' => $dao->openid,
103 'locationTypeId' => $dao->locationTypeId,
104 'allowed_to_login' => $dao->allowed_to_login,
105 ];
106
107 if ($updateBlankLocInfo) {
108 $openids[$count++] = $values;
109 }
110 else {
111 $openids[$dao->openid_id] = $values;
112 }
113 }
114 return $openids;
115 }
116
117 /**
118 * Call common delete function.
119 *
120 * @param int $id
121 *
122 * @return bool
123 */
124 public static function del($id) {
125 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('OpenID', $id);
126 }
127
128 }