User.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-12)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\models;
  20. use MGProvision\Proxmox\v2\ProxmoxApiException;
  21. /**
  22. * Description of User
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class User extends AbstractObject
  28. {
  29. protected $userid;
  30. public function __construct($userid = null)
  31. {
  32. $this->userid = $userid;
  33. }
  34. public function setUserid($userid)
  35. {
  36. if (empty($userid))
  37. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  38. $this->userid = $userid;
  39. return $this;
  40. }
  41. public function getUserid()
  42. {
  43. return $this->userid;
  44. }
  45. public function updatePermission($vmid, $roles, $delete = 0)
  46. {
  47. if (empty($this->userid))
  48. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  49. if (empty($vmid))
  50. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("VMID is empty");
  51. $parameter = array(
  52. "path" => "/vms/" . $vmid,
  53. "delete" => $delete,
  54. "roles" => $roles,
  55. "users" => $this->userid,
  56. );
  57. return $this->api()->put("/access/acl", $parameter);
  58. }
  59. public function permissions()
  60. {
  61. return $this->api()->get("/access/acl");
  62. }
  63. public function create($parameters)
  64. {
  65. $res = $this->api()->post("/access/users", $parameters);
  66. $this->userid = $parameters['userid'];
  67. return $res;
  68. }
  69. public function update($parameters)
  70. {
  71. if (empty($this->userid))
  72. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  73. return $this->api()->put("/access/users/{$this->userid}", $parameters);
  74. }
  75. public function disable()
  76. {
  77. if (empty($this->userid))
  78. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  79. return $this->api()->put("/access/users/{$this->userid}", array("enable" => "0"));
  80. }
  81. public function enable()
  82. {
  83. if (empty($this->userid))
  84. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  85. return $this->api()->put("/access/users/{$this->userid}", array("enable" => "1"));
  86. }
  87. public function changePassword($password)
  88. {
  89. if (empty($this->userid))
  90. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  91. $data = array(
  92. "userid" => $this->userid,
  93. "password" => $password
  94. );
  95. return $this->api()->put("/access/password", $data);
  96. }
  97. public function delete()
  98. {
  99. if (empty($this->userid))
  100. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  101. return $this->api()->delete("/access/users/" . $this->userid);
  102. }
  103. public function configuration()
  104. {
  105. if (empty($this->userid))
  106. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("UserId is empty");
  107. return $this->api()->get("/access/users/" . $this->userid);
  108. }
  109. public function exist(){
  110. try {
  111. $this->configuration();
  112. return true;
  113. }catch (ProxmoxApiException $exception){
  114. return false;
  115. }
  116. }
  117. }