MagicMerge - Per-request cache of path/url properties
[civicrm-core.git] / CRM / Core / BAO / OpenID.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains function for Open Id
38 */
39class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID {
40
41 /**
fe482240 42 * Takes an associative array and adds OpenID.
6a488035 43 *
6a0b768e
TO
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
6a488035 46 *
a6c01b45
CW
47 * @return object
48 * CRM_Core_BAO_OpenID object on success, null otherwise
6a488035 49 */
00be9182 50 public static function add(&$params) {
6a488035 51 $hook = empty($params['id']) ? 'create' : 'edit';
12445e1c 52 CRM_Utils_Hook::pre($hook, 'OpenID', CRM_Utils_Array::value('id', $params), $params);
6a488035
TO
53
54 $openId = new CRM_Core_DAO_OpenID();
55 $openId->copyValues($params);
56 $openId->save();
57
12445e1c 58 CRM_Utils_Hook::post($hook, 'OpenID', $openId->id, $openId);
6a488035
TO
59 return $openId;
60 }
61
62 /**
63 * Given the list of params in the params array, fetch the object
64 * and store the values in the values array
65 *
6a0b768e
TO
66 * @param array $entityBlock
67 * Input parameters to find object.
6a488035
TO
68 *
69 * @return mixed
6a488035 70 */
00be9182 71 public static function &getValues($entityBlock) {
6a488035
TO
72 return CRM_Core_BAO_Block::getValues('openid', $entityBlock);
73 }
74
75 /**
fe482240 76 * Returns whether or not this OpenID is allowed to login.
6a488035 77 *
6a0b768e
TO
78 * @param string $identity_url
79 * The OpenID to check.
6a488035 80 *
8d7a9d07 81 * @return bool
6a488035 82 */
00be9182 83 public static function isAllowedToLogin($identity_url) {
6a488035
TO
84 $openId = new CRM_Core_DAO_OpenID();
85 $openId->openid = $identity_url;
86 if ($openId->find(TRUE)) {
87 return $openId->allowed_to_login == 1;
88 }
89 return FALSE;
90 }
91
92 /**
93 * Get all the openids for a specified contact_id, with the primary openid being first
94 *
6a0b768e
TO
95 * @param int $id
96 * The contact id.
6a488035 97 *
77b97be7
EM
98 * @param bool $updateBlankLocInfo
99 *
a6c01b45
CW
100 * @return array
101 * the array of openid's
6a488035 102 */
00be9182 103 public static function allOpenIDs($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
104 if (!$id) {
105 return NULL;
106 }
107
108 $query = "
cbb7c7e0 109SELECT civicrm_openid.openid, civicrm_location_type.name as locationType, civicrm_openid.is_primary as is_primary,
110civicrm_openid.allowed_to_login as allowed_to_login, civicrm_openid.id as openid_id,
6a488035
TO
111civicrm_openid.location_type_id as locationTypeId
112FROM civicrm_contact
113LEFT JOIN civicrm_openid ON ( civicrm_openid.contact_id = civicrm_contact.id )
114LEFT JOIN civicrm_location_type ON ( civicrm_openid.location_type_id = civicrm_location_type.id )
115WHERE
116 civicrm_contact.id = %1
117ORDER BY
118 civicrm_openid.is_primary DESC, openid_id ASC ";
119 $params = array(1 => array($id, 'Integer'));
120
121 $openids = $values = array();
353ffa53
TO
122 $dao = CRM_Core_DAO::executeQuery($query, $params);
123 $count = 1;
6a488035
TO
124 while ($dao->fetch()) {
125 $values = array(
126 'locationType' => $dao->locationType,
127 'is_primary' => $dao->is_primary,
128 'id' => $dao->openid_id,
129 'openid' => $dao->openid,
130 'locationTypeId' => $dao->locationTypeId,
131 'allowed_to_login' => $dao->allowed_to_login,
132 );
133
134 if ($updateBlankLocInfo) {
135 $openids[$count++] = $values;
136 }
137 else {
138 $openids[$dao->openid_id] = $values;
139 }
140 }
141 return $openids;
142 }
12445e1c
CW
143
144 /**
fe482240 145 * Call common delete function.
12445e1c 146 */
00be9182 147 public static function del($id) {
a65e2e55 148 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('OpenID', $id);
12445e1c 149 }
96025800 150
6a488035 151}