Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
mellow repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn奕䜣 committed Nov 14, 2020
1 parent 9e1459e commit f657d7d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package moe.itsusinn.mychat

import moe.itsusinn.mychat.security.atri.AtriAuthenticationToken
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.security.core.context.SecurityContextHolder

fun Any.logger(): Logger = LoggerFactory.getLogger(javaClass)

val Any.credential
get() =
SecurityContextHolder.getContext().authentication as AtriAuthenticationToken
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ class UserController {

@Autowired
lateinit var bCryptPasswordEncoder: BCryptPasswordEncoder

@Autowired
lateinit var userService: UserService

@Autowired
lateinit var userRoleService: UserRoleService

Expand Down Expand Up @@ -51,6 +49,7 @@ class UserController {
UUID.fromString(user.uid.toString()).toString(),
roles.joinToString(":")
)

return "token" to token
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package moe.itsusinn.mychat.models

data class CommentData(
val id: Long,
val author: Long,
val postID: Long,
val commentID: Long,
val authorID: Long,
val content: String
)

data class CommentCreateRequest(
val content: String,
val postID: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ data class PostData(
val title: String
)

typealias NewPost = PostData
data class PostCreateRequest(
val title: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ class RolePermissionRepository {
@Autowired
lateinit var database: Database

fun getRolePermissions(): List<RolePermission> {
fun readRolePermissions(): List<RolePermission> {
val rolePermissions = mutableListOf<RolePermission>()
database
return database
.from(RolePermissionTable)
.select()
.map { row ->
val roleID = row[RolePermissionTable.roleID] ?: return@map null
val permissionID = row[RolePermissionTable.permissionID] ?: return@map null
val roleID = row[RolePermissionTable.roleID]
?: return@map null
val permissionID = row[RolePermissionTable.permissionID]
?: return@map null
createRolePermission(roleID, permissionID)
}
return rolePermissions
}

fun createRolePermission(roleID: Long, permissionID: Long): RolePermission? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MyInvocationSecurityMetadataSourceService : FilterInvocationSecurityMetada
*/
fun loadResourceDefine() {
//权限资源 和 角色对应的表 也就是 角色权限 中间表
val rolePermissions: List<RolePermission> = rolePermissionRepository.getRolePermissions()
val rolePermissions: List<RolePermission> = rolePermissionRepository.readRolePermissions()

//某个资源 可以被哪些角色访问
for (rolePermission in rolePermissions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ class PostService(
.select()
.where { CommentTable.postID eq postID }
.map { row ->
val id = row[CommentTable.commentID] ?: 0
val author = row[CommentTable.authorID] ?: 0
val commentID = row[CommentTable.commentID]
?: return@map null
val authorID = row[CommentTable.authorID] ?: 0L
val content = row[CommentTable.content] ?: ""
CommentData(id, author, content)
CommentData(postID, commentID, authorID, content)
}
}
//get all posts
Expand Down
23 changes: 9 additions & 14 deletions backend-springboot/src/main/resources/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ CREATE TABLE `permission`
`permission_id` bigint(11) not null primary key auto_increment,
`url` varchar(255) not null,
`name` varchar(255) not null,
`description` varchar(255) NULL,
`pid` bigint(11) not null
`description` varchar(255) NULL
);
CREATE TABLE comment
(
Expand All @@ -60,21 +59,17 @@ VALUES (1, 'USER');
INSERT INTO role (role_id, name)
VALUES (2, 'ADMIN');

INSERT INTO permission (permission_id, url, name, pid)
VALUES (1, '/user/common', 'common', 0);
INSERT INTO permission (permission_id, url, name, pid)
VALUES (2, '/user/admin', 'admin', 0);
INSERT INTO permission (permission_id, url, name)
VALUES (1, '/user/login', 'common');
INSERT INTO permission (permission_id, url, name)
VALUES (2, '/user/register', 'common');
INSERT INTO permission (permission_id, url, name)
VALUES (3, '/user/test', 'common');
INSERT INTO permission (permission_id, url, name)
VALUES (4, '/user/admin', 'admin');

INSERT INTO user_role (user_id, role_id)
VALUES (1, 1);
INSERT INTO user_role (user_id, role_id)
VALUES (2, 1);
INSERT INTO user_role (user_id, role_id)
VALUES (2, 2);

INSERT INTO role_permission (role_id, permission_id)
VALUES (1, 1);
INSERT INTO role_permission (role_id, permission_id)
VALUES (2, 1);
INSERT INTO role_permission (role_id, permission_id)
VALUES (2, 2);

0 comments on commit f657d7d

Please sign in to comment.