Skip to content

Commit 0c9b023

Browse files
committed
pb password
1 parent 8e62475 commit 0c9b023

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

src/bin/changepwd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
config=u.read_config('../etc/config.conf')
1616
ad.set_config(config)
1717
if u.is_backend_concerned(entity):
18-
ad.ad_exec_script(entity,'changepassword.template',entity['payload']['uid']+ " '"+ entity['payload']['oldPassword'] + "' '"+ entity['payload']['newPassword'] +"'")
18+
ad.ad_exec_script(entity,'changepassword.template',entity['payload']['uid']+ ' "'+ entity['payload']['oldPassword'] + '" "'+ entity['payload']['newPassword'] +'"')
1919
else:
2020
u.returcode(0,"not concerned")

src/lib/ad_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def gen_script_from_template(entity,template):
9797
'path': dn_superior(compose_dn(entity)),
9898
'e': u.make_entry_array(entity),
9999
'config': u.get_config(),
100+
'dataStatus' : entity['payload']['dataStatus']
100101
}
102+
101103
environment = jinja2.Environment(loader=FileSystemLoader("../ps1_templates/"))
102104
template = environment.get_template(template)
103105
content=template.render(data)

src/ps1_templates/changepassword.template

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,37 @@ param (
44
[string]$newp
55
)
66

7+
78
Function Test-ADAuthentication {
89
param(
9-
$username,
10-
$password)
11-
12-
(New-Object DirectoryServices.DirectoryEntry "",$username,$password).psbase.name -ne $null
10+
[string]$username,
11+
[string]$password)
12+
((New-Object DirectoryServices.DirectoryEntry -ArgumentList "",$username,$password).psbase.name) -ne $null
1313
}
14-
15-
$test=Test-ADAuthentication -username $user -password $oldp
16-
if ($test -eq $false){
17-
Write-Host "Invalid password"
18-
exit 1
19-
}
20-
21-
try{
22-
Set-ADUser -Identity $user -CannotChangePassword $false
23-
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
24-
Set-ADUser -Identity $user -CannotChangePassword $true -PasswordNeverExpires $true
25-
}catch{
26-
Write-Host $_
14+
{% if dataStatus == -2 %}
15+
Set-ADUser -Identity $user -Enabled $True
16+
{% endif %}
17+
$userFound=get-aduser -Filter "SamAccountName -eq 'jcerri' -and Enabled -eq '$True' -and PasswordExpired -eq '$False'"
18+
if ( ! $userFound ){
19+
Write-Host 'user not active or not found or password expired'
2720
exit 1
2821
}
29-
Set-ADUser -Identity $user -CannotChangePassword $true
30-
WriteHost "Password changed"
31-
Exit 0
22+
if (Test-ADAuthentication -username "$user" -password "$oldp"){
23+
Write-Host "password ok"
24+
try{
25+
Set-ADUser -Identity $user -CannotChangePassword $false
26+
# on lance 2 fois la commande car ne marche pas la premiere fois bug 2019 ?
27+
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
28+
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
29+
Set-ADUser -Identity $user -CannotChangePassword $true -PasswordNeverExpires $true
30+
}catch{
31+
Write-Host $_
32+
exit 1
33+
}
34+
Set-ADUser -Identity $user -CannotChangePassword $true
35+
Write-Host "Password changed"
36+
Exit 0
37+
}else{
38+
Write-Host "Authentication Invalid password"
39+
exit 1
40+
}

src/ps1_templates/resetpassword.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ param (
55
try{
66
Set-ADUser -Identity $user -CannotChangePassword $false -Enabled $true
77
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -Reset
8+
Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -Reset
89
Set-ADUser -Identity $user -CannotChangePassword $true
910
}catch{
1011
Write-Host $_

0 commit comments

Comments
 (0)