1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/dotclear2_ynh.git synced 2024-09-03 18:26:29 +02:00
dotclear2_ynh/conf/class.auth.ldap.php

81 lines
4.2 KiB
PHP
Raw Normal View History

2019-03-01 02:05:03 +01:00
<?php
2019-02-28 01:43:05 +01:00
class myDcAuth extends dcAuth
{
2019-03-01 22:48:44 +01:00
# The user can't change his password
2019-03-01 02:05:03 +01:00
protected $allow_pass_change = false;
2019-02-28 01:43:05 +01:00
2019-03-01 02:05:03 +01:00
public function checkUser($user_id, $pwd=null, $user_key=null, $check_blog=true)
{
if ($pwd == '') {
return parent::checkUser($user_id, null, $user_key, $check_blog);
}
2019-02-28 01:43:05 +01:00
2019-03-01 02:05:03 +01:00
$this->con->begin();
$cur = $this->con->openCursor($this->user_table);
2019-02-28 01:43:05 +01:00
2019-03-01 22:48:44 +01:00
# LDAP parameter
2019-03-01 02:05:03 +01:00
$server = "localhost";
$port = "389";
$racine = "dc=yunohost,dc=org";
2019-02-28 01:43:05 +01:00
2019-03-01 22:48:44 +01:00
# LDAP connection
2019-03-01 02:05:03 +01:00
$ds=ldap_connect($server);
ldap_set_option ($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if (ldap_bind($ds,"uid=".$user_id.",ou=users,dc=yunohost,dc=org",$pwd))
{
2019-03-01 22:48:44 +01:00
# Store the password
2019-03-01 02:05:03 +01:00
$cur->user_pwd = $pwd;
2019-02-28 01:43:05 +01:00
2019-03-01 22:48:44 +01:00
# If the user exist, then we just update his password.
2019-03-01 02:05:03 +01:00
if ($this->core->userExists($user_id))
{
$this->sudo(array($this->core,'updUser'),$user_id,$cur);
$this->con->commit();
}
2019-03-01 22:48:44 +01:00
# If not, we create him.
# In order for him to connect,
# it is necessary to give him at least
# a permission "usage" on the blog "default".
2019-03-01 02:05:03 +01:00
else
{
2019-03-01 22:48:44 +01:00
# search the user in ldap, and get infos
$sr=ldap_search($ds,$racine,"uid=$user_id",array( "dn", "cn", "sn", "mail", "givenname")); # /!\ fields have to be in lowercase
2019-03-01 02:05:03 +01:00
$info = ldap_get_entries($ds, $sr);
2019-02-28 01:43:05 +01:00
2019-03-01 02:05:03 +01:00
if ($info["count"] ==1)
{
$cur->user_id = $user_id;
$cur->user_email = $info[0]['mail'][0];
2019-03-01 22:48:44 +01:00
$cur->user_name = $info[0]['givenname'][0];
2019-03-01 02:05:03 +01:00
$cur->user_firstname = $info[0]['sn'][0];
2019-03-01 22:48:44 +01:00
$cur->user_lang = 'fr'; # Can change this, PR are welcome
$cur->user_tz = 'Europe/Paris'; # Can change this, PR are welcome
$cur->user_default_blog = 'default'; # Can change this, PR are welcome
2019-03-01 02:05:03 +01:00
$this->sudo(array($this->core,'addUser'),$cur);
2019-03-01 22:48:44 +01:00
# Possible roles:
2019-03-01 02:15:21 +01:00
#admin "administrator"
#usage "manage their own entries and comments"
#publish "publish entries and comments"
#delete "delete entries and comments"
#contentadmin "manage all entries and comments"
#categories "manage categories"
#media "manage their own media items"
#media_admin "manage all media items"
#pages "manage pages"
#blogroll "manage blogroll"
2019-03-01 22:48:44 +01:00
$this->sudo(array($this->core,'setUserBlogPermissions'),$user_id,'default',array('usage'=>true)); # Can change this, PR are welcome
2019-03-01 02:05:03 +01:00
$this->con->commit();
}
}
2019-02-28 01:43:05 +01:00
2019-03-01 22:48:44 +01:00
# The previous operations proceeded without error,
# we can now call the parent method
2019-03-01 02:05:03 +01:00
return parent::checkUser($user_id, $pwd, $user_key, $check_blog);
}
2019-03-01 22:48:44 +01:00
# In case of error we cancel and return "false"
2019-03-01 02:05:03 +01:00
$this->con->rollback();
return false;
}
2019-02-28 01:43:05 +01:00
}
?>