how to authorize and authenticate the same type of user at diffrent permission? - asp.net-mvc

I'm working on part of a project about authorize and authenticate.so,but I've problem in the the same type of user with different permission.for example,
UserPermission Table
------------------------
UserId(int) //this is a AUTO INCREMENT primary key
UserBasicInformation(bool)
UserOperation(bool)
UserEntrance(bool)
UserExit(bool)
UserReport(bool)
AdminBasicInformation(bool)
AdminOperation(bool)
AdminSettings(bool)
UserPermissionId(int) // in this field I've kept key of users from UserDefs table
UserTitle(nvarchar)
UserDefs (brief)
--------------------------
UserId ///this is a AUTO INCREMENT primary key
UserType(int) //1,2
UserName(nvarchar) // admin,user
UserTitle(nvarchar) // alex,sara,....
UserId UserType UserName UserTitle
--------------------------------------------
1 1 Admin alex
2 1 Admin sara
3 2 user steve
4 2 user nicolas
this is UserPermision table that I populate for better understand.
UserId UserBasicInformation UserOperation UserEntrance UserExit
-----------------------------------------------------------------------------
1 0 0 0 0
2 0 0 0 0
3 1 1 1
UserReport AdminBasicInformation AdminOperation AdminSettings
-----------------------------------------------------------------------------
0 1 1 0
0 0 1 1
0 0 0 0
UserPermissionId UserTitle
-------------------------------
1 alex
1 sara
2 steve
if you take a look at userDefs table I have a two admins or two users but they are different permission in UserPermission table.but now,I don't know how to implement it.some people said me I use sessions what is your idea about it? in fact,I will read userpermission table.

Related

Rails: Possible to refactor this query?

I have 2 active record relation objects with the code as follow:
#obj1 = User.select('user.X, table2.Y, table2.Z, count (*)')
.merge(#some_variable)
.joins(:table1, :table2)
.group(1, 2, 3)
#obj2 = User.select('user.X, table2.Y, count (*)')
.merge(#some_variable)
.joins(:table1, :table2)
.group(1, 2)
Basically, the only difference between #obj1 and #obj2 is that #obj2 is not selecting table2.Z column data.
Here is a sample data that I would like both #obj to have:
#obj1
-------------------------------------
user.X table2.Y table2.Z count
-------------------------------------
1 1 A 1
1 1 B 1
2 1 A 1
2 1 B 1
2 1 C 1
#obj2
-------------------------
user.X table2.Y count
-------------------------
1 1 2
2 1 3
Currently the queries above are working fine, but I believe it is possible to further refactor the code? Like having #obj2 to get the records based on #obj1 data without having to do similar sql query? Appreciate if anyone got input on this. Many thanks in advance.
columns = %w(users.X, table2.Y table2.Z count(*))
#obj1 = User.merge(#some_variable)
.joins(:table1, :table2)
.group(1, 2, 3)
.select(*columns)
#obj2 = #obj1.select(*(columns - ["table2.Z"]))
A further step in refactoring would be to use Arel to replace the string conditions for portability.

Complex Left Outer Self-Join in Rails 5

I have a list_events table where I want to get the latest event per user per list between a certain time. Here's an example of the table.
id user_id list_id event created_at
1 5 1 sub 13:45
2 1 1 sub 14:01
3 1 2 sub 14:02
4 3 1 sub 14:03
5 4 1 sub 14:04
6 1 1 unsub 14:05
The last events per user for list 1 between 14:00 and 15:00 would be...
id user_id list_id event created_at
4 3 1 sub 14:03
5 4 1 sub 14:04
6 1 1 unsub 14:05
In my Rails 5 model I've written the query like so:
list.events
.joins("
left outer join list_events b
on list_events.user_id = b.user_id
and list_events.list_id = b.list_id
and list_events.created_at < b.created_at
")
.where("b.user_id is null")
.where(created_at: start..end)
This works fine, but I'm wondering if there's a way to write this without hand-coding the join. I do notice Rails has a left_outer_join method, but there's no way to specify custom on. Perhaps with a belongs_to?
Also if there's a way to alias list_events as a while still being able to take advantage of the list.events relationship abstraction.

Reports for permissions to Informix user

I want to generate a report file based on the permissions granted to an Informix user for three different tables like follows:
USER Report
USER tbl1 tbl2 tbl3
A 1 0 0
B 1 0 0
C 0 1 1
D 0 1 1
In the above example 1 denotes user 'A' and 'B' has 'SELECT' permission on the 'tbl1' table and user 'C' and 'D' has 'SELECT' permission on the 'tbl2' and 'tbl3' tables.
I went through internet and got dbschema command. But its output is different then required.
Please guide me to generate this report.

How can i count a column doing a condition?

i'm trying to count a column using conditions
Tables
select* from policies
|policies|
|id| |client_id| |expiration_date
1 1 2013-10-10
2 1 2013-10-10
3 2 2013-10-10
|clients|
|id| |name|
1 ABC
2 CDE
3 EFG
i WANT
select *,count(number_expirations) from policies where(client=1)
select *,count(number_expirations) from policies where(client=2)
|policies|
|id| |client_id| |number_of_expirations|
1 1 2
3 2 1
This is consult
#count = Policy.count('expiration_date',:joins=> :client,:conditions=>['name =?',params[:name])
But i'm trying to count expiration_date by client_id
I will really appreciate help.
i did not completely understand your question or the finder that you provided, but i think that you want to get a count of expiration_dates grouped by client.
this would look like this:
Policy.where(name: params[:name]).group(:client_id).count

How to store word compositions in a relational database in a normalized way?

I'm trying to find a nice way to store word compositions of the following form:
exhaustcleaningsystem
exhaust cleaning system
exhaustcleaning system
exhaust cleaningsystem
The combinations are given by a default per case. Every word in a composition is stored as a unique row in table 'labels'.
labels
id value
--------------------------
1 exhaustcleaningsystem
2 exhaust
3 cleaning
4 system
5 exhaustcleaning
6 cleaningsystem
I thought about a new table called 'compositions':
compositions
id domain_id range
----------------------
1 1 2,3,4
2 1 5,4
etc...
But storing multiple separated values in a column isn't normalized design. Any ideas for that?
BTW: I'm using MySQL und ActiveRecord/Rails.
The design you propose is not even in first normal form, since range is not atomic
The schema I'd use here would be
compositions
id domain_id
-------------
1 1
2 1
compositions-content
composition_id rank label_id
------------------------------------------
1 1 2
1 2 3
1 3 4
2 1 5
2 2 4
with composition_id referencing an composition.id and label_id referencing label.id
The rank column is optional and should be here if and only if the range you define here is order-sensitive.
With this design, you have some referential integrity at DB level.
Well, this is as far as I can think of in terms of normalisation:
sets
id domain_id
--------------
1 1
2 1
etc...
compositions
id set_id label_id order
---------------------------
1 1 2 1
2 1 3 2
3 1 4 3
4 2 5 1
5 2 4 2
etc...

Resources