Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.

Feature multi user #587

Open
wants to merge 6 commits into
base: feature-multi-user
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sqlite.version>3.15.1</sqlite.version>
<blade-mvc.version>2.0.5-SNAPSHOT</blade-mvc.version>
<blade-mvc.version>2.0.5-RELEASE</blade-mvc.version>
<blade-jdbc.version>0.2.2-beta</blade-jdbc.version>
<blade-tpl.verion>0.1.2</blade-tpl.verion>
<blade-validator>0.0.2</blade-validator>
<commonmark.version>0.9.0</commonmark.version>
<commonmark.version>0.10.0</commonmark.version>
<rome.version>1.0</rome.version>
<emoji.version>3.2.0</emoji.version>
</properties>
Expand Down Expand Up @@ -87,6 +87,12 @@
<version>1.16.18</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>4.1.18.Final</version>
</dependency>
</dependencies>

<repositories>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/tale/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ public RestResponse comment(Request request, Response response,
comments.setAuthor(EmojiParser.parseToAliases(comments.getAuthor()));
comments.setContent(EmojiParser.parseToAliases(comments.getContent()));
comments.setIp(request.address());
comments.setParent(comments.getCoid());

try {
commentsService.saveComment(comments);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/tale/controller/admin/ArticleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.blade.mvc.ui.RestResponse;
import com.blade.validator.annotation.Valid;
import com.tale.controller.BaseController;
import com.tale.enums.GroupName;
import com.tale.exception.TipException;
import com.tale.extension.Commons;
import com.tale.model.dto.LogActions;
Expand All @@ -19,6 +20,7 @@
import com.tale.service.ContentsService;
import com.tale.service.MetasService;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
Expand Down Expand Up @@ -52,8 +54,12 @@ public class ArticleController extends BaseController {
@GetRoute(value = "")
public String index(@Param(defaultValue = "1") int page, @Param(defaultValue = "15") int limit,
Request request) {

Page<Contents> articles = new Contents().where("type", Types.ARTICLE).page(page, limit, "created desc");
Users users = TaleUtils.getLoginUser();
Contents contents = new Contents().where("type", Types.ARTICLE);
if (users != null && !GroupName.ADMIN.toString().equals(users.getGroupName())) {
contents = contents.where("author_id", users.getUid());
}
Page<Contents> articles = contents.page(page, limit, "created desc");
request.attribute("articles", articles);
return "admin/article_list";
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/tale/controller/admin/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public RestResponse doLogin(LoginParam loginParam, Request request,
if (null == user) {
return RestResponse.fail("用户名或密码错误");
}
if(user.getState() != 1) {
return RestResponse.fail("用户已被封禁,请联系管理员");
}
session.attribute(TaleConst.LOGIN_SESSION_KEY, user);
if (StringKit.isNotBlank(loginParam.getRemeberMe())) {
TaleUtils.setCookie(response, user.getUid());
Expand Down
58 changes: 54 additions & 4 deletions src/main/java/com/tale/controller/admin/UsersController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.tale.controller.admin;

import com.blade.ioc.annotation.Inject;
import com.blade.jdbc.page.Page;
import com.blade.kit.DateKit;
import com.blade.mvc.annotation.*;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.http.Request;
import com.blade.mvc.ui.RestResponse;
import com.tale.controller.BaseController;
import com.tale.exception.TipException;
import com.tale.init.TaleConst;
import com.tale.model.entity.Users;
import com.tale.service.UsersService;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -20,6 +23,9 @@
@Path("admin/users")
public class UsersController extends BaseController {

@Inject
private UsersService usersService;

@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
Page<Users> usersPage = new Users().page(1, TaleConst.MAX_POSTS);
Expand All @@ -36,16 +42,60 @@ public String index(Request request) {
@PostRoute("create")
@JSON
public RestResponse create(@Param Users users) {
System.out.println(users);
Integer time = DateKit.nowUnix();
users.setCreated(time);
try {
Integer time = DateKit.nowUnix();
users.setCreated(time);
usersService.saveUser(users);
} catch (Exception e) {
String msg = "操作失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
log.error(msg, e);
}
return RestResponse.fail(msg);
}
return RestResponse.ok();
}


/**
* 删除用户
*
* @param users
* @return
*/
@PostRoute("delete")
@JSON
public RestResponse delete(@Param Users users) {
try {
usersService.deleteUser(users);
} catch (Exception e) {
String msg = "操作失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
log.error(msg, e);
}
return RestResponse.fail(msg);
}
return RestResponse.ok();
}

@PostRoute("update")
@JSON
public RestResponse update(@Param Users users) {
System.out.println(users);
try {
usersService.updateUser(users);
} catch (Exception e) {
String msg = "操作失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
log.error(msg, e);
}
return RestResponse.fail(msg);
}
return RestResponse.ok();
}

Expand Down
48 changes: 32 additions & 16 deletions src/main/java/com/tale/extension/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.tale.model.entity.Comments;
import com.tale.model.entity.Contents;
import com.tale.model.entity.Metas;
import com.tale.model.entity.Users;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;
import jetbrick.template.runtime.InterpretContext;
Expand Down Expand Up @@ -41,8 +42,8 @@ public static void setSiteService(SiteService ss) {
* @return
*/
public static String meta_keywords() {
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("keywords");
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("keywords");
if (null != value) {
return value.toString();
}
Expand All @@ -55,8 +56,8 @@ public static String meta_keywords() {
* @return
*/
public static String meta_description() {
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("description");
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("description");
if (null != value) {
return value.toString();
}
Expand All @@ -69,8 +70,8 @@ public static String meta_description() {
* @return
*/
public static String head_title() {
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("title");
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("title");

String p = "首页";
if (null != value) {
Expand Down Expand Up @@ -197,7 +198,7 @@ public static List<String> tag_list() {
*/
public static String show_categories(String categories) throws UnsupportedEncodingException {
if (StringKit.isNotBlank(categories)) {
String[] arr = categories.split(",");
String[] arr = categories.split(",");
StringBuffer sbuf = new StringBuffer();
for (String c : arr) {
sbuf.append("<a href=\"/category/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
Expand All @@ -216,7 +217,7 @@ public static String show_categories(String categories) throws UnsupportedEncodi
public static String show_tags(String split) throws UnsupportedEncodingException {
Contents contents = current_article();
if (StringKit.isNotBlank(contents.getTags())) {
String[] arr = contents.getTags().split(",");
String[] arr = contents.getTags().split(",");
StringBuffer sbuf = new StringBuffer();
for (String c : arr) {
sbuf.append(split).append("<a href=\"/tag/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
Expand Down Expand Up @@ -327,11 +328,11 @@ public static String show_thumb(Contents contents) {
return contents.getThumbImg();
}
String content = article(contents.getContent());
String img = Commons.show_thumb(content);
String img = Commons.show_thumb(content);
if (StringKit.isNotBlank(img)) {
return img;
}
int cid = contents.getCid();
int cid = contents.getCid();
int size = cid % 20;
size = size == 0 ? 1 : size;
return "/templates/themes/default/static/img/rand/" + size + ".jpg";
Expand Down Expand Up @@ -612,9 +613,9 @@ public static Page<Comment> comments(int limit) {
if (null == contents) {
return new Page<>();
}
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("cp");
int page = 1;
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("cp");
int page = 1;
if (null != value) {
page = (int) value;
}
Expand All @@ -629,7 +630,7 @@ public static Page<Comment> comments(int limit) {
*/
public static Page<Contents> articles(int limit) {
Request request = WebContext.request();
Integer page = request.attribute("page_num");
Integer page = request.attribute("page_num");
page = null == page ? request.queryInt("page", 1) : page;
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;

Expand All @@ -644,14 +645,29 @@ public static Page<Contents> articles(int limit) {
return articles;
}

/**
* 获取当前文章的作者
*
* @return
*/
public static Users current_author() {
Contents contents = current_article();
if (null == contents) {
return new Users();
}
Integer authorId = contents.getAuthorId();
Users users = siteService.getUser(authorId);
return users == null ? new Users() : users;
}

/**
* 获取当前上下文的文章对象
*
* @return
*/
private static Contents current_article() {
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("article");
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("article");
if (null != value) {
return (Contents) value;
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/tale/service/CommentsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
import com.blade.jdbc.page.Page;
import com.blade.kit.BladeKit;
import com.blade.kit.DateKit;
import com.blade.kit.StringKit;
import com.tale.exception.TipException;
import com.tale.model.dto.Comment;
import com.tale.model.entity.Comments;
import com.tale.model.entity.Contents;
import com.tale.utils.TaleUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* 评论Service
Expand Down
35 changes: 24 additions & 11 deletions src/main/java/com/tale/service/SiteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,22 @@ public class SiteService {

@Inject
private CommentsService commentsService;
@Inject
private UsersService usersService;

public MapCache mapCache = new MapCache();


/**
* 获取用户
*
* @param uid
* @return
*/
public Users getUser(Integer uid) {
return usersService.findUser(uid);
}

/**
* 初始化站点
*
Expand All @@ -58,8 +71,8 @@ public void initSite(Users users) {
Integer uid = users.save();

try {
String cp = SiteService.class.getClassLoader().getResource("").getPath();
File lock = new File(cp + "install.lock");
String cp = SiteService.class.getClassLoader().getResource("").getPath();
File lock = new File(cp + "install.lock");
lock.createNewFile();
TaleConst.INSTALL = Boolean.TRUE;
new Logs(LogActions.INIT_SITE, null, "", uid.intValue()).save();
Expand Down Expand Up @@ -126,11 +139,11 @@ public Statistics getStatistics() {

statistics = new Statistics();

long articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).count();
long pages = new Contents().where("type", Types.PAGE).and("status", Types.PUBLISH).count();
long comments = new Comments().count();
long attachs = new Attach().count();
long tags = new Metas().where("type", Types.TAG).count();
long articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).count();
long pages = new Contents().where("type", Types.PAGE).and("status", Types.PUBLISH).count();
long comments = new Comments().count();
long attachs = new Attach().count();
long tags = new Metas().where("type", Types.TAG).count();
long categories = new Metas().where("type", Types.CATEGORY).count();

statistics.setArticles(articles);
Expand Down Expand Up @@ -161,14 +174,14 @@ public List<Archive> getArchives() {

private Archive parseArchive(Archive archive) {
String date_str = archive.getDate_str();
Date sd = DateKit.toDate(date_str + "01", "yyyy年MM月dd");
Date sd = DateKit.toDate(date_str + "01", "yyyy年MM月dd");
archive.setDate(sd);
int start = DateKit.toUnix(sd);
int start = DateKit.toUnix(sd);
Calendar calender = Calendar.getInstance();
calender.setTime(sd);
calender.add(Calendar.MONTH, 1);
Date endSd = calender.getTime();
int end = DateKit.toUnix(endSd) - 1;
int end = DateKit.toUnix(endSd) - 1;
List<Contents> contents = new Contents().where("type", Types.ARTICLE)
.and("status", Types.PUBLISH)
.and("created", ">", start)
Expand Down Expand Up @@ -224,7 +237,7 @@ public BackResponse backup(String bkType, String bkPath, String fmt) throws Exce
// 备份数据库
if ("db".equals(bkType)) {
String filePath = "upload/" + DateKit.toString(new Date(), "yyyyMMddHHmmss") + "_" + StringKit.rand(8) + ".db";
String cp = AttachController.CLASSPATH + filePath;
String cp = AttachController.CLASSPATH + filePath;
Files.createDirectory(Paths.get(cp));
Files.copy(Paths.get(SqliteJdbc.DB_PATH), Paths.get(cp));
backResponse.setSql_path("/" + filePath);
Expand Down
Loading