multi level login, and restricted area on code igniter - codeigniter

i have problem to proces multi level log in and restristed area
there will be 2 type of user: admin and user (common), when a user register they fill form : username, email, and city. --> acces will be set as NULL. for admin acces will be st to 1 (manually)
table users:
CREATE TABLE IF NOT EXISTS users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
acces' varchar(5),
'city varchar(25) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
table event :
CREATE TABLE IF NOT EXISTS events (
id int(11) NOT NULL AUTO_INCREMENT,
event varchar(255) NOT NULL,
'cityvarchar(25) NOT NULL,
PRIMARY KEY (id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
the problems are:
1. when a user want to log in, they fill 2 form : email and password.
2. how to to check if they user or admin
3. if they log in as user, they will be directed to their 'city'. the 'city' will be same to the city when they register.
i really appreciate all possible solution to my problems..
thanks all
Regards.

Simply add a third table with 'roles', that assigns a user with a role.
instead of checking for the user login, You can limit functions/controllers to rules.
if(user_has_role('admin'))
You can with some minor effort expand this to multiple roles per user, with each role having it's own permissions etc., there are also several premade libraries for this sort of functionality.
Just google around for user libraries.

Related

spring security authentication different profiles and permissions with hibernate and jsf

i want to authenticate the users using spring security. i'm using hiebrnate and jsf.
the problem is in the different tutorials that i saw online there is just one role. but in my case i have a different database with permissions and profiles
Table User
user id
profile id
login
password
Table profile
profile_id
profile
Table action or permission
action_id
action
profile_id
I am wondering if anyone knows how to implement this or knows some good tutorials.
if you want to manage your user role, groups and permissions, you could see the Spring Security documentation about schema.
Here some snippets and details on the above link :
create table users(
username varchar_ignorecase(50) not null primary key,
password varchar_ignorecase(50) not null,
enabled boolean not null);
create table authorities (
username varchar_ignorecase(50) not null,
authority varchar_ignorecase(50) not null,
constraint fk_authorities_users foreign key(username) references users(username));
create unique index ix_auth_username on authorities (username,authority);
create table groups (
id bigint generated by default as identity(start with 0) primary key,
group_name varchar_ignorecase(50) not null);
create table group_authorities (
group_id bigint not null,
authority varchar(50) not null,
constraint fk_group_authorities_group foreign key(group_id) references groups(id));
create table group_members (
id bigint generated by default as identity(start with 0) primary key,
username varchar(50) not null,
group_id bigint not null,
constraint fk_group_members_group foreign key(group_id) references groups(id));

How to get DataMapper to not specify a NULL default value

I'm getting an error when I use DataMapper's auto_upgrade! method to add fields in an SQLite3 db based on the properties defined in my code:
DataObjects::SyntaxError at /history
Cannot add a NOT NULL column with default value NULL
An example of an offending line would be:
property :fieldname, Text, required: true
The error goes away if I (a) remove the line, (b) remove required: true, (c) change true to false, or (d) add a default value.
SQLite does not require a default value to be specified for every field, so this problem is definitely with DataMapper, not SQLite.
How can I get around this, so DataMapper can specify that a field is required without assuming that not specifying a default value automatically means the default should be NULL?
(If you want to know more about why I'm designing this way: there will be another client process accessing SQLite and logging data into the SQLite database, while a Sinatra app will be pulling data out of the db for display in a browser. I want the database therefore to enforce the field requirements, but DM's auto_upgrade is a very convenient way to be able to upgrade the database as needed—so long as it doesn't foul things up in the process.)
You are requiring the field, hence it cannot be NULL. This is simple table properties.
When DataMapper runs auto_upgrade! it runs the SQL commands on the database.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
And doing something like this won't work.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL DEFAULT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
I tested it in MySQL and this is the error.
02:52:43 CREATE TABLE TestTest ( P_Id int NOT NULL, lname
varchar(255) NOT NULL DEFAULT NULL, fname varchar(255), Address
varchar(255), City varchar(255) ) Error Code: 1067. Invalid default
value for 'lname' 0.062 sec
Correction: SQLite does allow you to create a table with such properties. However, when trying to insert anything to that table makes it throw an error whether the field is NULL or not. So DataMapper might be doing some sanitation for your before even creating the table.
It is not clear to me if you are creating a new table or modifying an existing one.
If you have an existing table and are trying to alter it with a column defined as NOT NULL, then you must provide a default value so that the existing rows can be migrated. The RDBMS needs to know what to put in the field for pre-existing rows.
If you are creating a new table, then the property definition you have should be fine.

Can I use spring security without username or password?

I need to make sso for applications and combile user table.
I want to use spring security with email authentication, without username or password.
How can I do this?
My limittations:
Single user can authenticate with multiple emails (Like github)
User can manage all authentication state and expire specific authentication. (In profile page)
No password. No string username or id. (Because no service supports basic login)
--- EDIT ---
OAuth 2.0 / 1.0a Authentication
Generated scheme:
(Is this proper for this case?)
create table hib_authentication (
id BIGINT UNSIGNED not null auto_increment,
firstAuthenticatedTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null,
ip VARBINARY(16) not null,
browser VARBINARY(24) not null,
lastAuthenticatedTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP not null,
user_id INT UNSIGNED not null,
primary key (id)
) ENGINE=InnoDB;
create table hib_user (
id INT UNSIGNED not null auto_increment,
country SMALLINT UNSIGNED not null,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null,
locale varchar(255) not null,
timeZone varchar(255) not null,
primary key (id)
) ENGINE=InnoDB;
create table hib_user_email (
id BIGINT UNSIGNED not null auto_increment,
email varchar(255) not null,
user_id INT UNSIGNED not null,
primary key (id)
) ENGINE=InnoDB;
create index index_to_get_authentication_by_user on hib_authentication (user_id);
alter table hib_user_email
add constraint UK_isuygi7fmcwnlht8f4plckt6n unique (email);
create index index_to_search_email_by_user on hib_user_email (user_id);
alter table hib_authentication
add constraint authentication_belongs_to_user
foreign key (user_id)
references hib_user (id);
alter table hib_user_email
add constraint email_belongs_to_user
foreign key (user_id)
references hib_user (id);

How to save session counts and retrieve the number of user access

I would like to know how to save the number of times a user access a webpage and return the session count to the user
Eg
A user opens his user profile on a web page and would be able to see
Users number of access: 20times.
I have created the page with Codeigniter and created a table in the database.
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
But I don't know how to save the number of times the user has accessed the webpage

CodeIgniter dependencies between models, without using any ORM or DataMapper systems

I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between models (without using any ORM or DataMapper systems) by calling methods from one model to another.
For example: in a "many Documents per User" realeation, when deleting a User, the User_Model directly calls the delete() method on the Document_Model, deleting any documents associated with it.
I’m certain there’s a better way implementing 'one to many' model releation (without ORM, etc.. ), and would appreciate some guidance.
Thanks
Alon.
Of course! You are allowed to use InnoDB engine. This engine has functionally foreign keys, so you can do smth like that, for example (very basic)
CREATE TABLE IF NOT EXISTS `documents` (
`did` bigint(11) NOT NULL AUTO_INCREMENT,
`uid` bigint(11) NOT NULL,
`data` varchar(255) NOT NULL,
PRIMARY KEY (`did`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `user` (
`uid` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `documents`
ADD CONSTRAINT `documents_cstr_1` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE NO ACTION ON UPDATE CASCADE;

Resources