ThisApplication.java 5.4 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7
package com.x.bbs.assemble.control;

import java.util.List;

import com.x.base.core.project.Context;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.LoggerFactory;
R
update  
roo00 已提交
8
import com.x.base.core.project.message.MessageConnector;
R
roo00 已提交
9
import com.x.base.core.project.tools.ListTools;
R
update  
roo00 已提交
10 11
import com.x.bbs.assemble.control.queue.QueueNewReplyNotify;
import com.x.bbs.assemble.control.queue.QueueNewSubjectNotify;
R
roo00 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import com.x.bbs.assemble.control.schedule.SubjectReplyTotalStatisticTask;
import com.x.bbs.assemble.control.schedule.SubjectTotalStatisticTask;
import com.x.bbs.assemble.control.schedule.UserSubjectReplyPermissionStatisticTask;
import com.x.bbs.assemble.control.service.BBSConfigSettingService;
import com.x.bbs.assemble.control.service.BBSForumInfoServiceAdv;
import com.x.bbs.assemble.control.service.BBSPermissionInfoService;
import com.x.bbs.assemble.control.service.BBSRoleInfoService;
import com.x.bbs.assemble.control.service.BBSSectionInfoServiceAdv;
import com.x.bbs.assemble.control.service.UserManagerService;
import com.x.bbs.entity.BBSForumInfo;
import com.x.bbs.entity.BBSSectionInfo;

public class ThisApplication {
	
	protected static Context context;
R
update  
roo00 已提交
27 28 29
	public static final String BBSMANAGER = "BBSManager@CMSManagerSystemRole@R";
	public static QueueNewReplyNotify queueNewReplyNotify;
	public static QueueNewSubjectNotify queueNewSubjectNotify;
30
	public static String CONFIG_BBS_ANONYMOUS_PERMISSION = "YES";
R
update  
roo00 已提交
31

R
roo00 已提交
32 33 34 35 36 37
	public static Context context() {
		return context;
	}

	public static void init() throws Exception {
		try {
38
			CONFIG_BBS_ANONYMOUS_PERMISSION = (new BBSConfigSettingService()).getValueWithConfigCode("BBS_ANONYMOUS_PERMISSION");;
R
roo00 已提交
39
			initAllSystemConfig();
R
update  
roo00 已提交
40 41 42 43 44 45 46 47
			queueNewReplyNotify = new QueueNewReplyNotify();
			queueNewSubjectNotify = new QueueNewSubjectNotify();

			MessageConnector.start(context());

			context().startQueue( queueNewReplyNotify );
			context().startQueue( queueNewSubjectNotify );

R
roo00 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			context.schedule( SubjectTotalStatisticTask.class, "0 10 * * * ?");
			context.schedule( SubjectReplyTotalStatisticTask.class, "0 40 * * * ?");
			context.schedule( UserSubjectReplyPermissionStatisticTask.class, "0 0/30 * * * ?");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void destroy() {
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getRoleAndPermissionCacheKey( String personName ) {
		return "RoleAndPermission.withPerson." + personName;
	}
	
	private static void initAllSystemConfig() {
		BBSPermissionInfoService permissionInfoService = new BBSPermissionInfoService();
		BBSRoleInfoService roleInfoService = new BBSRoleInfoService();
		BBSForumInfoServiceAdv forumInfoServiceAdv = new BBSForumInfoServiceAdv();
		BBSSectionInfoServiceAdv sectionInfoServiceAdv = new BBSSectionInfoServiceAdv();
		BBSConfigSettingService configSettingService = new BBSConfigSettingService();
		List<BBSForumInfo> forumInfoList = null;
		List<BBSSectionInfo> sectionInfoList = null;

		try {
			configSettingService.initAllSystemConfig();
		} catch (Exception e) {
			LoggerFactory.getLogger(ThisApplication.class)
					.warn("BBS system check all config setting got an exception.");
			LoggerFactory.getLogger(ThisApplication.class).error(e);
		}

		try {
			forumInfoList = forumInfoServiceAdv.listAll();
			if (forumInfoList != null) {
				for (BBSForumInfo forumInfo : forumInfoList) {
					permissionInfoService.createForumPermission(forumInfo);
					roleInfoService.createForumRole( null, forumInfo);
				}
			}
		} catch (Exception e) {
			LoggerFactory.getLogger(ThisApplication.class)
					.warn("BBS system check all forum permission and role got an exception.");
			LoggerFactory.getLogger(ThisApplication.class).error(e);
		}

		try {
			sectionInfoList = sectionInfoServiceAdv.listAll();
			if (sectionInfoList != null) {
				for (BBSSectionInfo sectionInfo : sectionInfoList) {
					permissionInfoService.createSectionPermission(sectionInfo);
					roleInfoService.createSectionRole( "System", sectionInfo);
				}
			}
		} catch (Exception e) {
			LoggerFactory.getLogger(ThisApplication.class)
					.warn("BBS system check all section permission and role got an exception.");
			LoggerFactory.getLogger(ThisApplication.class).error(e);
		}
	}
	
	/**
	 * 判断用户是否有BBS系统管理权限
	 * 1、系统管理员Manager或者xadmin
	 * 2、拥有BBSManager角色的人员
	 * @param effectivePerson
	 * @return
	 */
	public static Boolean isBBSManager( EffectivePerson effectivePerson ) {
		UserManagerService userManagerService = new UserManagerService();
		try {
			if ( userManagerService.isHasPlatformRole( effectivePerson.getDistinguishedName(), ThisApplication.BBSMANAGER  )
				|| effectivePerson.isManager()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 判断用户是否有权限对指定的论坛进行管理
	 * 1、系统管理员、BBS管理员
	 * 2、指定论坛设置的管理员
	 * @param effectivePerson
	 * @param forumInfo
	 * @return
	 */
	public static Boolean isForumManager( EffectivePerson effectivePerson, BBSForumInfo forumInfo ) {
		if( isBBSManager( effectivePerson ) ) {
			return true;
		}
		if ( forumInfo != null && ListTools.isNotEmpty( forumInfo.getForumManagerList() ) ) {
			for (String name : forumInfo.getForumManagerList()) {
				if ( effectivePerson.getDistinguishedName().equals( name ) ) {
					return true;
				}
			}
		}
		return false;
	}
	
}