学习 jForum笔记 三 .ForumAction 发现用户认证模板
2013-03-14 15:18
573 查看
PermissionControl pc = SecurityRepository.get (userId); //权限控制
if (pc.canAccess (SecurityConstants.PERM_FORUM, Integer.toString(forum.getId()))) {
forums.add(forum); //如果有权控制板块,则添加到返回列表
}
正如字面意思pc是权限控制,先看net.jforum.security/PermissionControl.java
private RoleCollection roles;//权限集合
private transient GroupSecurityDAO smodel;//组权限数据表
这里有两个属性,一个是权力集合,一个是组权限数据表,暂时没看懂有什么用。
public boolean canAccess(String roleName, String roleValue)
{
Role role = this.roles.get(roleName);
if (role == null) {
return false;
}
return role.getValues ().contains(new RoleValue(roleValue));
}
根据Category.java中的调用语句看,canAccess()的两个参数,一个是'perm_fourm',另一个是板块的ID。而在canAccess()这里,先根据perm_fourm获取一个Role,rol.getValues()是什么?
根据net.jforum.security/role.java
public RoleValueCollection getValues()
{
return this.
roleValues ;
}
private final RoleValueCollection roleValues = new RoleValueCollection();
就是说,一个role 对应多个RoleValue ,而fourm_id也可能是一个RoleValue
pc.canAccess()就是判断role中是否包含指定的RoleValue。
再根据本页第一句,看net.jforum.security/SecurityRepository.java
public static PermissionControl get(int userId)
{ //从缓存中读权限控制
PermissionControl pc = (PermissionControl)cache.get(FQN, Integer.toString(userId));
if (pc == null) { //缓存中没有
try {
pc = load (userId); //取用户权限
}
catch (Exception e) {
throw new SecurityLoadException(e);
}
}
public static PermissionControl load(int userId)
{
return SecurityRepository.
load (userId, false);
}
好绕啊。
public static PermissionControl load(int userId, boolean force)
{
if (force || cache.get(FQN, Integer.toString(userId)) == null) { //强制重取或缓存中没有
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
return SecurityRepository.
load (um.selectById(userId), force);
}
return SecurityRepository.get(userId);
}
再看
public static PermissionControl load(User user, boolean force)
{
String userId = Integer.toString(user.getId());
if (force || cache.get(FQN, userId) == null) { //强制重取或缓存中没有
PermissionControl pc = new PermissionControl();
// load roles
GroupSecurityDAO dao = DataAccessDriver.getInstance().newGroupSecurityDAO();
pc.setRoles(dao.
loadRolesByUserGroups (user)); //从用户所在的组中取权限
cache.add(FQN, userId, pc);
return pc;
}
return SecurityRepository.get(user.getId());
}
再找doa.loadRolesByUserGroups(),在net.jforum.dao.generic.security\GenericGroupSecurityDAO.java中
public RoleCollection loadRolesByUserGroups(User user)
{
List groups = user.getGroupsList(); //取用户的所有组
// When the user is associated to more than one group, we
// should check the merged roles
int[] groupIds = this.getSortedGroupIds(groups); //对组排序
RoleCollection groupRoles = RolesRepository.getGroupRoles(groupIds); //从缓存中根据组取权限
// Not cached yet? then do it now
if (groupRoles == null) { //缓存中没有
groupRoles = this.
loadRoles (groupIds); //从数据表中取所有组的权限
RolesRepository.addGroupRoles(groupIds, groupRoles); //将所有组ID及组权限写入缓存中
}
return groupRoles;
}
再看从数据表中取组权限的过程:
protected RoleCollection loadRoles(int[] groupIds) //从数据表中取所有组的权限
{
String sql = SystemGlobals.getSql("PermissionControl.loadGroupRoles"); //取sql语句
String groupIdAsString = SecurityCommon.groupIdAsString(groupIds); //将组ID转成适合SQL语句的字符型
if ("".equals(groupIdAsString)) {
// We suppose there is no "negative" group ids
sql = sql.replaceAll("#IN#","-1");
}
else { //将组ID加入SQL语句。
sql = sql.replaceAll("#IN#", groupIdAsString);
}
RoleCollection roles = null;
PreparedStatement p = null;
ResultSet rs = null;
try {
p = JForumExecutionContext.getConnection().prepareStatement(sql);
rs = p.executeQuery();
roles = SecurityCommon.
loadRoles (rs); //将SQL执行结果转为roles
}
catch (SQLException e) {
throw new DatabaseException(e);
}
finally {
DbUtils.close(rs, p);
}
return roles;
}
查看net.jforum.dao.generic.security/SecurityCommon.java
public static RoleCollection loadRoles(ResultSet rs)
{
RoleCollection rc = new RoleCollection();
try {
Role r = null;
String lastName = null;
while (rs.next()) {
String currentName = rs.getString("name'); //当前规则名称
if (!currentName.equals(lastName)) { //当前规则名称与最后规则名称不同
if (r != null) {
rc.add(r); //将规则加入返回集,首次不同由于r的初始值为null,因此不会加入
}
r = new Role();
r.setName(rs.getString("name")); //设置规则名称,那么ID呢?没看到设置role的ID啊。
lastName = currentName; //最后规则名称刷新为当前规则名称
}
String roleValue = rs.getString("role_value");
if (!rs.wasNull() && StringUtils.isNotBlank(roleValue)) {
r.getValues().add(new RoleValue(roleValue)); //设置role的rolevalue
}
}
if (r != null) {
rc.add(r);//将循环后最后一个role放入结果集
}
return rc;
}
catch (SQLException e) {
throw new DatabaseException(e);
}
}
总结一下,role中有多个rolevalue.取权限的语句在"PermissionControl.loadGroupRoles" 中
if (pc.canAccess (SecurityConstants.PERM_FORUM, Integer.toString(forum.getId()))) {
forums.add(forum); //如果有权控制板块,则添加到返回列表
}
正如字面意思pc是权限控制,先看net.jforum.security/PermissionControl.java
private RoleCollection roles;//权限集合
private transient GroupSecurityDAO smodel;//组权限数据表
这里有两个属性,一个是权力集合,一个是组权限数据表,暂时没看懂有什么用。
public boolean canAccess(String roleName, String roleValue)
{
Role role = this.roles.get(roleName);
if (role == null) {
return false;
}
return role.getValues ().contains(new RoleValue(roleValue));
}
根据Category.java中的调用语句看,canAccess()的两个参数,一个是'perm_fourm',另一个是板块的ID。而在canAccess()这里,先根据perm_fourm获取一个Role,rol.getValues()是什么?
根据net.jforum.security/role.java
public RoleValueCollection getValues()
{
return this.
roleValues ;
}
private final RoleValueCollection roleValues = new RoleValueCollection();
就是说,一个role 对应多个RoleValue ,而fourm_id也可能是一个RoleValue
pc.canAccess()就是判断role中是否包含指定的RoleValue。
再根据本页第一句,看net.jforum.security/SecurityRepository.java
public static PermissionControl get(int userId)
{ //从缓存中读权限控制
PermissionControl pc = (PermissionControl)cache.get(FQN, Integer.toString(userId));
if (pc == null) { //缓存中没有
try {
pc = load (userId); //取用户权限
}
catch (Exception e) {
throw new SecurityLoadException(e);
}
}
public static PermissionControl load(int userId)
{
return SecurityRepository.
load (userId, false);
}
好绕啊。
public static PermissionControl load(int userId, boolean force)
{
if (force || cache.get(FQN, Integer.toString(userId)) == null) { //强制重取或缓存中没有
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
return SecurityRepository.
load (um.selectById(userId), force);
}
return SecurityRepository.get(userId);
}
再看
public static PermissionControl load(User user, boolean force)
{
String userId = Integer.toString(user.getId());
if (force || cache.get(FQN, userId) == null) { //强制重取或缓存中没有
PermissionControl pc = new PermissionControl();
// load roles
GroupSecurityDAO dao = DataAccessDriver.getInstance().newGroupSecurityDAO();
pc.setRoles(dao.
loadRolesByUserGroups (user)); //从用户所在的组中取权限
cache.add(FQN, userId, pc);
return pc;
}
return SecurityRepository.get(user.getId());
}
再找doa.loadRolesByUserGroups(),在net.jforum.dao.generic.security\GenericGroupSecurityDAO.java中
public RoleCollection loadRolesByUserGroups(User user)
{
List groups = user.getGroupsList(); //取用户的所有组
// When the user is associated to more than one group, we
// should check the merged roles
int[] groupIds = this.getSortedGroupIds(groups); //对组排序
RoleCollection groupRoles = RolesRepository.getGroupRoles(groupIds); //从缓存中根据组取权限
// Not cached yet? then do it now
if (groupRoles == null) { //缓存中没有
groupRoles = this.
loadRoles (groupIds); //从数据表中取所有组的权限
RolesRepository.addGroupRoles(groupIds, groupRoles); //将所有组ID及组权限写入缓存中
}
return groupRoles;
}
再看从数据表中取组权限的过程:
protected RoleCollection loadRoles(int[] groupIds) //从数据表中取所有组的权限
{
String sql = SystemGlobals.getSql("PermissionControl.loadGroupRoles"); //取sql语句
String groupIdAsString = SecurityCommon.groupIdAsString(groupIds); //将组ID转成适合SQL语句的字符型
if ("".equals(groupIdAsString)) {
// We suppose there is no "negative" group ids
sql = sql.replaceAll("#IN#","-1");
}
else { //将组ID加入SQL语句。
sql = sql.replaceAll("#IN#", groupIdAsString);
}
RoleCollection roles = null;
PreparedStatement p = null;
ResultSet rs = null;
try {
p = JForumExecutionContext.getConnection().prepareStatement(sql);
rs = p.executeQuery();
roles = SecurityCommon.
loadRoles (rs); //将SQL执行结果转为roles
}
catch (SQLException e) {
throw new DatabaseException(e);
}
finally {
DbUtils.close(rs, p);
}
return roles;
}
查看net.jforum.dao.generic.security/SecurityCommon.java
public static RoleCollection loadRoles(ResultSet rs)
{
RoleCollection rc = new RoleCollection();
try {
Role r = null;
String lastName = null;
while (rs.next()) {
String currentName = rs.getString("name'); //当前规则名称
if (!currentName.equals(lastName)) { //当前规则名称与最后规则名称不同
if (r != null) {
rc.add(r); //将规则加入返回集,首次不同由于r的初始值为null,因此不会加入
}
r = new Role();
r.setName(rs.getString("name")); //设置规则名称,那么ID呢?没看到设置role的ID啊。
lastName = currentName; //最后规则名称刷新为当前规则名称
}
String roleValue = rs.getString("role_value");
if (!rs.wasNull() && StringUtils.isNotBlank(roleValue)) {
r.getValues().add(new RoleValue(roleValue)); //设置role的rolevalue
}
}
if (r != null) {
rc.add(r);//将循环后最后一个role放入结果集
}
return rc;
}
catch (SQLException e) {
throw new DatabaseException(e);
}
}
总结一下,role中有多个rolevalue.取权限的语句在"PermissionControl.loadGroupRoles" 中
相关文章推荐
- 学习 jForum笔记 四ForumAction 发现用户认证模板 .
- 用户体验学习笔记(工程中发现的PM常犯错误)
- 学习 jForum笔记 二 ForumAction
- django学习笔记---文件上传,用户认证,单选框,复选框接受
- mongo db 学习笔记 之二: mongodb 用户认证
- SpringSecurity学习笔记之五:认证用户
- Angular2学习笔记.3、通过模板调用Controller事件实现用户输入和显示,UserInput
- 《Spring+Mybatis》拦截器实现用户权限认证学习笔记与代码
- SharePoint【学习笔记】-- SharePoint 2010 启用Form认证并增加用户注册功能
- 产品经理学习笔记(4)-第一种角色:产品策划——善于发现用户需求的人
- HTTP学习笔记10 用户身份认证
- php学习笔记(一)php模板与控制器
- (原创)TCP-IP学习笔记之UDP(用户数据报协议)
- 【学习笔记】--Java过滤器,用户登陆验证过滤等
- JavaScript学习笔记7-Ajax(编写模板)
- JavaScript for Kids 学习笔记9. 用户交互
- oracle 学习笔记(一)--用户管理
- Oracle 学习笔记3 —— 使用Profile管理用户密码
- [学习笔记]Java模板方法和单例设计模式
- 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)