modules/core/domain/src/main/java/org/rhq/core/domain/auth/Subject.java | 42 +++++- modules/core/domain/src/main/java/org/rhq/core/domain/auth/SubjectRoleEntity.java | 62 +++++++++- 2 files changed, 96 insertions(+), 8 deletions(-)
New commits: commit dcb59bb828983e2879cac829852437e00e0f29d8 Author: Partha Aji paji@redhat.com Date: Tue Feb 23 11:03:16 2010 -0500
More clean up work on Subjects and SubjectRoleEntity
diff --git a/modules/core/domain/src/main/java/org/rhq/core/domain/auth/Subject.java b/modules/core/domain/src/main/java/org/rhq/core/domain/auth/Subject.java index f7f6ac2..121f555 100644 --- a/modules/core/domain/src/main/java/org/rhq/core/domain/auth/Subject.java +++ b/modules/core/domain/src/main/java/org/rhq/core/domain/auth/Subject.java @@ -229,9 +229,9 @@ public class Subject implements Externalizable {
@OneToMany @JoinTable(name = "RHQ_SUBJECT_ROLE_MAP", joinColumns = { @JoinColumn(name = "SUBJECT_ID") }) - private java.util.Set<SubjectRoleEntity> subjectRoles; + private Set<SubjectRoleEntity> subjectRoles;
- private java.util.Set<Role> roles; + private Set<Role> roles;
@Transient private Integer sessionId = null; @@ -362,14 +362,18 @@ public class Subject implements Externalizable { this.configuration = configuration; }
- public java.util.Set<SubjectRoleEntity> getSubjectRoles() { + public Set<SubjectRoleEntity> getSubjectRoles() { if (subjectRoles == null) { subjectRoles = new HashSet<SubjectRoleEntity>(); } return subjectRoles; }
- public java.util.Set<Role> getRoles() { + public void setSubjectRoles(Set<SubjectRoleEntity> subjectRolesIn) { + subjectRoles = subjectRolesIn; + } + + public Set<Role> getRoles() { Set<Role> r = new HashSet<Role>(); for (SubjectRoleEntity s : getSubjectRoles()) { r.add(s.getRole()); @@ -378,15 +382,39 @@ public class Subject implements Externalizable { }
public void setRoles(Set<Role> roles) { - this.roles = roles; + Set<SubjectRoleEntity> sroles = getSubjectRoles(); + sroles.clear(); + for (Role r : roles) { + SubjectRoleEntity s = new SubjectRoleEntity(); + s.setSubject(this); + s.setRole(r); + sroles.add(s); + } + } + + public void addRole(Role role, boolean ldap) { + SubjectRoleEntity s = new SubjectRoleEntity(); + s.setSubject(this); + s.setRole(role); + s.setLdap(ldap); + getSubjectRoles().add(s); }
public void addRole(Role role) { - getRoles().add(role); + addRole(role, false); }
public void removeRole(Role role) { - getRoles().remove(role); + SubjectRoleEntity toRemove = null; + for (SubjectRoleEntity s : getSubjectRoles()) { + if (s.getSubject().equals(this) && s.getRole().equals(role)) { + toRemove = s; + break; + } + } + if (toRemove != null) { + getSubjectRoles().remove(toRemove); + } }
@Override diff --git a/modules/core/domain/src/main/java/org/rhq/core/domain/auth/SubjectRoleEntity.java b/modules/core/domain/src/main/java/org/rhq/core/domain/auth/SubjectRoleEntity.java index 369b491..fc0b0ed 100644 --- a/modules/core/domain/src/main/java/org/rhq/core/domain/auth/SubjectRoleEntity.java +++ b/modules/core/domain/src/main/java/org/rhq/core/domain/auth/SubjectRoleEntity.java @@ -1,3 +1,25 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.rhq.core.domain.auth;
import java.io.Serializable; @@ -11,7 +33,6 @@ import org.rhq.core.domain.authz.Role;
@Table(name = "RHQ_SUBJECT_ROLE_MAP") public class SubjectRoleEntity implements Serializable { - @ManyToOne @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID") private Subject subject; @@ -47,4 +68,43 @@ public class SubjectRoleEntity implements Serializable { this.isLdap = isLdap; }
+ @Override + public int hashCode() { + final int PRIME = 31; + int result = 1; + result = (PRIME * result) + ((subject == null) ? 0 : subject.hashCode()); + result = (PRIME * result) + ((role == null) ? 0 : role.hashCode()); + result = (PRIME * result) + ((isLdap) ? 0 : PRIME); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if ((obj == null) || !(obj instanceof SubjectRoleEntity)) { + return false; + } + + final SubjectRoleEntity other = (SubjectRoleEntity) obj; + if (subject == null) { + if (other.subject != null) { + return false; + } + } else if (!subject.equals(other.subject)) { + return false; + } + + if (role == null) { + if (other.role != null) { + return false; + } + } else if (!role.equals(other.role)) { + return false; + } + + return isLdap == other.isLdap; + } }
rhq-commits@lists.fedorahosted.org