@ -0,0 +1,8 @@ | |||||
<?php | |||||
$PORT = 389; | |||||
$HOST = "ldap.lilik.it"; | |||||
$LOGIN_DN=''; | |||||
$LOGIN_PASS=''; | |||||
?> |
@ -0,0 +1,249 @@ | |||||
<!DOCTYPE html> | |||||
<html> | |||||
<head> | |||||
<script type="text/javascript" src="/jquery-1.9.1.js"></script> | |||||
<!-- <script type="text/javascript" src="/jquery.validate.js"></script>--> | |||||
<script type="text/javascript" src="/js/bootstrap.js"></script> | |||||
<!-- <script type="text/javascript" src="jquery.dform-1.0.1.js"></script>--> | |||||
<script type="text/javascript"> | |||||
var service_hash={'admin':{'text':'Gestisci gli utenti del LILiK', 'href': 'http://login.lilik.it'}, | |||||
'mail':{'text':'Controlla la tua casella @lilik.it', 'href': 'http://webmail.lilik.it'}, | |||||
'ltsp':{'text':'Accedi ai client del LILiK'}, | |||||
'users':{'text':'Hai un utente sulla macchina users', 'href': 'http://users.lilik.it'}, | |||||
'wiki':{'text':'Modifica la wiki', 'href': 'http://wiki.lilik.it'}, | |||||
'public_html':{'text':'Hai un sito web sulla macchina users', 'href': 'http://users.lilik.it'}, | |||||
'lilik.it':{'text':'Accedi con il tuo utente a lilik.it', 'href': 'http://www.lilik.it'}, | |||||
'cloud':{'text':'Accedi alla piattaforma cloud', 'href': 'http://cloud.lilik.it'}, | |||||
'projects':{'text':'Accedi al portale progetti', 'href': 'http://projects.lilik.it'}, | |||||
'teambox':{'text':'Accedi a teambox', 'href': 'http://teambox.lilik.it'} | |||||
}; | |||||
function parsedata(data){ | |||||
$('#content').text(''); | |||||
$('#alerts').text(''); | |||||
$('#services').text(''); | |||||
if (!data['loggedin']){ | |||||
$('#loginModal').modal('show'); | |||||
} | |||||
if (data['admin']==true){ | |||||
$('#search-form').show(); | |||||
$('#old_password').hide() | |||||
}else{ | |||||
$('#search-form').hide(); | |||||
$('#old_password').show() | |||||
} | |||||
if ('username' in data){ | |||||
$('#content').append($('<h2 class="">').text(data['username'])); | |||||
var cn=$('<h5>').text(data['cn']); | |||||
if (data['admin']==true){ | |||||
cn=$('<input type="text" name="cn" placeholder="Nome completo" >').val(data['cn']); | |||||
} | |||||
$('#content').append($('<div class="row-fluid">').append($('<div class="span4">').append(cn)).append($('<div class="span8">').append($('<a href="#passwordModal" role="button" class="btn inline" data-toggle="modal">').text('Cambia password')))); | |||||
} | |||||
['error', 'info', 'success'].forEach(function(what){ | |||||
if (what in data){ | |||||
$('#alerts').append($('<div class="alert alert-block alert-'+what+'">').text(data[what]).append('uup-').fadeIn(700)) | |||||
} | |||||
}); | |||||
if ('avaible_services' in data){ | |||||
services=data['services']; | |||||
if (data['admin']==true){ | |||||
var fieldset=$('<fieldset>'); | |||||
$('#content').append(fieldset); | |||||
data['avaible_services'].forEach(function(service){ | |||||
tmp=$('<label>'); | |||||
tmp=tmp.prepend($('<input name="services[]" type="checkbox" value="'+service+'" '+((services.indexOf(service)>=0)?'checked="true"':'')+'>')); | |||||
tmp.append((service in service_hash)?service_hash[service]['text']:service); | |||||
fieldset.append(tmp); | |||||
}); | |||||
}else{ | |||||
$('#content').append($('<ul id="services">')); | |||||
services.forEach(function(service){ | |||||
var tmp=$('<li>'); | |||||
tmp.text((service in service_hash && 'text' in service_hash[service])?service_hash[service]['text']:service); | |||||
if (service in service_hash && 'href' in service_hash[service]){ | |||||
tmp.wrapInner($('<a target="_blank">').attr('href', service_hash[service]['href'])); | |||||
} | |||||
$('#services').append(tmp); | |||||
}); | |||||
} | |||||
} | |||||
if (data['admin']==true & data['exist']!=false & $('#content').text()!=''){ | |||||
$('#content').append($('<button type="submit" class="btn btn-primary">Salva</button>')); | |||||
$('#content').wrapInner($('<form data-async action="user.php" method="POST">')); | |||||
} | |||||
} | |||||
function get(url){ | |||||
$.getJSON(url).done(parsedata); | |||||
} | |||||
$(document).ready(function() { | |||||
$("#search-user").change(function() { | |||||
$("#search-form").attr("action", "/users/" + $(this).val() + "/"); | |||||
}); | |||||
$("#inputUsername").change(function() { | |||||
$("#newUserForm").attr("action", "/users/" + $(this).val() + "/user.php"); | |||||
}); | |||||
jQuery(function($) { | |||||
$("body").on('submit', 'form[data-async]', function(event) { | |||||
var $form = $(this); | |||||
$.ajax({ | |||||
type: $form.attr('method'), | |||||
url: $form.attr('action'), | |||||
data: $form.serialize(), | |||||
dataType: 'json', | |||||
success: function(data, status) { | |||||
parsedata(data); | |||||
$('#loginModal').modal('hide'); | |||||
$('#passwordModal').modal('hide'); | |||||
$('#newUserModal').modal('hide'); | |||||
} | |||||
}); | |||||
$form.find('input:password').val(''); | |||||
event.preventDefault(); | |||||
}); | |||||
}); | |||||
get('user.php'); | |||||
}); | |||||
</script> | |||||
<link href="/css/bootstrap.css" rel="stylesheet" media="screen"> | |||||
<style type="text/css"> | |||||
form.form-modal{ | |||||
margin-bottom: 0px; | |||||
} | |||||
</style> | |||||
</head> | |||||
<body> | |||||
<div id="container" class="container"> | |||||
<div class="navbar"> | |||||
<div class="navbar-inner"> | |||||
<a class="brand" href="/">Lilik User</a> | |||||
<ul class="nav"> | |||||
<li class="active"><a href="#">Home</a></li> | |||||
<li><a href="#newUserModal" data-toggle="modal">Crea utente</a></li> | |||||
<li><a href="javascript:get('/user.php?logout')">Logout</a></li> | |||||
</ul> | |||||
<form id="search-form" class="navbar-search pull-right"> | |||||
<input id="search-user" type="text" class="search-query" placeholder="Cerca"> | |||||
</form> | |||||
</div> | |||||
</div> | |||||
<div id="alerts"></div> | |||||
<div id="content"></div> | |||||
<div id="newUserModal" class="modal hide fade"> | |||||
<div class="modal-header"> | |||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> | |||||
<h3>Crea utente</h3> | |||||
</div> | |||||
<form data-async action="user.php" method="POST" id="newUserForm" class="form-horizontal form-modal"> | |||||
<div class="modal-body"> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputUsername">Username</label> | |||||
<div class="controls"> | |||||
<input type="text" id="inputUsername" placeholder="username" name="new_username"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputName">Nome</label> | |||||
<div class="controls"> | |||||
<input type="text" id="inputName" placeholder="name" name="new_name"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputSurname">Cognome</label> | |||||
<div class="controls"> | |||||
<input type="text" id="inputSurname" placeholder="surname" name="new_surname"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputPassword">Password</label> | |||||
<div class="controls"> | |||||
<input type="password" id="inputPassword" placeholder="password" name="new_passwd"> | |||||
</div> | |||||
</div> | |||||
<input type="hidden" name="new_user" value="true"> | |||||
</div> | |||||
<div class="modal-footer"> | |||||
<a href="#" class="btn" data-toggle="modal" data-target="#newUserModal">Chiudi</a> | |||||
<button type="submit" class="btn btn-primary">Salva</button> | |||||
</div> | |||||
</form> | |||||
</div> | |||||
<div id="passwordModal" class="modal hide fade"> | |||||
<div class="modal-header"> | |||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> | |||||
<h3>Cambia password</h3> | |||||
</div> | |||||
<form data-async action="user.php" method="POST" class="form-horizontal form-modal"> | |||||
<div class="modal-body"> | |||||
<div class="control-group" id="old_password" > | |||||
<label class="control-label" for="inputOldPassword">Password attuale</label> | |||||
<div class="controls"> | |||||
<input type="text" id="inputOldPassword" placeholder="old password" name="old_password"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputPassword">Password</label> | |||||
<div class="controls"> | |||||
<input type="password" id="inputPassword" placeholder="new password" name="new_password"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputPassword2">Reimmetti password</label> | |||||
<div class="controls"> | |||||
<input type="password" id="inputPassword2" placeholder="new password" name="new_password2"> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
<div class="modal-footer"> | |||||
<a href="#" class="btn" data-toggle="modal" data-target="#passwordModal">Chiudi</a> | |||||
<button type="submit" class="btn btn-primary">Salva</button> | |||||
</div> | |||||
</form> | |||||
</div> | |||||
<div id="loginModal" class="modal hide fade"> | |||||
<div class="modal-header"> | |||||
<h3>Login</h3> | |||||
</div> | |||||
<form data-async action="user.php" method="POST" class="form-horizontal form-modal"> | |||||
<div class="modal-body"> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputUsername">Username</label> | |||||
<div class="controls"> | |||||
<input type="text" id="inputUsername" placeholder="username" name="username"> | |||||
</div> | |||||
</div> | |||||
<div class="control-group"> | |||||
<label class="control-label" for="inputPassword">Password</label> | |||||
<div class="controls"> | |||||
<input type="password" id="inputPassword" placeholder="password" name="password"> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
<div class="modal-footer"> | |||||
<button type="submit" class="btn btn-primary">Login</button> | |||||
</div> | |||||
</form> | |||||
</div> | |||||
</div> | |||||
</body> | |||||
</html> |
@ -0,0 +1,458 @@ | |||||
<? | |||||
$ssha=function($pw){ | |||||
if (!in_array(strtolower(substr($pw, 0, 5)), array('{ssha', '{cryp', '{sha}',))){ | |||||
$salt = substr(md5(uniqid(mt_rand(), true)), 0, 4); | |||||
return '{SSHA}'.base64_encode( sha1( $pw . $salt, true) . $salt ); | |||||
} | |||||
return $pw; | |||||
}; | |||||
class LdapAdapter{ | |||||
function __construct($host, $port, $user, $pass){ | |||||
$this->conn = ldap_connect($host,$port); | |||||
ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3); | |||||
ldap_bind($this->conn, $user, $pass); | |||||
} | |||||
function bind($host, $port, $user, $pass){ | |||||
$conn = ldap_connect($host,$port); | |||||
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3); | |||||
return ldap_bind($conn, $user, $pass); | |||||
} | |||||
function get_ldap_attr($dn, $attr){ | |||||
# $search = ldap_search($this->conn, $dn, "$attr=*", array($attr)); | |||||
$dn_array=ldap_explode_dn($dn, 0); | |||||
$new_dn=array(); | |||||
for ($i=1; $i<$dn_array['count']; $i++){ | |||||
$new_dn[]=$dn_array[$i]; | |||||
} | |||||
$search=ldap_search($this->conn, implode(',', $new_dn), $dn_array[0]); | |||||
if (ldap_count_entries($this->conn, $search)==0){ | |||||
return Null; | |||||
} | |||||
$entry = ldap_first_entry($this->conn, $search); | |||||
return ldap_get_values ($this->conn, $entry, $attr); | |||||
} | |||||
function get_ldap_attr_max($dn, $attr){ | |||||
$search = ldap_search($this->conn, $dn, "$attr=*", array($attr)); | |||||
$entry = ldap_first_entry($this->conn, $search); | |||||
$max=0; | |||||
while( $entry){ | |||||
$tmp=ldap_get_values ($this->conn, $entry, $attr)[0]; | |||||
if ($tmp>$max){ | |||||
$max=$tmp; | |||||
} | |||||
$entry = ldap_next_entry($this->conn, $search); | |||||
} | |||||
return $max; | |||||
} | |||||
function add_ldap_obj($dn, $obj){ | |||||
return ldap_add($this->conn, $dn, $obj); | |||||
} | |||||
function exist_ldap_obj($dn){ | |||||
$dn_array=ldap_explode_dn($dn, 0); | |||||
$new_dn=array(); | |||||
for ($i=1; $i<$dn_array['count']; $i++){ | |||||
$new_dn[]=$dn_array[$i]; | |||||
} | |||||
$search=ldap_search($this->conn, implode(',', $new_dn), $dn_array[0]); | |||||
if (ldap_count_entries($this->conn, $search)==0){ | |||||
return False; | |||||
} | |||||
return True; | |||||
} | |||||
function set_ldap_attr($dn, $attr, $value){ | |||||
return ldap_modify($this->conn, $dn , array($attr=>$value)); | |||||
} | |||||
} | |||||
abstract class Adapter{ | |||||
function __construct($dn){ | |||||
$this->dn = $dn; | |||||
$this->setters = array(); | |||||
$this->getters = array(); | |||||
} | |||||
function add_getter($attr, $fn = Null){ | |||||
if (array_key_exists($attr, $this->getters)){ | |||||
$this->getters[$attr][]= $fn; | |||||
}else{ | |||||
$this->getters[$attr] = array($fn); | |||||
} | |||||
} | |||||
function add_setter($attr, $fn = Null){ | |||||
if (array_key_exists($attr, $this->setters)){ | |||||
$this->setters[$attr][]= $fn; | |||||
}else{ | |||||
$this->setters[$attr] = array($fn); | |||||
} | |||||
} | |||||
function get_attr($la, $username, $attr){ | |||||
if (array_key_exists($attr, $this->getters)){ | |||||
foreach ($this->getters[$attr] as $getter){ | |||||
$result=$la->get_ldap_attr(sprintf($this->dn, $username), $attr); | |||||
if ($getter != Null) | |||||
return $getter($result); | |||||
return $result; | |||||
} | |||||
} | |||||
return Null; | |||||
} | |||||
function set_attr($la, $username, $attr, $value){ | |||||
$done=False; | |||||
if (array_key_exists($attr, $this->setters)){ | |||||
$done=True; | |||||
foreach ($this->setters[$attr] as $setter){ | |||||
if ($setter != Null) | |||||
$value=$setter($value); | |||||
$la->set_ldap_attr(sprintf($this->dn, $username), $attr, $value); | |||||
} | |||||
} | |||||
return $done; | |||||
} | |||||
function get_attributes(){ | |||||
$attributes=array(); | |||||
foreach ($this->setters as $setter => $value) | |||||
$attributes[]=$setter; | |||||
return $attributes; | |||||
} | |||||
function exist($la, $username){ | |||||
if ($la->exist_ldap_obj(sprintf($this->dn, $username))){ | |||||
return True; | |||||
} | |||||
return False; | |||||
} | |||||
function bind($username, $password){ | |||||
global $HOST, $PORT, $LOGIN_DN; | |||||
return LdapAdapter::bind($HOST, $PORT, sprintf($this->dn,$username), $password); | |||||
} | |||||
} | |||||
class MailAdapter extends Adapter{ | |||||
function __construct($dn){ | |||||
global $ssha; | |||||
parent::__construct($dn); | |||||
$this->add_getter('mail', function($x){return $x[0];}); # function($x){return substr($x, strlen("@lilik.it")*-1);} | |||||
$this->add_setter('mail'); # function($x){return $x[0]."@lilik.it";} | |||||
$this->add_getter('cn', function($x){return $x[0];}); | |||||
$this->add_setter('cn'); | |||||
$this->add_getter('userPassword', function($x){return $x[0];}); | |||||
$this->add_setter('userPassword', $ssha); | |||||
$this->add_getter('accountActive', function($x){return $x[0];}); | |||||
$this->add_setter('accountActive'); | |||||
} | |||||
function create($la, $id, $name, $surname, $password){ | |||||
global $ssha; | |||||
$new_mail_obj=['cn'=> [sprintf('%s %s',$name,$surname)], | |||||
'accountActive'=> ['FALSE'], | |||||
'objectClass'=> ['top', 'VirtualMailAccount', 'Vacation', 'VirtualForward', 'amavisAccount'], | |||||
'smtpAuth'=> ['FALSE'], | |||||
'mailAutoreply'=> [sprintf('%s@lilik.it.autoreply',$id)], | |||||
'uid'=> [sprintf('%s.lilik.it',$id)], | |||||
'vacationEnd'=> ['200701010000'], | |||||
'userPassword'=> [$ssha($password)], | |||||
'amavisBypassSpamChecks'=> ['FALSE'], | |||||
'amavisSpamTagLevel'=> ['3.0'], | |||||
'otherTransport'=> ['phamm=>'], | |||||
'vacationInfo'=> ['vacation'], | |||||
'mail'=> [sprintf('%s@lilik.it',$id)], | |||||
'vacationStart'=> ['200701010000'], | |||||
'vacationActive'=> ['FALSE'], | |||||
'amavisSpamTag2Level'=> ['5.5'], | |||||
'vdHome'=> [sprintf('/home/mail_deliver/lilik.it/%s',$id)], | |||||
'quota'=> ['1024000'], | |||||
'mailbox'=> [sprintf('lilik.it/%s/',$id)], | |||||
'forwardActive'=> ['FALSE'], | |||||
'amavisBypassVirusChecks'=> ['FALSE'], | |||||
'sn'=> [sprintf('%s',$surname)], | |||||
'amavisSpamKillLevel'=> ['6.0'], | |||||
'givenName'=> [sprintf('%s',$name)], | |||||
'delete'=> ['FALSE'], | |||||
'lastChange'=> [sprintf('%d',time())] | |||||
]; | |||||
return $la->add_ldap_obj(sprintf($this->dn,$id),$new_mail_obj); | |||||
} | |||||
} | |||||
class PosixAdapter extends Adapter{ | |||||
function __construct($dn){ | |||||
global $ssha; | |||||
parent::__construct($dn); | |||||
$this->add_getter('cn', function($x){return $x[0];}); | |||||
$this->add_setter('cn'); | |||||
$this->add_getter('host'); | |||||
$this->add_setter('host'); | |||||
$this->add_getter('userPassword', function($x){return $x[0];}); | |||||
$this->add_setter('userPassword', $ssha); | |||||
$this->add_getter('memberOf'); | |||||
$this->add_setter('memberOf'); | |||||
} | |||||
function create($la, $id, $name, $surname, $password){ | |||||
global $ssha; | |||||
$new_posix_obj=['uid'=> [sprintf('%s',$id)], | |||||
'objectClass'=> ['top', 'shadowAccount', 'posixAccount', 'account'], | |||||
'loginShell'=> ['/bin/bash'], | |||||
'userPassword'=> [$ssha($password)], | |||||
'uidNumber'=> [$la->get_ldap_attr_max('o=People,dc=lilik,dc=it', 'uidNumber')], | |||||
'host'=> ['*'], | |||||
'gidNumber'=> ['9000'], #stdusers | |||||
'homeDirectory'=> [sprintf('/home/%s',$id)], | |||||
'cn'=> [sprintf('%s %s',$name,$surname)], | |||||
]; | |||||
return $la->add_ldap_obj(sprintf($this->dn,$id),$new_posix_obj); | |||||
} | |||||
} | |||||
class GroupAdapter extends Adapter{ | |||||
function __construct($dn){ | |||||
parent::__construct($dn); | |||||
$this->add_getter('member'); | |||||
$this->add_setter('member'); | |||||
} | |||||
} | |||||
abstract class pippo{ | |||||
function __construct($id){ | |||||
global $HOST, $PORT, $LOGIN_DN, $LOGIN_PASS; | |||||
$this->id=$id; | |||||
$this->la=new LdapAdapter($HOST, $PORT, $LOGIN_DN, $LOGIN_PASS); | |||||
} | |||||
function get_attr($attr){ | |||||
foreach($this->adapters as $adapter){ | |||||
$result=$adapter->get_attr($this->la, $this->id, $attr); | |||||
if ($result!=Null) | |||||
return $result; | |||||
} | |||||
return Null; | |||||
} | |||||
function set_attr($attr, $value){ | |||||
$result=False; | |||||
foreach($this->adapters as $adapter){ | |||||
if ($adapter->set_attr($this->la, $this->id, $attr, $value)){ | |||||
$result=True; | |||||
} | |||||
} | |||||
return $result; | |||||
} | |||||
function add_in_list($attr, $value){ | |||||
$tmp=$this->get_attr($attr); | |||||
if (in_array($value, $tmp)){ | |||||
return True; | |||||
} | |||||
$tmp[]=$value; | |||||
unset($tmp['count']); | |||||
if ($this->set_attr($attr, $tmp)){ | |||||
return True; | |||||
} | |||||
return False; | |||||
} | |||||
function del_in_list($attr, $value){ | |||||
$tmp=$this->get_attr($attr); | |||||
if (!in_array($value, $tmp)){ | |||||
return True; | |||||
} | |||||
if(($key = array_search($value, $tmp)) !== false) { | |||||
unset($tmp[$key]); | |||||
} | |||||
unset($tmp['count']); | |||||
$tmp=array_values($tmp); | |||||
if ($this->set_attr($attr, $tmp)){ | |||||
return True; | |||||
} | |||||
return False; | |||||
} | |||||
function exist(){ | |||||
foreach ($this->adapters as $adapter){ | |||||
if ($adapter->exist($this->la, $this->id)){ | |||||
return True; | |||||
} | |||||
} | |||||
return False; | |||||
} | |||||
function check_password($password){ | |||||
foreach ($this->adapters as $adapter) | |||||
if ($adapter->bind($this->id, $password)) | |||||
return True; | |||||
return False; | |||||
} | |||||
} | |||||
class LilikGroup extends pippo{ | |||||
function __construct($id){ | |||||
global $HOST, $PORT, $LOGIN_DN; | |||||
parent::__construct($id); | |||||
$this->adapters=[new GroupAdapter("cn=%s,o=Group,dc=lilik,dc=it")]; | |||||
} | |||||
function add_user($user){ | |||||
return $this->add_in_list('member', sprintf($user->adapters[1]->dn,$user->id)); | |||||
} | |||||
function del_user($user){ | |||||
return $this->del_in_list('member', sprintf($user->adapters[1]->dn,$user->id)); | |||||
} | |||||
function is_user($user){ | |||||
if (in_array(sprintf($user->adapters[1]->dn,$user->id), $this->get_attr('member'))){ | |||||
return True; | |||||
} | |||||
return False; | |||||
} | |||||
} | |||||
class LilikUser extends pippo{ | |||||
function __construct($id){ | |||||
parent::__construct($id); | |||||
$this->adapters=array(new MailAdapter("mail=%s@lilik.it,vd=lilik.it,o=hosting,dc=lilik,dc=it"), | |||||
new PosixAdapter("uid=%s,o=People,dc=lilik,dc=it")); | |||||
$this->_flag=array('mail'=>'accountActive'); | |||||
$this->_host=array('ltsp'=>'ltsp', | |||||
'users'=>'users'); | |||||
$this->_member=array('admin'=>'admin', | |||||
'wiki'=>'wiki', | |||||
'public_html'=>'public_html', | |||||
'lilik.it'=>'lilik.it', | |||||
'cloud'=>'cloud', | |||||
'projects'=>'projects', | |||||
'teambox'=>'teambox' | |||||
); | |||||
if ($this->exist()){ | |||||
$this->sanitize(); | |||||
} | |||||
} | |||||
function is_admin(){ | |||||
return $this->status('admin'); | |||||
} | |||||
function enable($service){ | |||||
if (array_key_exists($service, $this->_flag)){ | |||||
return $this->set_attr($this->_flag[$service], 'TRUE'); | |||||
}elseif (array_key_exists($service, $this->_host)){ | |||||
return $this->add_in_list('host', $this->_host[$service]); | |||||
}elseif (array_key_exists($service, $this->_member)){ | |||||
$l=new LilikGroup($this->_member[$service]); | |||||
return $l->add_user($this); | |||||
}else{ | |||||
throw new Exception("Service not found"); | |||||
} | |||||
} | |||||
function disable($service){ | |||||
if (array_key_exists($service, $this->_flag)){ | |||||
return $this->set_attr($this->_flag[$service], 'FALSE'); | |||||
}elseif (array_key_exists($service, $this->_host)){ | |||||
return $this->del_in_list('host', $this->_host[$service]); | |||||
}elseif (array_key_exists($service, $this->_member)){ | |||||
$l=new LilikGroup($this->_member[$service]); | |||||
return $l->del_user($this); | |||||
}else{ | |||||
throw new Exception("Service not found"); | |||||
} | |||||
} | |||||
function status($service){ | |||||
if (array_key_exists($service, $this->_flag)){ | |||||
if ($this->get_attr($this->_flag[$service])=='TRUE'){ | |||||
return True; | |||||
} | |||||
return False; | |||||
}elseif (array_key_exists($service, $this->_host)){ | |||||
if (in_array($this->_host[$service], $this->get_attr("host"))){ | |||||
return True; | |||||
} | |||||
return False; | |||||
}elseif (array_key_exists($service, $this->_member)){ | |||||
$l=new LilikGroup($this->_member[$service]); | |||||
return $l->is_user($this); | |||||
}else{ | |||||
throw new Exception("Service not found"); | |||||
} | |||||
} | |||||
function get_attributes(){ | |||||
$attributes=array(); | |||||
foreach ($this->adapters as $adapter) | |||||
$attributes+= $adapter->get_attributes(); | |||||
return $attributes; | |||||
} | |||||
function get_services(){ | |||||
$services=array(); | |||||
foreach (array($this->_flag, $this->_host, $this->_member) as $k) | |||||
foreach ($k as $x => $value) | |||||
$services[]= $x; | |||||
return $services; | |||||
} | |||||
function get_enabled_services(){ | |||||
$enabled_services=array(); | |||||
foreach ($this->get_services() as $service){ | |||||
if ($this->status($service)){ | |||||
$enabled_services[]=$service; | |||||
} | |||||
} | |||||
return $enabled_services; | |||||
} | |||||
function sanitize(){ | |||||
$tmp=explode(' ', $this->get_attr('cn'), 2); | |||||
if (count($tmp) > 1){ | |||||
$name=$tmp[0]; | |||||
$surname=$tmp[1]; | |||||
}else{ | |||||
$name=$this->get_attr('cn'); | |||||
$surname=''; | |||||
} | |||||
return $this->create($name, $surname, $this->get_attr('userPassword')); | |||||
} | |||||
function create($name, $surname, $password){ | |||||
$result=True; | |||||
foreach ($this->adapters as $adapter){ | |||||
if (!$adapter->exist($this->la, $this->id)){ | |||||
if (!$adapter->create($this->la, $this->id, $name, $surname, $password)){ | |||||
$result=False; | |||||
} | |||||
} | |||||
} | |||||
return $result; | |||||
} | |||||
} | |||||
@ -0,0 +1,132 @@ | |||||
<? | |||||
session_start(); | |||||
$response=array(); | |||||
if (isset($_POST['username'])) { | |||||
$_SESSION['username']=$_POST['username']; | |||||
$_SESSION['password']=$_POST['password']; | |||||
} | |||||
if (!isset($_SESSION['username'])) { | |||||
$response['error']='Credenziali non valide'; | |||||
echo json_encode($response); | |||||
exit; | |||||
} | |||||
if (isset($_GET['logout'])){ | |||||
session_destroy(); | |||||
$response['info']='Logout effettuato'; | |||||
$response['loggedin']=False; | |||||
echo json_encode($response); | |||||
exit; | |||||
} | |||||
include 'config.php'; | |||||
include 'lilikuser.php'; | |||||
$u=new LilikUser($_SESSION['username']); | |||||
#bind ldap or exit | |||||
$loggedin=$u->check_password($_SESSION['password']); | |||||
$response['loggedin']=$loggedin; | |||||
if (!$loggedin){ | |||||
$response['error']='Credenziali non valide'; | |||||
echo json_encode($response); | |||||
exit; | |||||
} | |||||
$admin=$u->is_admin(); | |||||
$response['admin']=$admin; | |||||
$user=$_SESSION['username']; | |||||
if (isset($_GET['user']) and $_GET['user']!=$_SESSION['username']){ | |||||
if ($admin){ | |||||
$user=$_GET['user']; | |||||
}else{ | |||||
$response['error']='Permessi non sufficienti per visualizzare l\'utente'; | |||||
echo json_encode($response); | |||||
exit; | |||||
} | |||||
} | |||||
if (isset($_POST['new_user'])){ | |||||
if($admin){ | |||||
$u3=new LilikUser($user); | |||||
$u3->create($_POST['new_name'],$_POST['new_surname'],$_POST['new_passwd']); | |||||
$response['success']='Utente creato <a href="/users/'.$user.'/">vai alla pagina</a>'; | |||||
echo json_encode($response); | |||||
exit; | |||||
}else{ | |||||
$response['error']='Permessi non sufficienti per creare l\'utente'; | |||||
} | |||||
} | |||||
$u2=new LilikUser($user); | |||||
if (!$u2->exist()){ | |||||
$response['error']='Utente inesistente'; | |||||
$response['exist']=false; | |||||
echo json_encode($response); | |||||
exit; | |||||
} | |||||
$response['avaible_services']=$u2->get_services(); | |||||
if (isset($_POST['new_password'])) { | |||||
if(!$admin and !$u2->check_password($_POST['old_password'])){ | |||||
$response['error']='Password errata'; | |||||
}else{ | |||||
if ($_POST['new_password']==$_POST['new_password2'] and $_POST['new_password']!=''){ | |||||
$u2->set_attr('userPassword', $_POST['new_password']); | |||||
$response['edit']=True; | |||||
if ($user==$_SESSION['username']){ | |||||
$_SESSION['password']=$_POST['new_password']; | |||||
} | |||||
}else{ | |||||
$response['error']='La password non coincide'; | |||||
} | |||||
} | |||||
} | |||||
if (isset($_POST['services'])) { | |||||
if($admin){ | |||||
$response['edit']=True; | |||||
$actual=$u2->get_enabled_services(); | |||||
$to_disable=array_diff($actual, $_POST['services']); | |||||
$to_enable=array_diff($_POST['services'], $actual); | |||||
foreach (["enable"=>$to_enable, "disable"=>$to_disable] as $function=>$services){ | |||||
foreach ($services as $service){ | |||||
try { | |||||
$u2->$function($service); | |||||
} catch (Exception $e) { | |||||
if ($e->getMessage()=="Service not found"){ | |||||
$response['error']="Servizio sconosciuto: $service"; | |||||
}else{ | |||||
throw $e; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
}else{ | |||||
$response['error']='Permessi non sufficienti'; | |||||
} | |||||
} | |||||
if (isset($_POST['cn'])) { | |||||
if($admin){ | |||||
$response['edit']=True; | |||||
if ($u2->get_attr('cn')!=$_POST['cn']){ | |||||
$u2->set_attr('cn', $_POST['cn']); | |||||
} | |||||
}else{ | |||||
$response['error']='Permessi non sufficienti'; | |||||
} | |||||
} | |||||
if ($response['edit']==True and !isset($response['error'])){ | |||||
$response['success']='Salvataggio effettuato'; | |||||
} | |||||
$response['services']=$u2->get_enabled_services(); | |||||
$response['username']=$user; | |||||
$response['cn']=$u2->get_attr('cn'); | |||||
echo json_encode($response); | |||||
?> |