PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Objects/AccountControl.php

https://gitlab.com/R13e/Adldap2
PHP | 451 lines | 181 code | 79 blank | 191 comment | 3 complexity | 2019d908a88f223d370b87d521255af8 MD5 | raw file
  1. <?php
  2. namespace Adldap\Objects;
  3. /**
  4. * The Account Control class.
  5. *
  6. * This class is for easily building a user account control value.
  7. *
  8. * https://support.microsoft.com/en-us/kb/305144
  9. */
  10. class AccountControl
  11. {
  12. const SCRIPT = 1;
  13. const ACCOUNTDISABLE = 2;
  14. const HOMEDIR_REQUIRED = 8;
  15. const LOCKOUT = 16;
  16. const PASSWD_NOTREQD = 32;
  17. const ENCRYPTED_TEXT_PWD_ALLOWED = 128;
  18. const TEMP_DUPLICATE_ACCOUNT = 256;
  19. const NORMAL_ACCOUNT = 512;
  20. const INTERDOMAIN_TRUST_ACCOUNT = 2048;
  21. const WORKSTATION_TRUST_ACCOUNT = 4096;
  22. const SERVER_TRUST_ACCOUNT = 8192;
  23. const DONT_EXPIRE_PASSWORD = 65536;
  24. const MNS_LOGON_ACCOUNT = 131072;
  25. const SMARTCARD_REQUIRED = 262144;
  26. const TRUSTED_FOR_DELEGATION = 524288;
  27. const NOT_DELEGATED = 1048576;
  28. const USE_DES_KEY_ONLY = 2097152;
  29. const DONT_REQ_PREAUTH = 4194304;
  30. const PASSWORD_EXPIRED = 8388608;
  31. const TRUSTED_TO_AUTH_FOR_DELEGATION = 16777216;
  32. const PARTIAL_SECRETS_ACCOUNT = 67108864;
  33. /**
  34. * Stores the values to be added together to
  35. * build the user account control integer.
  36. *
  37. * @var array
  38. */
  39. protected $values = [];
  40. /**
  41. * Constructor.
  42. *
  43. * @param int $flag
  44. */
  45. public function __construct($flag = null)
  46. {
  47. if (!is_null($flag)) {
  48. $this->apply($flag);
  49. }
  50. }
  51. /**
  52. * Returns the account control integer as a string
  53. * when the object is casted as a string.
  54. *
  55. * @return string
  56. */
  57. public function __toString()
  58. {
  59. return (string) $this->getValue();
  60. }
  61. /**
  62. * Returns the account control integer when
  63. * the object is casted as an integer.
  64. *
  65. * @return int
  66. */
  67. public function __toInt()
  68. {
  69. return $this->getValue();
  70. }
  71. /**
  72. * Applies the specified flag.
  73. *
  74. * @param $flag
  75. */
  76. public function apply($flag)
  77. {
  78. $flags = [];
  79. for ($i = 0; $i <= 26; $i++) {
  80. if ((int) $flag & (1 << $i)) {
  81. array_push($flags, 1 << $i);
  82. }
  83. }
  84. $this->setValues($flags);
  85. }
  86. /**
  87. * The logon script will be run.
  88. *
  89. * @return AccountControl
  90. */
  91. public function runLoginScript()
  92. {
  93. $this->applyValue(self::SCRIPT);
  94. return $this;
  95. }
  96. /**
  97. * The user account is locked.
  98. *
  99. * @return AccountControl
  100. */
  101. public function accountIsLocked()
  102. {
  103. $this->applyValue(self::LOCKOUT);
  104. return $this;
  105. }
  106. /**
  107. * The user account is disabled.
  108. *
  109. * @return AccountControl
  110. */
  111. public function accountIsDisabled()
  112. {
  113. $this->applyValue(self::ACCOUNTDISABLE);
  114. return $this;
  115. }
  116. /**
  117. * This is an account for users whose primary account is in another domain.
  118. *
  119. * This account provides user access to this domain, but not to any domain that
  120. * trusts this domain. This is sometimes referred to as a local user account.
  121. *
  122. * @return AccountControl
  123. */
  124. public function accountIsTemporary()
  125. {
  126. $this->applyValue(self::TEMP_DUPLICATE_ACCOUNT);
  127. return $this;
  128. }
  129. /**
  130. * This is a default account type that represents a typical user.
  131. *
  132. * @return AccountControl
  133. */
  134. public function accountIsNormal()
  135. {
  136. $this->applyValue(self::NORMAL_ACCOUNT);
  137. return $this;
  138. }
  139. /**
  140. * This is a permit to trust an account for a system domain that trusts other domains.
  141. *
  142. * @return AccountControl
  143. */
  144. public function accountIsForInterdomain()
  145. {
  146. $this->applyValue(self::INTERDOMAIN_TRUST_ACCOUNT);
  147. return $this;
  148. }
  149. /**
  150. * This is a computer account for a computer that is running Microsoft
  151. * Windows NT 4.0 Workstation, Microsoft Windows NT 4.0 Server, Microsoft
  152. * Windows 2000 Professional, or Windows 2000 Server and is a member of this domain.
  153. *
  154. * @return AccountControl
  155. */
  156. public function accountIsForWorkstation()
  157. {
  158. $this->applyValue(self::WORKSTATION_TRUST_ACCOUNT);
  159. return $this;
  160. }
  161. /**
  162. * This is a computer account for a domain controller that is a member of this domain.
  163. *
  164. * @return AccountControl
  165. */
  166. public function accountIsForServer()
  167. {
  168. $this->applyValue(self::SERVER_TRUST_ACCOUNT);
  169. return $this;
  170. }
  171. /**
  172. * This is an MNS logon account.
  173. *
  174. * @return AccountControl
  175. */
  176. public function accountIsMnsLogon()
  177. {
  178. $this->applyValue(self::MNS_LOGON_ACCOUNT);
  179. return $this;
  180. }
  181. /**
  182. * (Windows 2000/Windows Server 2003) This account does
  183. * not require Kerberos pre-authentication for logging on.
  184. *
  185. * @return AccountControl
  186. */
  187. public function accountDoesNotRequirePreAuth()
  188. {
  189. $this->applyValue(self::DONT_REQ_PREAUTH);
  190. return $this;
  191. }
  192. /**
  193. * When this flag is set, it forces the user to log on by using a smart card.
  194. *
  195. * @return AccountControl
  196. */
  197. public function accountRequiresSmartCard()
  198. {
  199. $this->applyValue(self::SMARTCARD_REQUIRED);
  200. return $this;
  201. }
  202. /**
  203. * (Windows Server 2008/Windows Server 2008 R2) The account is a read-only domain controller (RODC).
  204. *
  205. * This is a security-sensitive setting. Removing this setting from an RODC compromises security on that server.
  206. *
  207. * @return AccountControl
  208. */
  209. public function accountIsReadOnly()
  210. {
  211. $this->applyValue(self::PARTIAL_SECRETS_ACCOUNT);
  212. return $this;
  213. }
  214. /**
  215. * The home folder is required.
  216. *
  217. * @return AccountControl
  218. */
  219. public function homeFolderIsRequired()
  220. {
  221. $this->applyValue(self::HOMEDIR_REQUIRED);
  222. return $this;
  223. }
  224. /**
  225. * No password is required.
  226. *
  227. * @return AccountControl
  228. */
  229. public function passwordIsNotRequired()
  230. {
  231. $this->applyValue(self::PASSWD_NOTREQD);
  232. return $this;
  233. }
  234. /**
  235. * The user cannot change the password. This is a permission on the user's object.
  236. *
  237. * For information about how to programmatically set this permission, visit the following Web site:
  238. * http://msdn2.microsoft.com/en-us/library/aa746398.aspx
  239. *
  240. * @return AccountControl
  241. */
  242. public function passwordCannotBeChanged()
  243. {
  244. $this->applyValue(self::PASSWD_NOTREQD);
  245. return $this;
  246. }
  247. /**
  248. * Represents the password, which should never expire on the account.
  249. *
  250. * @return AccountControl
  251. */
  252. public function passwordDoesNotExpire()
  253. {
  254. $this->applyValue(self::DONT_EXPIRE_PASSWORD);
  255. return $this;
  256. }
  257. /**
  258. * (Windows 2000/Windows Server 2003) The user's password has expired.
  259. *
  260. * @return AccountControl
  261. */
  262. public function passwordIsExpired()
  263. {
  264. $this->applyValue(self::PASSWORD_EXPIRED);
  265. return $this;
  266. }
  267. /**
  268. * The user can send an encrypted password.
  269. *
  270. * @return AccountControl
  271. */
  272. public function allowEncryptedTextPassword()
  273. {
  274. $this->applyValue(self::ENCRYPTED_TEXT_PWD_ALLOWED);
  275. return $this;
  276. }
  277. /**
  278. * When this flag is set, the service account (the user or computer account)
  279. * under which a service runs is trusted for Kerberos delegation.
  280. *
  281. * Any such service can impersonate a client requesting the service.
  282. *
  283. * To enable a service for Kerberos delegation, you must set this
  284. * flag on the userAccountControl property of the service account.
  285. *
  286. * @return AccountControl
  287. */
  288. public function trustForDelegation()
  289. {
  290. $this->applyValue(self::TRUSTED_FOR_DELEGATION);
  291. return $this;
  292. }
  293. /**
  294. * (Windows 2000/Windows Server 2003) The account is enabled for delegation.
  295. *
  296. * This is a security-sensitive setting. Accounts that have this option enabled
  297. * should be tightly controlled. This setting lets a service that runs under the
  298. * account assume a client's identity and authenticate as that user to other remote
  299. * servers on the network.
  300. *
  301. * @return AccountControl
  302. */
  303. public function trustToAuthForDelegation()
  304. {
  305. $this->applyValue(self::TRUSTED_TO_AUTH_FOR_DELEGATION);
  306. return $this;
  307. }
  308. /**
  309. * When this flag is set, the security context of the user is not delegated to a
  310. * service even if the service account is set as trusted for Kerberos delegation.
  311. *
  312. * @return AccountControl
  313. */
  314. public function doNotTrustForDelegation()
  315. {
  316. $this->applyValue(self::NOT_DELEGATED);
  317. return $this;
  318. }
  319. /**
  320. * (Windows 2000/Windows Server 2003) Restrict this principal to
  321. * use only Data Encryption Standard (DES) encryption types for keys.
  322. *
  323. * @return AccountControl
  324. */
  325. public function useDesKeyOnly()
  326. {
  327. $this->applyValue(self::USE_DES_KEY_ONLY);
  328. return $this;
  329. }
  330. /**
  331. * Returns the complete account control integer.
  332. *
  333. * @return int
  334. */
  335. public function getValue()
  336. {
  337. $total = 0;
  338. foreach ($this->values as $value) {
  339. $total = $total + $value;
  340. }
  341. return $total;
  342. }
  343. /**
  344. * Returns the account control's values.
  345. *
  346. * @return array
  347. */
  348. public function getValues()
  349. {
  350. return $this->values;
  351. }
  352. /**
  353. * Sets the account control values.
  354. *
  355. * @param array $flags
  356. */
  357. public function setValues(array $flags)
  358. {
  359. $this->values = $flags;
  360. }
  361. /**
  362. * Applies the inserted value to the values property array.
  363. *
  364. * @param $value
  365. */
  366. protected function applyValue($value)
  367. {
  368. // Use the value as a key so if the same value
  369. // is used, it will always be overwritten
  370. $this->values[$value] = $value;
  371. }
  372. }