Skip to content

Commit

Permalink
🎨 improved code struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 14, 2017
1 parent c28f1d7 commit d7c5496
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
41 changes: 23 additions & 18 deletions src/main/java/com/tale/hooks/BaseWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@ public boolean before(Signature signature) {
log.info("UserAgent: {}", request.userAgent());
log.info("用户访问地址: {}, 来路地址: {}", uri, ip);

if (uri.startsWith("/static")) {
if (uri.startsWith(TaleConst.STATIC_URI)) {
return true;
}

if (!TaleConst.INSTALL && !uri.startsWith("/install")) {
response.redirect("/install");
if (!TaleConst.INSTALLED && !uri.startsWith(TaleConst.INSTALL_URI)) {
response.redirect(TaleConst.INSTALL_URI);
return false;
}

if (TaleConst.INSTALL) {
Users user = TaleUtils.getLoginUser();
if (null == user) {
Integer uid = TaleUtils.getCookieUid(request);
if (null != uid) {
user = new Users().find(uid);
request.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
}
}
if (TaleConst.INSTALLED) {
return isRedirect(request, response);
}
return true;
}

if (uri.startsWith("/admin") && !uri.startsWith("/admin/login")) {
if (null == user) {
response.redirect("/admin/login");
return false;
}
request.attribute("PLUGIN_MENUS", TaleConst.PLUGIN_MENUS);
private boolean isRedirect(Request request, Response response) {
Users user = TaleUtils.getLoginUser();
String uri = request.uri();
if (null == user) {
Integer uid = TaleUtils.getCookieUid(request);
if (null != uid) {
user = new Users().find(uid);
request.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
}
}
if (uri.startsWith(TaleConst.ADMIN_URI) && !uri.startsWith(TaleConst.LOGIN_URI)) {
if (null == user) {
response.redirect(TaleConst.LOGIN_URI);
return false;
}
request.attribute(TaleConst.PLUGINS_MENU_NAME, TaleConst.PLUGIN_MENUS);
}
return true;
}
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/tale/init/TaleConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TaleConst {
public static String AES_SALT = "0123456789abcdef";
public static String LOGIN_SESSION_KEY = "login_user";
public static Environment OPTIONS = Environment.of(new HashMap<>());
public static Boolean INSTALL = false;
public static Boolean INSTALLED = false;
public static Boolean ENABLED_CDN = true;
public static Environment BCONF = null;

Expand Down Expand Up @@ -56,4 +56,29 @@ public class TaleConst {
*/
public static final Set<String> BLOCK_IPS = new HashSet<>(16);

/**
* 静态资源URI
*/
public static final String STATIC_URI = "/static";

/**
* 安装页面URI
*/
public static final String INSTALL_URI = "/install";

/**
* 后台URI前缀
*/
public static final String ADMIN_URI = "/admin";

/**
* 后台登录地址
*/
public static final String LOGIN_URI = "/admin/login";

/**
* 插件菜单 Attribute Name
*/
public static final String PLUGINS_MENU_NAME = "plugin_menus";

}
2 changes: 1 addition & 1 deletion src/main/java/com/tale/init/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void processor(Blade blade) {
TaleConst.BLOCK_IPS.addAll(Arrays.asList(ips.split(",")));
}
if (Files.exists(Paths.get(AttachController.CLASSPATH + "install.lock"))) {
TaleConst.INSTALL = Boolean.TRUE;
TaleConst.INSTALLED = Boolean.TRUE;
}

BaseController.THEME = "themes/" + Commons.site_option("site_theme");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tale/service/SiteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void initSite(Users users) {
String cp = SiteService.class.getClassLoader().getResource("").getPath();
File lock = new File(cp + "install.lock");
lock.createNewFile();
TaleConst.INSTALL = Boolean.TRUE;
TaleConst.INSTALLED = Boolean.TRUE;
new Logs(LogActions.INIT_SITE, null, "", uid.intValue()).save();
} catch (Exception e) {
throw new TipException("初始化站点失败");
Expand Down

0 comments on commit d7c5496

Please sign in to comment.