You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.1 KiB

12 years ago
  1. <?
  2. session_start();
  3. $response=array();
  4. if (isset($_POST['username'])) {
  5. $_SESSION['username']=$_POST['username'];
  6. $_SESSION['password']=$_POST['password'];
  7. }
  8. if (!isset($_SESSION['username'])) {
  9. $response['error']='Credenziali non valide';
  10. echo json_encode($response);
  11. exit;
  12. }
  13. if (isset($_GET['logout'])){
  14. session_destroy();
  15. $response['info']='Logout effettuato';
  16. $response['loggedin']=False;
  17. echo json_encode($response);
  18. exit;
  19. }
  20. include 'config.php';
  21. include 'lilikuser.php';
  22. $u=new LilikUser($_SESSION['username']);
  23. #bind ldap or exit
  24. $loggedin=$u->check_password($_SESSION['password']);
  25. $response['loggedin']=$loggedin;
  26. if (!$loggedin){
  27. $response['error']='Credenziali non valide';
  28. echo json_encode($response);
  29. exit;
  30. }
  31. $admin=$u->is_admin();
  32. $response['admin']=$admin;
  33. $user=$_SESSION['username'];
  34. if (isset($_GET['user']) and $_GET['user']!=$_SESSION['username']){
  35. if ($admin){
  36. $user=$_GET['user'];
  37. }else{
  38. $response['error']='Permessi non sufficienti per visualizzare l\'utente';
  39. echo json_encode($response);
  40. exit;
  41. }
  42. }
  43. if (isset($_POST['new_user'])){
  44. if($admin){
  45. $u3=new LilikUser($user);
  46. $u3->create($_POST['new_name'],$_POST['new_surname'],$_POST['new_passwd']);
  47. $response['success']='Utente creato <a href="/users/'.$user.'/">vai alla pagina</a>';
  48. echo json_encode($response);
  49. exit;
  50. }else{
  51. $response['error']='Permessi non sufficienti per creare l\'utente';
  52. }
  53. }
  54. $u2=new LilikUser($user);
  55. if (!$u2->exist()){
  56. $response['error']='Utente inesistente';
  57. $response['exist']=false;
  58. echo json_encode($response);
  59. exit;
  60. }
  61. $response['avaible_services']=$u2->get_services();
  62. if (isset($_POST['new_password'])) {
  63. if(!$admin and !$u2->check_password($_POST['old_password'])){
  64. $response['error']='Password errata';
  65. }else{
  66. if ($_POST['new_password']==$_POST['new_password2'] and $_POST['new_password']!=''){
  67. $u2->set_attr('userPassword', $_POST['new_password']);
  68. $response['edit']=True;
  69. if ($user==$_SESSION['username']){
  70. $_SESSION['password']=$_POST['new_password'];
  71. }
  72. }else{
  73. $response['error']='La password non coincide';
  74. }
  75. }
  76. }
  77. if (isset($_POST['services'])) {
  78. if($admin){
  79. $response['edit']=True;
  80. $actual=$u2->get_enabled_services();
  81. $to_disable=array_diff($actual, $_POST['services']);
  82. $to_enable=array_diff($_POST['services'], $actual);
  83. foreach (["enable"=>$to_enable, "disable"=>$to_disable] as $function=>$services){
  84. foreach ($services as $service){
  85. try {
  86. $u2->$function($service);
  87. } catch (Exception $e) {
  88. if ($e->getMessage()=="Service not found"){
  89. $response['error']="Servizio sconosciuto: $service";
  90. }else{
  91. throw $e;
  92. }
  93. }
  94. }
  95. }
  96. }else{
  97. $response['error']='Permessi non sufficienti';
  98. }
  99. }
  100. if (isset($_POST['cn'])) {
  101. if($admin){
  102. $response['edit']=True;
  103. if ($u2->get_attr('cn')!=$_POST['cn']){
  104. $u2->set_attr('cn', $_POST['cn']);
  105. }
  106. }else{
  107. $response['error']='Permessi non sufficienti';
  108. }
  109. }
  110. if ($response['edit']==True and !isset($response['error'])){
  111. $response['success']='Salvataggio effettuato';
  112. }
  113. $response['services']=$u2->get_enabled_services();
  114. $response['username']=$user;
  115. $response['cn']=$u2->get_attr('cn');
  116. echo json_encode($response);
  117. ?>