uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
DB, StdCtrls, Buttons, ActiveDs_TLB, oleauto,
adshlp ,
JwaWindows,
JwsclSid,
JwsclToken,
JwsclAcl,
JwsclDescriptor,
JwsclSecureObjects,
JwsclKnownSid, JwsclTypes,
Grids, strutils, dateutils;
type
TADsObject =
class (TObject)
FName,FDesc,FPath :
string;
constructor Create (AName,ADesc,Apath :
string);
end;
....
Const
FDomain='
dh-karlsruhe.de';
HomeDirBase='
\\dh-karlsruhe.de\homes\';
....
procedure TForm1.BtnADdirektClick(Sender: TObject);
var
Context:
string;
root : ActiveDs_TLB.IADs;
DNSName, ap,
ou1, ou2 :
string;
erg : boolean;
User : IDispatch;
ADsCont : ActiveDs_TLB.IADsContainer;
Grp : ActiveDs_TLB.IAdsGroup;
bind : widestring;
i, Anzahl : integer;
StudFullname, StudKontoName, StudFirstName,
StudLastName, StudEmail, StudMatnr, StudHomedir:
string;
begin
erg:=true;
Anzahl:=0;
ou1:=trim(StringGrid1.Cells[7,1]);
ou2:=trim(StringGrid1.Cells[8,1]);
bind:='
LDAP://'+FDomain;
try
adshlp.ADsGetObject(bind,IADs,root);
Context:=root.Get('
distinguishedName');
DNSName:=ToPrincipal(Context);
except
on e: EOleException
do
begin
ErrorDialog('
Fehler "'+e.
Message+'
" bei Verbindung zu '+FDomain);
erg:=false;
end
else
begin
ErrorDialog('
Error connecting zu '+FDomain);
erg:=false;
end
end;
// try
if erg
then
begin
// MessageDlg('Verbunden mit'+chr(13)+'DNSName: '+dnsName+chr(13)+
// 'Context: '+Context, mtInformation,[mbOK],0);
form1.caption:='
UserNeu '+Version+'
Verbunden mit: '+DNSName;
for i := 1
to StringGrid1.RowCount-1
do
begin
StudMatnr:=trim(StringGrid1.Cells[0,i]);
StudEmail:=trim(StringGrid1.Cells[2,i]);
StudFirstName:=trim(StringGrid1.Cells[3,i]);
StudLastName:=trim(StringGrid1.Cells[4,i]);
StudKontoName:=trim(StringGrid1.Cells[5,i]);
StudHomedir:=trim(StringGrid1.Cells[6,i]);
StudFullname:=StudFirstName+'
'+StudLastName;
begin // schon vorhanden ??
ap:=SearchUser (StudKontoName);
if length(ap)>0
then
begin
adshlp.ADsGetObject(ap,IID_IADsUser,user);
(user
as IADsUser).GetInfo;
ErrorDialog('
"'+(user
as IADsUser).FullName+'
" gibt es bereits'+sLineBreak+
(user
as IADsUser).ADsPath+sLineBreak+
StudKontoName+'
@'+FDomain);
erg:=false;
StudKontoName:=StudKontoName+'
1';
end
end;
if erg
and (MessageDlg(StudKontoName+'
anlegen',mtConfirmation,[mbYes,mbNo],0)=mrYes)
then
begin
bind:='
LDAP://ou='+ou2+'
,ou='+ou1+'
,ou=Studenten,'+Context;
adshlp.ADsGetObject(bind,ActiveDs_TLB.IADsContainer,ADsCont);
User:=ADsCont.Create('
user','
CN='+ReplaceSpecChar(StudFullname));
(User
as ActiveDs_TLB.IADsUser).Put('
sAMAccountName',StudKontoName);
(User
as ActiveDs_TLB.IADsUser).FullName:=ReplaceSpecChar(StudFullname);
if length(StudFirstName)>0
then
(User
as ActiveDs_TLB.IADsUser).FirstName:=StudFirstName;
(User
as ActiveDs_TLB.IADsUser).LastName:=StudLastName;
(User
as ActiveDs_TLB.IADsUser).HomeDirectory:=StudHomedir;
(User
as ActiveDs_TLB.IADsUser).Put('
homeDrive','
H');
(User
as ActiveDs_TLB.IADsUser).Profile:=StudHomedir+'
\profil';
(User
as ActiveDs_TLB.IADsUser).Put('
userPrincipalName',StudKontoName+'
@'+DNSName);
try
(User
as ActiveDs_TLB.IADsUser).SetInfo;
// Speichern
except
on e: EOleException
do
begin
ErrorDialog('
Fehler "'+e.
Message+'
" bei Speichern der Accounts '+StudKontoName);
erg:=false;
end
else
begin
ErrorDialog('
Fehler bei Speichern der Accounts '+StudKontoName);
erg:=false;
end
end;
// try
// additional attributes
if erg
then
begin
try
(User
as ActiveDs_TLB.IADsUser).SetPassword(StudMatnr);
except
on e: EOleException
do
begin
ErrorDialog('
Fehler "'+e.
Message+'
" bei Setzen des Passworts'+StudKontoName);
erg:=false;
end
else
begin
ErrorDialog('
Fehler bei Speichern der Accounts'+StudKontoName);
erg:=false;
end
end;
end;
if erg
then
begin
try
if StudEmail<>'
'
then
(User
as ActiveDs_TLB.IADsUser).EmailAddress:=StudEmail;
(User
as ActiveDs_TLB.IADsUser).AccountDisabled:=false;
(User
as ActiveDs_TLB.IADsUser).Put('
pwdLastSet',0);
(User
as ActiveDs_TLB.IADsUser).SetInfo;
// Speichern
except
on e: EOleException
do
begin
ErrorDialog('
Fehler "'+e.
Message+'
" bei Speichern zusätzlicher Infos für den Account '+StudKontoName);
erg:=false;
end
else
begin
ErrorDialog('
Fehler bei Speichern usätzlicher Infos für Accounts'+StudKontoName);
erg:=false;
end
end;
// try
end;
if erg
then // Arbeitsgruppen zuordnen
begin
bind:=(User
as ActiveDs_TLB.IADsUser).ADsPath;
try
adshlp.AdsGetObject('
LDAP://cn='+kurs+'
,ou=Gruppen,'+Context, IADsGroup, grp);
grp.Add ((user
as ActiveDs_TLB.IAdsUser).ADsPath);
except
on e: EOleException
do
begin
ErrorDialog('
Fehler "'+e.
Message+'
" Lesen der Gruppe '+Kurs);
erg:=false;
end
else
begin
ErrorDialog('
Fehler bei Lesen der Gruppe '+Kurs);
erg:=false;
end
end;
// try
grp :=
nil;
end;
if erg
then // Homedir anlegen
begin
erg:=createdir(StudHomedir);
if not erg
then
ErrorDialog('
Fehler beim Anlegen des Verzeichnisses "'+StudHomedir+'
"');
end;
if erg
then // Homedir anlegen
erg:=SetDACL_Dir(StudHomedir,StudKontoName,FDomain);
if erg
then // Erfolg
begin
MessageDlg('
Student '+StudKontoName+'
wurde angelegt',mtInformation,[mbOK],0);
inc(Anzahl);
lblads.Caption:=inttostr(Anzahl);
end;
user:=nil;
end;
end;
// for
end;
ADsCont:=nil;
end;