2 /*********************************************************************************
3 * SugarCRM is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 class EAPMController extends SugarController
42 * @var ExternalAPIPlugin
46 var $admin_actions = array('listview', 'index');
48 public function process() {
49 if(!is_admin($GLOBALS['current_user']) && in_array(strtolower($this->action), $this->admin_actions)) {
50 $this->hasAccess = false;
55 protected function failed($error)
57 SugarApplication::appendErrorMessage($error);
58 $GLOBALS['log']->error("Login error: $error");
59 $url = 'index.php?module=EAPM&action=EditView&record='.$this->bean->id;
60 return $this->set_redirect($url);
63 public function pre_save()
66 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
67 if(empty($this->api)) {
68 return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
70 if(empty($this->bean->id)){
71 $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
73 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
74 $this->bean->id = $eapmBean->id;
77 $this->bean->validated = false;
78 $this->bean->save_cleanup();
79 $this->api->loadEAPM($this->bean);
82 protected function post_save()
84 if($this->bean->active) {
85 // do not load bean here since password is already encoded
86 $reply = $this->api->checkLogin();
87 if ( !$reply['success'] ) {
88 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
90 $this->bean->validated();
93 if($this->return_module == 'Users'){
94 $this->return_action = 'EditView';
96 return parent::post_save();
99 protected function action_oauth()
101 if(empty($this->bean->id)) {
102 return $this->set_redirect('index.php');
104 if(!$this->bean->ACLAccess('save')){
105 ACLController::displayNoAccess(true);
109 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
110 $reply = $this->api->checkLogin($this->bean);
111 if ( !$reply['success'] ) {
112 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
114 $this->bean->validated();
116 // This is a tweak so that we can automatically close windows if requested by the external account system
117 if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
118 if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
119 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
120 }else if(!empty($_REQUEST['refreshParentWindow'])){
121 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
123 $js = '<script type="text/javascript">window.close();</script>';
129 // redirect to detail view, as in save
130 return parent::post_save();
134 protected function pre_QuickSave(){
135 if(!empty($_REQUEST['application'])){
136 $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
138 $this->bean->application = $_REQUEST['application'];
139 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
141 $this->bean = $eapmBean;
142 $this->bean->active = 1;
147 sugar_die("Please pass an application name.");
151 public function action_QuickSave(){
152 $this->action_save();
155 protected function post_QuickSave(){
159 protected function pre_Reauthenticate(){
160 $this->bean->active = 1;
164 protected function action_Reauthenticate(){
165 $this->action_save();
168 protected function post_Reauthenticate(){
172 protected function action_FlushFileCache()
174 $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
175 if ( $api == false ) {
180 if ( method_exists($api,'loadDocCache') ) {
181 $api->loadDocCache(true);