1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2009, 2010, 2013, 2014, 2015, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2009, 2010, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates an empty mobile device object.
 26  * @constructor
 27  * @class
 28  * This class represents  mobile device.
 29  * 
 30  * @author Parag Shah
 31  *
 32  * @param	{Hash}		params		a hash of parameters
 33  * @param	{Date}		params.lastPolicyUpdate		the last policy update time
 34  * @param	{Date}		params.firstReqReceived		the first request received time
 35  * @param	{Date}		params.remoteWipeAckTime		the remote wipe acknowledged time
 36  * @param	{Date}		params.remoteWipeReqTime		the remote wipe requested time
 37  * @param	{constant}	params.status			the status (see <code>ZmMobileDevice.STATUS_</code>)
 38  */
 39 ZmMobileDevice = function(params) {
 40 	this.lastPolicyUpdate = params.lastPolicyUpdate;
 41 	this.firstReqReceived = params.firstReqReceived;
 42 	this.remoteWipeAckTime = params.remoteWipeAckTime;
 43 	this.remoteWipeReqTime = params.remoteWipeReqTime;
 44 	this.status = params.status;
 45 	this.provisionable = params.provisionable;
 46 	this.protocol = params.protocol;
 47 	this.ua = params.ua;
 48 	this.type = params.type;
 49 	this.id = params.id;
 50 };
 51 
 52 ZmOAuthConsumerApp = function(params) {
 53     this.accessToken = params.accessToken ;
 54     this.appName = params.appName;
 55     this.approvedOn = params.approvedOn;
 56     this.device = params.device;
 57 };
 58 
 59 
 60 // Consts
 61 
 62 /**
 63  * Defines the "need provision" status.
 64  */
 65 ZmMobileDevice.STATUS_NEED_PROVISION		= 0;
 66 /**
 67  * Defines the "OK" status.
 68  */
 69 ZmMobileDevice.STATUS_OK					= 1;
 70 /**
 71  * Defines the "suspended" status.
 72  */
 73 ZmMobileDevice.STATUS_SUSPENDED				= 2;
 74 /**
 75  * Defines the "remote wipe requested" status.
 76  */
 77 ZmMobileDevice.STATUS_REMOTE_WIPE_REQUESTED	= 3;
 78 /**
 79  * Defines the "remote wipe complete" status.
 80  */
 81 ZmMobileDevice.STATUS_REMOTE_WIPE_COMPLETE	= 4;
 82 /**
 83  * Defines the "OAuth Consumer App" status.
 84  */
 85 ZmMobileDevice.TYPE_OAUTH = 'oauth';
 86 
 87 
 88 // Public methods
 89 
 90 ZmMobileDevice.prototype.toString =
 91 function() {
 92 	return "ZmMobileDevice";
 93 };
 94 
 95 /**
 96  * Gets the status.
 97  * 
 98  * @return	{constant}	the status (see <code>ZmMobileDevice.STATUS_</code> constants)
 99  */
100 ZmMobileDevice.prototype.getStatus =
101 function() {
102 	return (!this.provisionable && this.status == ZmMobileDevice.STATUS_NEED_PROVISION)
103 		? ZmMobileDevice.STATUS_OK : this.status;
104 };
105 
106 /**
107  * Gets the status string.
108  * 
109  * @return	{String}	the status string
110  */
111 ZmMobileDevice.prototype.getStatusString =
112 function() {
113 	var status = this.getStatus();
114 
115 	switch (status) {
116 		case ZmMobileDevice.STATUS_NEED_PROVISION:			return ZmMsg.mobileStatusNeedProvision;
117 		case ZmMobileDevice.STATUS_OK:						return ZmMsg.mobileStatusOk;
118 		case ZmMobileDevice.STATUS_SUSPENDED:				return ZmMsg.mobileStatusSuspended;
119 		case ZmMobileDevice.STATUS_REMOTE_WIPE_REQUESTED:	return ZmMsg.mobileStatusWipe;
120 		case ZmMobileDevice.STATUS_REMOTE_WIPE_COMPLETE:	return ZmMsg.mobileStatusWipeComplete;
121 	}
122 	return "";
123 };
124 
125 /**
126  * Gets the last policy update time as a string.
127  * 
128  * @return	{String}	the last policy update string
129  */
130 ZmMobileDevice.prototype.getLastPolicyUpdateString =
131 function() {
132 	return this.lastPolicyUpdate ? AjxDateUtil.computeDateTimeString(new Date(this.lastPolicyUpdate*1000)) : "";
133 };
134 
135 /**
136  * Gets the first request received time as a string.
137  * 
138  * @return	{String}	the first request received string
139  */
140 ZmMobileDevice.prototype.getFirstReqReceivedString =
141 function() {
142 	return this.firstReqReceived ? AjxDateUtil.computeDateTimeString(new Date(this.firstReqReceived*1000)) : "";
143 };
144 
145 /**
146  * Gets the remote wipe acknowledged time as a string.
147  * 
148  * @return	{String}	the remote wipe acknowledged string
149  */
150 ZmMobileDevice.prototype.getRemoteWipeAckTimeString =
151 function() {
152 	return this.remoteWipeAckTime ? AjxDateUtil.computeDateTimeString(new Date(this.remoteWipeAckTime*1000)) : "";
153 };
154 
155 /**
156  * Gets the remote wipe requested time as a string.
157  * 
158  * @return	{String}	the remote wipe requested string
159  */
160 ZmMobileDevice.prototype.getRemoteWipeReqTimeString =
161 function() {
162 	return this.remoteWipeReqTime ? AjxDateUtil.computeDateTimeString(new Date(this.remoteWipeReqTime*1000)) : "";
163 };
164 
165 ZmMobileDevice.prototype.doAction =
166 function(id, callback) {
167 	var request;
168 	switch (id) {
169 		case ZmOperation.MOBILE_REMOVE: 		request = "RemoveDeviceRequest"; break;
170 		case ZmOperation.MOBILE_RESUME_SYNC:	request = "ResumeDeviceRequest"; break;
171 		case ZmOperation.MOBILE_SUSPEND_SYNC:	request = "SuspendDeviceRequest"; break;
172 		case ZmOperation.MOBILE_WIPE:			request = "RemoteWipeRequest"; break;
173 		case ZmOperation.MOBILE_CANCEL_WIPE:	request = "CancelPendingRemoteWipeRequest"; break;
174 	}
175 
176 	if (request) {
177 		var soapDoc = AjxSoapDoc.create(request, "urn:zimbraSync");
178 		var node = soapDoc.set("device");
179 		node.setAttribute("id", this.id);
180 
181 		var respCallback = new AjxCallback(this, this._handleDoAction, callback);
182 		appCtxt.getAppController().sendRequest({soapDoc:soapDoc, asyncMode:true, callback:respCallback});
183 	}
184 };
185 
186 ZmMobileDevice.prototype._handleDoAction =
187 function(callback, results) {
188 	var resp = results.getResponse();
189 	for (var i in resp) {
190 		var device = resp[i].device && resp[i].device[0];
191 		if (device && device.id == this.id) {
192 			this.status = device.status;
193 			if (device.lastPolicyUpdate) {
194 				this.lastPolicyUpdate = device.lastPolicyUpdate;
195 			}
196 			if (device.firstReqReceived) {
197 				this.firstReqReceived = device.firstReqReceived;
198 			}
199 			if (device.remoteWipeAckTime) {
200 				this.remoteWipeAckTime = device.remoteWipeAckTime;
201 			}
202 			if (device.remoteWipeReqTime) {
203 				this.remoteWipeReqTime = device.remoteWipeReqTime;
204 			}
205 		}
206 	}
207 
208 	if (callback) {
209 		callback.run();
210 	}
211 };
212