📄 osecurity.pas
字号:
unit oSecurity;
{
SQL DMO Security Object DEMO for SQL Server 7.x.
How to Use:
1. Create the COM object in your application.
MyServer := CoSQLServer.Create;
// Set some defaults for MyServer here.
2. Call Login with the server name, database name,
login name, and password. This will connect the server object
to the specified database using the specified user.
3. Invoke the remaining methods to return collections or modify collections.
// Returns a databaseRoles collection.
function GetRoles(const sDBName: WideString): OleVariant;
// Returns a TStrings of Roles that a specific user belongs to.
function GetUserRoles(const Login, sDBName: WideString): IStrings;
// Returns a Users collection of specified database users.
function GetUsers(const sDBName: WideString): OleVariant;
// Tests whether a user is in a particular role.
function IsUserInRole(const Login, Role, sDBName: WideString): WordBool;
// Add a specified user to a specified role in a specified database.
procedure AddUserToRole(const Login, Role, sDBName: WideString);
// Remove a specified user from a specified role in a specified database.
procedure RemoveUserFromRole(const Login, Role, sDBName: WideString);
// Connect a user to a server and database.
function Login(const sServer, sDatabase, sLogin, sPWD: WideString): WordBool;
This object is a no-brainer to user. What else can I say?
DISCLAIMER
--------------------
This source code is the property of Core Software. You may use and distrubute
this source code at your own risk. Warantee as to the to the completeness,
robustness, timliness, or any issue relevant to this source code whatsoever is
not expressed or implied in any way.
FREE INFORMATION
--------------------
Most companies disallow any religious content to be published in articles or
submitted material which is made public. This is illegal. I have a constitutional
right to practice my religion, which includes bringing the news of Jesus Christ
to the world. It is not my intention to invade anyone's privacy, insult, or offend
any person - religious or not.
Jesus Loves You!
Core Software
CTO, Jason 'Wedge' Perry
534 Denver Ave
Chesapeake, VA 23322
jason.perry@home.com
AOL Instant Messager : GuiOOP
ICQ Pager Address : 37953032
}
interface
uses
Sysutils, ComObj, ActiveX, security_TLB, SQLDMO_TLB, StdVcl, Classes,
Dialogs, axCtrls;
type
TSQLDMO_Security = class(TAutoObject, ISQLDMO_Security)
private
oServer : _SQLServer;
function getServer : _SQLServer;
function getDB(sDBName : string) : _Database;
protected
function GetRoles(const sDBName: WideString): OleVariant; safecall;
function GetUserRoles(const Login, sDBName: WideString): IStrings;
safecall;
function GetUsers(const sDBName: WideString): OleVariant; safecall;
function IsUserInRole(const Login, Role, sDBName: WideString): WordBool;
safecall;
procedure AddUserToRole(const Login, Role, sDBName: WideString); safecall;
procedure RemoveUserFromRole(const Login, Role, sDBName: WideString);
safecall;
function Login(const sServer, sDatabase, sLogin,
sPWD: WideString): WordBool; safecall;
public
procedure Initialize; override;
end;
implementation
uses ComServ;
// Oh, some basic setup stuff.
procedure TSQLDMO_Security.Initialize;
begin
inherited Initialize;
if assigned(oServer) then begin
oServer := nil;
end else begin
oServer := getServer;
oServer.Set_QueryTimeout(5);
oServer.Set_LoginSecure(true);
oServer.Set_ApplicationName('SQLDMO Security Object');
end;
end;
// Return the SQL Server object.
function TSQLDMO_Security.getServer;
begin
result := CoSQLServer.Create;
end;
// Return a database object by name.
function TSQLDMO_Security.getDB(sDBName : string) : _Database;
begin
result := oServer.Databases.Item(sDBName, '');
if not assigned(result) then begin
raise exception.create('The database ' + sDBName + ' was not found.');
end;
end;
// Get the roles collection for a specified database.
function TSQLDMO_Security.GetRoles(const sDBName: WideString): OleVariant;
begin
result := getDB(sDBName).DatabaseRoles;
end;
// Get a users collection for a specified database.
function TSQLDMO_Security.GetUsers(const sDBName: WideString): OleVariant;
begin
result := getDB(sDBName).Users;
end;
// Get a list of roles that a user belongs to.
function TSQLDMO_Security.GetUserRoles(const Login,
sDBName: WideString): IStrings;
var
lcv : integer;
oUser : _User;
oStrings : TStrings;
oRoles : NameList;
lFound : boolean;
oDB : _Database;
oSA : IStrings;
begin
lFound := false;
for lcv := 1 to getDB(sDBName).Users.Count do begin
oDB := getDB(sDBName);
if pos(Login, oDB.Users.Item(Login).Login) > 0 then begin
oUser := oDB.Users.Item(Login);
oRoles := oUser.ListMembers;
lFound := true;
break;
end else begin
lFound := false;
continue;
end;
end;
if lFound then begin
oStrings := TStringList.Create;
GetOleStrings(oStrings, OSA);
for lcv := 1 to oRoles.Count do begin
oSA.add(oRoles.Item(lcv));
end;
//SetOleStrings(oStrings, result);
result := oSA;
end else result := nil;
end;
// Translage a user name to a login name.
{function TSQLDMO_Security.GetUserLogin(const Login, sDBName: WideString) : string;
var
lcv : integer;
oDb : _Database;
begin
oDb := getDB(sDBName);
for lcv := 1 to oDb.Users.count do begin
if (pos(Login, oDb.Users.Item(lcv).Login) > 0) then begin
result := oDb.Users.Item(lcv).Login;
break;
end;
end;
end;}
// So, is this user login a member of the specified role?
function TSQLDMO_Security.IsUserInRole(const Login, Role,
sDBName: WideString): WordBool;
begin
result := getDB(sDBName).Users.Item(Login).IsMember(Role);
end;
// Add a user to a role.
procedure TSQLDMO_Security.AddUserToRole(const Login, Role,
sDBName: WideString);
begin
getDB(sDBName).DatabaseRoles.Item(Role).AddMember(Login);
end;
// Remove a user from a role.
procedure TSQLDMO_Security.RemoveUserFromRole(const Login, Role,
sDBName: WideString);
begin
getDB(sDBName).DatabaseRoles.Item(Role).DropMember(Login);
end;
// A login routine. Whimpy.
function TSQLDMO_Security.Login(const sServer, sDatabase, sLogin,
sPWD: WideString): WordBool;
begin
oServer.Connect(sServer, sLogin, sPWD);
result := oServer.VerifyConnection(SQLDMOConn_ReconnectIfDead);
end;
initialization
TAutoObjectFactory.Create(ComServer, TSQLDMO_Security, Class_SQLDMO_Security,
ciMultiInstance, tmApartment);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -