Struts2 Hibernate Login with User table and group table
- by J2ME NewBiew
My problem is, i have a table User and Table Group (this table use to authorization for user - it mean when user belong to a group like admin, they can login into admincp and other user belong to group member, they just only read and write and can not login into admincp) each user maybe belong to many groups and each group has been contain many users and they have relationship are many to many
I use hibernate for persistence storage. and struts 2 to handle business logic.
When i want to implement login action from Struts2 how can i get value of group member belong to ? to compare with value i want to know?
Example
I get user from username and password then get group from user class but i dont know how to get value of group user belong to
it mean if user belong to Groupid is 1 and in group table , at column adminpermission is 1, that user can login into admincp, otherwise he can't
my code:
User.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.model;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
/**
*
* @author Administrator
*/
@Entity
@Table(name="User")
public class User implements Serializable{
private static final long serialVersionUID = 2575677114183358003L;
private Long userId;
private String username;
private String password;
private String email;
private Date DOB;
private String address;
private String city;
private String country;
private String avatar;
private Set<Group> groups = new HashSet<Group>(0);
@Column(name="dob")
@Temporal(javax.persistence.TemporalType.DATE)
public Date getDOB() {
return DOB;
}
public void setDOB(Date DOB) {
this.DOB = DOB;
}
@Column(name="address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="city")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name="country")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(name="email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name="usergroup",joinColumns={@JoinColumn(name="userid")},inverseJoinColumns={@JoinColumn(
name="groupid")})
public Set<Group> getGroups() {
return groups;
}
public void setGroups(Set<Group> groups) {
this.groups = groups;
}
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
@GeneratedValue
@Column(name="iduser")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Column(name="username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="avatar")
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
Group.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Administrator
*/
@Entity
@Table(name="Group")
public class Group implements Serializable{
private static final long serialVersionUID = -2722005617166945195L;
private Long idgroup;
private String groupname;
private String adminpermission;
private String editpermission;
private String modpermission;
@Column(name="adminpermission")
public String getAdminpermission() {
return adminpermission;
}
public void setAdminpermission(String adminpermission) {
this.adminpermission = adminpermission;
}
@Column(name="editpermission")
public String getEditpermission() {
return editpermission;
}
public void setEditpermission(String editpermission) {
this.editpermission = editpermission;
}
@Column(name="groupname")
public String getGroupname() {
return groupname;
}
public void setGroupname(String groupname) {
this.groupname = groupname;
}
@Id
@GeneratedValue
@Column (name="idgroup")
public Long getIdgroup() {
return idgroup;
}
public void setIdgroup(Long idgroup) {
this.idgroup = idgroup;
}
@Column(name="modpermission")
public String getModpermission() {
return modpermission;
}
public void setModpermission(String modpermission) {
this.modpermission = modpermission;
}
}
UserDAO
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.dao;
import java.util.List;
import org.dejavu.software.model.User;
import org.dejavu.software.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
/**
*
* @author Administrator
*/
public class UserDAO extends HibernateUtil{
public User addUser(User user){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
return user;
}
public List<User> getAllUser(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<User> user = null;
try {
user = session.createQuery("from User").list();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return user;
}
public User checkUsernamePassword(String username, String password){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User user = null;
try {
Query query = session.createQuery("from User where username = :name and password = :password");
query.setString("username", username);
query.setString("password", password);
user = (User) query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return user;
}
}
AdminLoginAction
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.view;
import com.opensymphony.xwork2.ActionSupport;
import org.dejavu.software.dao.UserDAO;
import org.dejavu.software.model.User;
/**
*
* @author Administrator
*/
public class AdminLoginAction extends ActionSupport{
private User user;
private String username,password;
private String role;
private UserDAO userDAO;
public AdminLoginAction(){
userDAO = new UserDAO();
}
@Override
public String execute(){
return SUCCESS;
}
@Override
public void validate(){
if(getUsername().length() == 0){
addFieldError("username", "Username is required");
}if(getPassword().length()==0){
addFieldError("password", getText("Password is required"));
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
other question. i saw some example about Login, i saw some developers use interceptor, im cant understand why they use it, and what benefit "Interceptor" will be taken for us?
Thank You Very Much!