Database design to access/manage multi-organisation - c#

How would you design a database to manage multi organisation?
(ie 1 user can own/manage more than 1 organisation)
Example
Xero (www.xero.com), you can login to xero then select the company on the list that you want to manage. I think freshbook has something simular.
USER_ACCESS
Id
CompanyUserId (UserId of company)
UserId (UserId that will manages company)
USER_PERMISSION
Id
UserAccessId
CanViewM
CanEditM
....
CanViewN
CanEditN

You should not mix users and logins. They should be kept treated as seperate tables/objects. As ones role in one company might not be the same role as in the other company.
Also do not create a permission table with one column for each possible permission. Instead you should create one row per allowed permission. (and if needed one table defining all permissions)
Hence you should have tables like:
USER_ACCOUNT (used to define logins)
Id
UserName
Password
USER
Id
AccountId (account used for login)
CompanyId (company that the user belongs to)
PERMISSIONS
Id
Name
USER_ALLOWED_PERMISSIONS
UserId
PermissionId
When logging in, simply check the USER table if more than one row is returned for the account and display a select user form if needed.

Related

Get application username for Audit table

I want to implement an audit table and I have no idea how am I supposed to get the username.
I am using C# and Sql Server. I have a Users table in my database. When I log in my windows form application I verify if the correct combination of username and password is used. But how do I inform the database of the current user? I thought of adding an extra column to my Users table in which to set on 1 the logged username. Is that a solution for single-user? But my application in supposed to support multi-user. What could be done in this case?
Depending on your authentication scheme, you need to get the the User name.
for thick client applications,
Environment.Username
and
System.Security.Principal.WindowsIdentity.GetCurrent()
are a couple of options.
typically for audit tables, there is a column called 'ModifiedByUser' where you can log the user name provided by the win form app.
create the nvarchar and datetime columns (if not already) in your audit table.
one will stored the user name and the other the datetime of the audit action.
in your code, whenever you want to add an entry to the audit table, get Environment.Username or System.Security.Principal.WindowsIdentity.GetCurrent(), along with DateTime.UtcNow and pass it on to be saved to the DB into the Audit table.
SQL Server knows who you are. You can simply use SUSER_SNAME() or/and ORIGINAL_LOGIN() function as a default value for the username column in your audit table. Same for the time of audit event, use GetDate() function. There is no need to send this information from the client.
This is a very open-ended question but I think I understand what you are trying to do. You have application-specfic users that are defined in a Users table (as opposed to using database users or active directory users) and you need to log specific information for auditing purposes or drive security based off of the logins. Is that correct?
This can be done, but the logic for it will need to be written in your application.
Let’s pretend we are writing a program to send out an invoice to a customer.
I used role based security where you can give users access to do specific tasks by granting them a role. For example, “Create New Invoice” could be a role. I usually have 2 tables for this:
SecuirtyRoleDefintion
SecurityRoleUsers
The fist table, Security Role Definition will have an ID column, the Description (“Create New Invoice”), and I usually have a Audit column to indicate if this action needs to be logged for Audit.
The second table, SecurityRoleUsers, is where I define if a user has permission to execute that role. Columns are usually something like this: a unique ID, User ID (foreign key to the Users table), RoleID (foreign key to SecurityRoleDefintion)
Now in your application we need a class to check if a user has a role. It needs to take in the role ID (or name) and the user ID. Example: public bool IsUserAuthorized(int RoleID, int UserID)
This method can run a query on your SecurityRoleUsers table to see if the user is in the table for that role. If so, it returns true. If not, it returns false.
Now back in the application when user click the “Create New Invoice” button it runs the IsUserAuthorized() method to check if a user can perform the operation.
If creating an audit log is necessary, you could do something similar. After the security check is done for “Create New Invoice” you can check to see if the Role needs to be audit logged, if so then write to an Audit table.
DECLARE #username varchar(128)
SET #username = CONVERT(VarChar(128), CONTEXT_INFO());
PRINT #username
DECLARE #ID_User int
SET #ID_User = ( SELECT Users.ID_User
FROM Users
WHERE Users.Username=#username )
PRINT #ID_User
This is how I solved it. I inserted this piece of code in each update trigger.

Authentication from different table?

I am new to mvc4 and trying to develop a new application where I have three user in table Employee, admin and customer. The problem is that customer has many fields than employee or admin so I cannot make a single user table and then I made a different table for each user where username and password is included. Now I am lost when I want to authenticate user from their respective table. Is it possible or should I make user table separately with username and password? But doing so should I have to first create user every time before I create customer, employee and admin?
so i cannot make a single user table
Sure you can. Just separate the "users" from the "details about the users." For example, say you have a Users table here:
Users
---------
ID (PK)
Username
Password
(maybe a few other fields)
Then you san sub-type some other tables off of this one. Tables like:
Employee
----------
ID (PK, FK to Users)
Name
EmployeeNumber
etc.
Customer
----------
ID (PK, FK to Users)
Name
CustomerNumber
etc.
Admin
----------
ID (PK, FK to Users)
(you get the idea)
Very similar to sub-classing in an object-oriented system, this allows you to sub-table in a relational system. The login components in the application are only concerned with the Users table, then once authenticated the other components can get information about the user from the other tables. (This has the added benefit of allowing a single user to be more than one thing. Such as an Employee who is also an Admin.)
Edit: Keep in mind this is based on knowing very little about your relational data needs. You might also be able to accomplish the same thing using a setup like:
Users
----------
ID (PK)
Username
etc.
Roles
----------
ID (PK)
RoleName
UsersInRoles
----------
UserID (FK to Users)
RoleID (FK to Roles)
(Other tables about users)
This setup is a lot closer to what's built in to the ASP.NET membership system as well, so you may be able to benefit significantly from it. Though based on your description of your user data you may still need to sub-type some tables containing the user information in order to avoid having lots of null values.

Filtering table dynamically using the user details entered

I Have a User table, where it has userFirstname, emailid, organisation as columns. I have to filter the users based on the user that have logged into the system. I have the Email ID of the user who logs into the system. I need a sql statement so that the users are filtered based on the organisation which is same as the logged in user.
something like this,
(get the logged on users organisation, then find all users in that organisation):
SELECT *
FROM USER
WHERE organisation IN (SELECT organisation
FROM USER
WHERE emailid = loggedon_emailid)
I don't think it's a big deal here, because this sounds like a homework question. But, in an application, you will most likely need to frequently reference the data associated with a given emailid. Thus, you would probably want to run
SELECT * FROM User WHERE emailid = loggedon_emailid
at the beginning of your codepath and save that data in memory. Then, to execute the query you need, you could simply run:
SELECT * FROM User WHERE organisation = $loggedon_organisation
Then, when you need to print the user's name or organisation on the screen, you will not need an additional SQL query. And, if you have other tables, you could run more queries without repeatedly looking up the user's row in the User table. For example (this is purely hypothetical):
SELECT organisation_address FROM Organisation WHERE organisation = $loggedon_organisation

Design Implementation SQL tables

I am new to SQL and been given a task. Following are the details:
What I have:
A C# desktop application for User login and view user status (only two options: user logs in and check status of all users that are dummy created)
A table named USER containing
id
username
datecreated
A table named LOGINSTAT containing
id
username
Logtime
logDate
What I have to implement
I have to save time and date when ever user logs in in LOGINSTAT table using SQL.
My question
My question is how can I implement that. I can do the coding part but I am interested in getting some good advice to implement it. I think of it as a formal way as I know to do it:
when user logs in insert values into the login table giving all the required values.
BUT
I think that might be a bit odd. Some of my friends said you may be able to implement it by use of foreign key and primary keys, but the problem lies that the user may log in many time in a day. How to keep track of login time and date in that case?
You don't need username in your LOGINSTAT table.
You'll probably want the LOGINSTAT to include:
id
u_id
loginDateTime
id is the unique ID of every login
u_id is a foreign key from the id in users that matches your log event to a user
loginDateTime is a datetime that will give you both your log date and log time in one column
What is unique in LOGINSTAT? Not user by itself, but ID+LogDate+LogTime should be. That would be your primary key.
The only foreign key is in LOGINSTAT: ID, which references the ID in the USER table.
Values in a PRIMARY KEY column (eg. USER.id) must be unique from one another.
Values in a FOREIGN KEY column in another table referencing that primary key (eg. LOGINSTAT.id referencing USER.id) do not need to be unique - you can have multiple records in a table have the same foreign key column reference the same primary key.

how to retrieve userId from membership users table

i'm new to membership. i have a table named contact, with a field, userId that must get its data from the membership users table. so when a user is created i have to get the userId from membership users table. how can i do that?
thanx in advance
Use the ProviderUserKey property of the MembershipUser object:
http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.provideruserkey.aspx
So say:
MembershipUser user = Membership.CreateUser("foo","password");
user.ProviderUserKey

Categories

Resources