Re: Permissions!

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 09/10/05


Date: Sat, 10 Sep 2005 11:02:06 -0500


> but one thing is for sure - users will be accessing the table only from
> my application. What would you suggest?

Since users can access the database only from your application, I'd suggest
a single login. You can grant object permissions to this login and control
access to individual objects/columns from your app because you know the
user's identity and can execute different queries accordingly.

> The bottomline is irrespective of the way I allow users to access data,
> either I have to create 100 views for 100 users (if I use only one
> login name & password) or create 100 login names in SQL Server & have
> 100 If....Else statements in my ASP application (with the login name &
> password for every user changing in the ConnectionString) & that is
> exactly what I would like to avoid. I am back to square one! I need a
> solution to overcome such a tedious & mnotonous approach.

I understand your desire to simplify code. You don't need 100 views or 100
SQL logins or conditional code in your application. All you need to do is
execute different queries depending on the individual user. You can
accomplish this the schema I posted earlier. All you need to do is add the
users and queries to the Users and UserViews configuration tables for use by
your app. The VBScript example below shows how you could do this. You can
beef this up to suit your needs.

--proc to retrieve query for current user
CREATE PROC GetUserSqlStatement
    @UserId varchar(30),
    @TableName sysname
AS
SET NOCOUNT ON
SELECT SqlStatement
FROM UserViews
WHERE UserId = @UserId AND
    TableName = @TableName
GO

'assuming strUserId contains the user id of current user
ExecuteQueryForUser strUserId, "Table1"

Sub ExecuteQueryForUser(UserId, TableName)
    Set connection = CreateObject("ADODB.Connection")

    'you could also use a standard SQL application login here
    connection.Open _
        "Provider=SQLOLEDB" & _
        ";Data Source=(local)" & _
        ";Initial Catalog=MyDatabase" & _
        ";Integrated Security=SSPI"

    'get the query for this user
    sqlUserQuery = GetUserSqlStatement(UserId, TableName, connection)

    If sqlUserQuery = "" Then
        'user has not been configured yet
        Response.Write "<p>You have not yet been configured. Contact
Arpan</p>"
    Else
        ExecuteQuery sqlUserQuery, connection
    End If

    connection.Close
    Set connection = Nothing
End Sub

'get the appropriate SQL query for this user
Function GetUserSqlStatement(UserId, TableName, Connection) ' As String
    Dim sqlStatement
    Set cmdGetUserSqlStatement = CreateObject("ADODB.Command")
    Set cmdGetUserSqlStatement.ActiveConnection = Connection
    cmdGetUserSqlStatement.CommandType = adCmdStoredProcedure
    cmdGetUserSqlStatement.CommandText = "GetUserSqlStatement"

    Set paramUserId = _
        cmdGetUserSqlStatement.CreateParameter("@UserId")
    paramUserId.Type = adVarChar
    paramUserId.Direction = adParamInput
    paramUserId.Size = 30
    paramUserId.Value = UserId
    cmdGetUserSqlStatement.Parameters.Append paramUserId

    Set paramTableName = _
        cmdGetUserSqlStatement.CreateParameter("@TableName")
    paramTableName.Type = adVarWChar
    paramTableName.Direction = adParamInput
    paramTableName.Size = 128
    paramTableName.Value = TableName
    cmdGetUserSqlStatement.Parameters.Append paramTableName

    Set rsSqlStatement = cmdGetUserSqlStatement.Execute

    If rsSqlStatement.EOF Then
        sqlStatement = ""
    Else
        sqlStatement = rsSqlStatement("SqlStatement")
    End If
    rsSqlStatement.Close
    Set rsSqlStatement = Nothing
    Set cmdGetUserSqlStatement = Nothing

     GetUserSqlStatement = sqlStatement

End Function

'Execute query and display results in table
Sub ExecuteQuery(SqlStatement, Connection)
    Set cmdUserQuery = CreateObject("ADODB.Command")
    Set cmdUserQuery.ActiveConnection = Connection
    cmdUserQuery.CommandType = adCmdText
    cmdUserQuery.CommandText = SqlStatement
    Set rsUserQuery = cmdUserQuery.Execute
    Response.Write "<table><tr>"
    For Each column In rsUserQuery.Fields
        Response.Write "<th>" & column.Name & "</th>"
    Next
    Response.Write "</tr>"

    Do While rsUserQuery.EOF = False
        Response.Write "<tr>"
        For Each column In rsUserQuery.Fields
            Response.Write "<td>" & column.Value & "</td>"
        Next
        Response.Write "</tr>"
        rsUserQuery.MoveNext
    Loop
    rsUserQuery.Close
    Response.Write "</table>"
    Set rsUserQuery = Nothing
    Set cmdUserQuery = Nothing
End Sub

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"Arpan" <arpan_de@hotmail.com> wrote in message 
news:1126273838.396106.289280@g47g2000cwa.googlegroups.com...
> Well, actually my application is at its primitive stage & I haven't
> decided whether to use an application login for database access i.e.
> have only 1 login name & password in the ConnectionString of the ASP
> application or connect users using different login names & passwords
> but one thing is for sure - users will be accessing the table only from
> my application. What would you suggest?
>
> The bottomline is irrespective of the way I allow users to access data,
> either I have to create 100 views for 100 users (if I use only one
> login name & password) or create 100 login names in SQL Server & have
> 100 If....Else statements in my ASP application (with the login name &
> password for every user changing in the ConnectionString) & that is
> exactly what I would like to avoid. I am back to square one! I need a
> solution to overcome such a tedious & mnotonous approach.
>
> Arpan
> 


Relevant Pages

  • Re: Login failure...
    ... loop) -- I'm suspecting the connection overhead with rapid ... connection/disconnect timing problem (i.e. my remote SQL Server can't keep ... > Private Sub MyMethod ... and I get Login failure. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Industry Standard Security and guest wifi access best practice
    ... usage policy and a login screen. ... Connection is simple for the end user and requires no VPN client ... Wireless subnet roaming would be really nice as well. ... implementing 802.1X as the complexity in supporting tennants would ...
    (alt.internet.wireless)
  • Re: Industry Standard Security and guest wifi access best practice
    ... usage policy and a login screen. ... Connection is simple for the end user and requires no VPN client ... Wireless subnet roaming would be really nice as well. ... implementing 802.1X as the complexity in supporting tennants would ...
    (alt.internet.wireless)
  • ADO Connection Object
    ... I would like a function to test whether I can open a connection to my SQL ... Server database. ... Dim WithEvents m_cnn As ADODB.Connection ... Private Sub Form_Open ...
    (comp.databases.ms-access)
  • Re: Threading.Monitor.Enter that doesnt /quite/ block the thread
    ... Protected Overloads Overrides Sub Dispose ... Public ReadOnly Property Connection() As SqlCeConnection ... Private Sub Form1_Activated(ByVal sender As Object, ...
    (microsoft.public.dotnet.framework.compactframework)

Loading