Login.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Gapps
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Gdata_Extension
  23. */
  24. require_once 'Zend/Gdata/Extension.php';
  25. /**
  26. * @see Zend_Gdata_Gapps
  27. */
  28. require_once 'Zend/Gdata/Gapps.php';
  29. /**
  30. * Represents the apps:login element used by the Apps data API. This
  31. * class is used to describe properties of a user, and is usually contained
  32. * within instances of Zene_Gdata_Gapps_UserEntry or any other class
  33. * which is linked to a particular username.
  34. *
  35. * @category Zend
  36. * @package Zend_Gdata
  37. * @subpackage Gapps
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Gdata_Gapps_Extension_Login extends Zend_Gdata_Extension
  42. {
  43. protected $_rootNamespace = 'apps';
  44. protected $_rootElement = 'login';
  45. /**
  46. * The username for this user. This is used as the user's email address
  47. * and when logging in to Google Apps-hosted services.
  48. *
  49. * @var string
  50. */
  51. protected $_username = null;
  52. /**
  53. * The password for the user. May be in cleartext or as an SHA-1
  54. * digest, depending on the value of _hashFunctionName.
  55. *
  56. * @var string
  57. */
  58. protected $_password = null;
  59. /**
  60. * Specifies whether the password stored in _password is in cleartext
  61. * or is an SHA-1 digest of a password. If the password is cleartext,
  62. * then this should be null. If the password is an SHA-1 digest, then
  63. * this should be set to 'SHA-1'.
  64. *
  65. * At the time of writing, no other hash functions are supported
  66. *
  67. * @var string
  68. */
  69. protected $_hashFunctionName = null;
  70. /**
  71. * True if the user has administrative rights for this domain, false
  72. * otherwise.
  73. *
  74. * @var boolean
  75. */
  76. protected $_admin = null;
  77. /**
  78. * True if the user has agreed to the terms of service for Google Apps,
  79. * false otherwise.
  80. *
  81. * @var boolean.
  82. */
  83. protected $_agreedToTerms = null;
  84. /**
  85. * True if this user has been suspended, false otherwise.
  86. *
  87. * @var boolean
  88. */
  89. protected $_suspended = null;
  90. /**
  91. * True if the user will be required to change their password at
  92. * their next login, false otherwise.
  93. *
  94. * @var boolean
  95. */
  96. protected $_changePasswordAtNextLogin = null;
  97. /**
  98. * Constructs a new Zend_Gdata_Gapps_Extension_Login object.
  99. *
  100. * @param string $username (optional) The username to be used for this
  101. * login.
  102. * @param string $password (optional) The password to be used for this
  103. * login.
  104. * @param string $hashFunctionName (optional) The name of the hash
  105. * function used to protect the password, or null if no
  106. * has function has been applied. As of this writing,
  107. * the only valid values are 'SHA-1' or null.
  108. * @param boolean $admin (optional) Whether the user is an administrator
  109. * or not.
  110. * @param boolean $suspended (optional) Whether this login is suspended or not.
  111. * @param boolean $changePasswordAtNextLogin (optional) Whether
  112. * the user is required to change their password at their
  113. * next login.
  114. * @param boolean $agreedToTerms (optional) Whether the user has
  115. * agreed to the terms of service.
  116. */
  117. public function __construct($username = null, $password = null,
  118. $hashFunctionName = null, $admin = null, $suspended = null,
  119. $changePasswordAtNextLogin = null, $agreedToTerms = null)
  120. {
  121. $this->registerAllNamespaces(Zend_Gdata_Gapps::$namespaces);
  122. parent::__construct();
  123. $this->_username = $username;
  124. $this->_password = $password;
  125. $this->_hashFunctionName = $hashFunctionName;
  126. $this->_admin = $admin;
  127. $this->_agreedToTerms = $agreedToTerms;
  128. $this->_suspended = $suspended;
  129. $this->_changePasswordAtNextLogin = $changePasswordAtNextLogin;
  130. }
  131. /**
  132. * Retrieves a DOMElement which corresponds to this element and all
  133. * child properties. This is used to build an entry back into a DOM
  134. * and eventually XML text for sending to the server upon updates, or
  135. * for application storage/persistence.
  136. *
  137. * @param DOMDocument $doc The DOMDocument used to construct DOMElements
  138. * @return DOMElement The DOMElement representing this element and all
  139. * child properties.
  140. */
  141. public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
  142. {
  143. $element = parent::getDOM($doc, $majorVersion, $minorVersion);
  144. if ($this->_username !== null) {
  145. $element->setAttribute('userName', $this->_username);
  146. }
  147. if ($this->_password !== null) {
  148. $element->setAttribute('password', $this->_password);
  149. }
  150. if ($this->_hashFunctionName !== null) {
  151. $element->setAttribute('hashFunctionName', $this->_hashFunctionName);
  152. }
  153. if ($this->_admin !== null) {
  154. $element->setAttribute('admin', ($this->_admin ? "true" : "false"));
  155. }
  156. if ($this->_agreedToTerms !== null) {
  157. $element->setAttribute('agreedToTerms', ($this->_agreedToTerms ? "true" : "false"));
  158. }
  159. if ($this->_suspended !== null) {
  160. $element->setAttribute('suspended', ($this->_suspended ? "true" : "false"));
  161. }
  162. if ($this->_changePasswordAtNextLogin !== null) {
  163. $element->setAttribute('changePasswordAtNextLogin', ($this->_changePasswordAtNextLogin ? "true" : "false"));
  164. }
  165. return $element;
  166. }
  167. /**
  168. * Given a DOMNode representing an attribute, tries to map the data into
  169. * instance members. If no mapping is defined, the name and value are
  170. * stored in an array.
  171. *
  172. * @param DOMNode $attribute The DOMNode attribute needed to be handled
  173. * @throws Zend_Gdata_App_InvalidArgumentException
  174. */
  175. protected function takeAttributeFromDOM($attribute)
  176. {
  177. switch ($attribute->localName) {
  178. case 'userName':
  179. $this->_username = $attribute->nodeValue;
  180. break;
  181. case 'password':
  182. $this->_password = $attribute->nodeValue;
  183. break;
  184. case 'hashFunctionName':
  185. $this->_hashFunctionName = $attribute->nodeValue;
  186. break;
  187. case 'admin':
  188. if ($attribute->nodeValue == "true") {
  189. $this->_admin = true;
  190. }
  191. else if ($attribute->nodeValue == "false") {
  192. $this->_admin = false;
  193. }
  194. else {
  195. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  196. throw new Zend_Gdata_App_InvalidArgumentException("Expected 'true' or 'false' for apps:login#admin.");
  197. }
  198. break;
  199. case 'agreedToTerms':
  200. if ($attribute->nodeValue == "true") {
  201. $this->_agreedToTerms = true;
  202. }
  203. else if ($attribute->nodeValue == "false") {
  204. $this->_agreedToTerms = false;
  205. }
  206. else {
  207. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  208. throw new Zend_Gdata_App_InvalidArgumentException("Expected 'true' or 'false' for apps:login#agreedToTerms.");
  209. }
  210. break;
  211. case 'suspended':
  212. if ($attribute->nodeValue == "true") {
  213. $this->_suspended = true;
  214. }
  215. else if ($attribute->nodeValue == "false") {
  216. $this->_suspended = false;
  217. }
  218. else {
  219. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  220. throw new Zend_Gdata_App_InvalidArgumentException("Expected 'true' or 'false' for apps:login#suspended.");
  221. }
  222. break;
  223. case 'changePasswordAtNextLogin':
  224. if ($attribute->nodeValue == "true") {
  225. $this->_changePasswordAtNextLogin = true;
  226. }
  227. else if ($attribute->nodeValue == "false") {
  228. $this->_changePasswordAtNextLogin = false;
  229. }
  230. else {
  231. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  232. throw new Zend_Gdata_App_InvalidArgumentException("Expected 'true' or 'false' for apps:login#changePasswordAtNextLogin.");
  233. }
  234. break;
  235. default:
  236. parent::takeAttributeFromDOM($attribute);
  237. }
  238. }
  239. /**
  240. * Get the value for this element's username attribute.
  241. *
  242. * @see setUsername
  243. * @return string The attribute being modified.
  244. */
  245. public function getUsername()
  246. {
  247. return $this->_username;
  248. }
  249. /**
  250. * Set the value for this element's username attribute. This string
  251. * is used to uniquely identify the user in this domian and is used
  252. * to form this user's email address.
  253. *
  254. * @param string $value The desired value for this attribute.
  255. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  256. */
  257. public function setUsername($value)
  258. {
  259. $this->_username = $value;
  260. return $this;
  261. }
  262. /**
  263. * Get the value for this element's password attribute.
  264. *
  265. * @see setPassword
  266. * @return string The requested attribute.
  267. */
  268. public function getPassword()
  269. {
  270. return $this->_password;
  271. }
  272. /**
  273. * Set the value for this element's password attribute. As of this
  274. * writing, this can be either be provided as plaintext or hashed using
  275. * the SHA-1 algorithm for protection. If using a hash function,
  276. * this must be indicated by calling setHashFunctionName().
  277. *
  278. * @param string $value The desired value for this attribute.
  279. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  280. */
  281. public function setPassword($value)
  282. {
  283. $this->_password = $value;
  284. return $this;
  285. }
  286. /**
  287. * Get the value for this element's hashFunctionName attribute.
  288. *
  289. * @see setHashFunctionName
  290. * @return string The requested attribute.
  291. */
  292. public function getHashFunctionName()
  293. {
  294. return $this->_hashFunctionName;
  295. }
  296. /**
  297. * Set the value for this element's hashFunctionName attribute. This
  298. * indicates whether the password supplied with setPassword() is in
  299. * plaintext or has had a hash function applied to it. If null,
  300. * plaintext is assumed. As of this writing, the only valid hash
  301. * function is 'SHA-1'.
  302. *
  303. * @param string $value The desired value for this attribute.
  304. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  305. */
  306. public function setHashFunctionName($value)
  307. {
  308. $this->_hashFunctionName = $value;
  309. return $this;
  310. }
  311. /**
  312. * Get the value for this element's admin attribute.
  313. *
  314. * @see setAdmin
  315. * @return boolean The requested attribute.
  316. * @throws Zend_Gdata_App_InvalidArgumentException
  317. */
  318. public function getAdmin()
  319. {
  320. if (!(is_bool($this->_admin))) {
  321. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  322. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for admin.');
  323. }
  324. return $this->_admin;
  325. }
  326. /**
  327. * Set the value for this element's admin attribute. This indicates
  328. * whether this user is an administrator for this domain.
  329. *
  330. * @param boolean $value The desired value for this attribute.
  331. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  332. * @throws Zend_Gdata_App_InvalidArgumentException
  333. */
  334. public function setAdmin($value)
  335. {
  336. if (!(is_bool($value))) {
  337. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  338. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for $value.');
  339. }
  340. $this->_admin = $value;
  341. return $this;
  342. }
  343. /**
  344. * Get the value for this element's agreedToTerms attribute.
  345. *
  346. * @see setAgreedToTerms
  347. * @return boolean The requested attribute.
  348. * @throws Zend_Gdata_App_InvalidArgumentException
  349. */
  350. public function getAgreedToTerms()
  351. {
  352. if (!(is_bool($this->_agreedToTerms))) {
  353. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  354. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for agreedToTerms.');
  355. }
  356. return $this->_agreedToTerms;
  357. }
  358. /**
  359. * Set the value for this element's agreedToTerms attribute. This
  360. * indicates whether this user has agreed to the terms of service.
  361. *
  362. * @param boolean $value The desired value for this attribute.
  363. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  364. * @throws Zend_Gdata_App_InvalidArgumentException
  365. */
  366. public function setAgreedToTerms($value)
  367. {
  368. if (!(is_bool($value))) {
  369. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  370. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for $value.');
  371. }
  372. $this->_agreedToTerms = $value;
  373. return $this;
  374. }
  375. /**
  376. * Get the value for this element's suspended attribute.
  377. *
  378. * @see setSuspended
  379. * @return boolean The requested attribute.
  380. * @throws Zend_Gdata_App_InvalidArgumentException
  381. */
  382. public function getSuspended()
  383. {
  384. if (!(is_bool($this->_suspended))) {
  385. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  386. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for suspended.');
  387. }
  388. return $this->_suspended;
  389. }
  390. /**
  391. * Set the value for this element's suspended attribute. If true, the
  392. * user will not be able to login to this domain until unsuspended.
  393. *
  394. * @param boolean $value The desired value for this attribute.
  395. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  396. * @throws Zend_Gdata_App_InvalidArgumentException
  397. */
  398. public function setSuspended($value)
  399. {
  400. if (!(is_bool($value))) {
  401. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  402. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for $value.');
  403. }
  404. $this->_suspended = $value;
  405. return $this;
  406. }
  407. /**
  408. * Get the value for this element's changePasswordAtNextLogin attribute.
  409. *
  410. * @see setChangePasswordAtNextLogin
  411. * @return boolean The requested attribute.
  412. * @throws Zend_Gdata_App_InvalidArgumentException
  413. */
  414. public function getChangePasswordAtNextLogin()
  415. {
  416. if (!(is_bool($this->_changePasswordAtNextLogin))) {
  417. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  418. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for changePasswordAtNextLogin.');
  419. }
  420. return $this->_changePasswordAtNextLogin;
  421. }
  422. /**
  423. * Set the value for this element's changePasswordAtNextLogin attribute.
  424. * If true, the user will be forced to set a new password the next
  425. * time they login.
  426. *
  427. * @param boolean $value The desired value for this attribute.
  428. * @return Zend_Gdata_Gapps_Extension_Login Provides a fluent interface.
  429. * @throws Zend_Gdata_App_InvalidArgumentException
  430. */
  431. public function setChangePasswordAtNextLogin($value)
  432. {
  433. if (!(is_bool($value))) {
  434. require_once('Zend/Gdata/App/InvalidArgumentException.php');
  435. throw new Zend_Gdata_App_InvalidArgumentException('Expected boolean for $value.');
  436. }
  437. $this->_changePasswordAtNextLogin = $value;
  438. return $this;
  439. }
  440. /**
  441. * Magic toString method allows using this directly via echo
  442. * Works best in PHP >= 4.2.0
  443. */
  444. public function __toString()
  445. {
  446. return "Username: " . $this->getUsername() .
  447. "\nPassword: " . (($this->getPassword() === null) ? "NOT SET" : "SET") .
  448. "\nPassword Hash Function: " . $this->getHashFunctionName() .
  449. "\nAdministrator: " . ($this->getAdmin() ? "Yes" : "No") .
  450. "\nAgreed To Terms: " . ($this->getAgreedToTerms() ? "Yes" : "No") .
  451. "\nSuspended: " . ($this->getSuspended() ? "Yes" : "No");
  452. }
  453. }