model.authent-local.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Authent Local
  20. * User authentication Module, password stored in the local database
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. class UserLocal extends UserInternal
  26. {
  27. public static function Init()
  28. {
  29. $aParams = array
  30. (
  31. "category" => "addon/authentication",
  32. "key_type" => "autoincrement",
  33. "name_attcode" => "login",
  34. "state_attcode" => "",
  35. "reconc_keys" => array('login'),
  36. "db_table" => "priv_user_local",
  37. "db_key_field" => "id",
  38. "db_finalclass_field" => "",
  39. "display_template" => "",
  40. );
  41. MetaModel::Init_Params($aParams);
  42. MetaModel::Init_InheritAttributes();
  43. MetaModel::Init_AddAttribute(new AttributeOneWayPassword("password", array("allowed_values"=>null, "sql"=>"pwd", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  44. // Display lists
  45. MetaModel::Init_SetZListItems('details', array('contactid', 'first_name', 'email', 'login', 'password', 'language', 'status', 'profile_list', 'allowed_org_list')); // Attributes to be displayed for the complete details
  46. MetaModel::Init_SetZListItems('list', array('first_name', 'last_name', 'login')); // Attributes to be displayed for a list
  47. // Search criteria
  48. MetaModel::Init_SetZListItems('standard_search', array('login', 'contactid', 'status')); // Criteria of the std search form
  49. MetaModel::Init_SetZListItems('advanced_search', array('login', 'contactid')); // Criteria of the advanced search form
  50. }
  51. public function CheckCredentials($sPassword)
  52. {
  53. $oPassword = $this->Get('password'); // ormPassword object
  54. // Cannot compare directly the values since they are hashed, so
  55. // Let's ask the password to compare the hashed values
  56. if ($oPassword->CheckPassword($sPassword))
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. public function TrustWebServerContext()
  63. {
  64. return true;
  65. }
  66. public function CanChangePassword()
  67. {
  68. if (MetaModel::GetConfig()->Get('demo_mode'))
  69. {
  70. return false;
  71. }
  72. return true;
  73. }
  74. public function ChangePassword($sOldPassword, $sNewPassword)
  75. {
  76. $oPassword = $this->Get('password'); // ormPassword object
  77. // Cannot compare directly the values since they are hashed, so
  78. // Let's ask the password to compare the hashed values
  79. if ($oPassword->CheckPassword($sOldPassword))
  80. {
  81. $this->SetPassword($sNewPassword);
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * Use with care!
  88. */
  89. public function SetPassword($sNewPassword)
  90. {
  91. $this->Set('password', $sNewPassword);
  92. $oChange = MetaModel::NewObject("CMDBChange");
  93. $oChange->Set("date", time());
  94. $sUserString = CMDBChange::GetCurrentUserName();
  95. $oChange->Set("userinfo", $sUserString);
  96. $oChange->DBInsert();
  97. $this->DBUpdateTracked($oChange, true);
  98. }
  99. /**
  100. * Returns the set of flags (OPT_ATT_HIDDEN, OPT_ATT_READONLY, OPT_ATT_MANDATORY...)
  101. * for the given attribute in the current state of the object
  102. * @param $sAttCode string $sAttCode The code of the attribute
  103. * @param $aReasons array To store the reasons why the attribute is read-only (info about the synchro replicas)
  104. * @param $sTargetState string The target state in which to evalutate the flags, if empty the current state will be used
  105. * @return integer Flags: the binary combination of the flags applicable to this attribute
  106. */
  107. public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
  108. {
  109. $iFlags = parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState);
  110. if (MetaModel::GetConfig()->Get('demo_mode'))
  111. {
  112. if (strpos('contactid,login,language,password,status,profile_list,allowed_org_list', $sAttCode) !== false)
  113. {
  114. // contactid and allowed_org_list are disabled to make sure the portal remains accessible
  115. $aReasons[] = 'Sorry, this attribute is read-only in the demonstration mode!';
  116. $iFlags |= OPT_ATT_READONLY;
  117. }
  118. }
  119. return $iFlags;
  120. }
  121. }