Add power to login
This commit is contained in:
@@ -2,6 +2,7 @@ package top.fatweb.api.mapper.permission
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
||||||
import org.apache.ibatis.annotations.Mapper
|
import org.apache.ibatis.annotations.Mapper
|
||||||
|
import org.apache.ibatis.annotations.Param
|
||||||
import top.fatweb.api.entity.permission.User
|
import top.fatweb.api.entity.permission.User
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,4 +14,6 @@ import top.fatweb.api.entity.permission.User
|
|||||||
* @since 2023-10-04
|
* @since 2023-10-04
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
interface UserMapper : BaseMapper<User>
|
interface UserMapper : BaseMapper<User> {
|
||||||
|
fun getOneWithPowerByUsername(@Param("username")username: String): User?
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,4 +11,6 @@ import top.fatweb.api.entity.permission.User
|
|||||||
* @author FatttSnake
|
* @author FatttSnake
|
||||||
* @since 2023-10-04
|
* @since 2023-10-04
|
||||||
*/
|
*/
|
||||||
interface IUserService : IService<User>
|
interface IUserService : IService<User> {
|
||||||
|
fun getUserWithPower(username: String): User?
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
package top.fatweb.api.service.permission.impl
|
package top.fatweb.api.service.permission.impl
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails
|
import org.springframework.security.core.userdetails.UserDetails
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService
|
import org.springframework.security.core.userdetails.UserDetailsService
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import top.fatweb.api.entity.permission.LoginUser
|
import top.fatweb.api.entity.permission.LoginUser
|
||||||
import top.fatweb.api.entity.permission.User
|
|
||||||
import top.fatweb.api.service.permission.IUserService
|
import top.fatweb.api.service.permission.IUserService
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class UserDetailsServiceImpl(val userService: IUserService) : UserDetailsService {
|
class UserDetailsServiceImpl(val userService: IUserService) : UserDetailsService {
|
||||||
override fun loadUserByUsername(username: String?): UserDetails {
|
override fun loadUserByUsername(username: String): UserDetails {
|
||||||
val user = userService.getOne(KtQueryWrapper(User()).eq(User::username, username))
|
val user = userService.getUserWithPower(username)
|
||||||
user ?: let { throw Exception("Username not found") }
|
user ?: let { throw Exception("Username not found") }
|
||||||
|
|
||||||
return LoginUser(user)
|
return LoginUser(user)
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
|||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import top.fatweb.api.entity.permission.User
|
import top.fatweb.api.entity.permission.User
|
||||||
import top.fatweb.api.mapper.permission.UserMapper
|
import top.fatweb.api.mapper.permission.UserMapper
|
||||||
|
import top.fatweb.api.service.permission.IElementService
|
||||||
|
import top.fatweb.api.service.permission.IMenuService
|
||||||
|
import top.fatweb.api.service.permission.IOperationService
|
||||||
import top.fatweb.api.service.permission.IUserService
|
import top.fatweb.api.service.permission.IUserService
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,4 +18,21 @@ import top.fatweb.api.service.permission.IUserService
|
|||||||
* @since 2023-10-04
|
* @since 2023-10-04
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
class UserServiceImpl : ServiceImpl<UserMapper, User>(), IUserService
|
class UserServiceImpl(
|
||||||
|
private val menuService: IMenuService,
|
||||||
|
private val elementService: IElementService,
|
||||||
|
private val operationService: IOperationService
|
||||||
|
) : ServiceImpl<UserMapper, User>(), IUserService {
|
||||||
|
override fun getUserWithPower(username: String): User? {
|
||||||
|
val user = baseMapper.getOneWithPowerByUsername(username)
|
||||||
|
user ?: let { return null }
|
||||||
|
|
||||||
|
if (user.id == 0L) {
|
||||||
|
user.menus = menuService.list()
|
||||||
|
user.elements = elementService.list()
|
||||||
|
user.operations = operationService.list()
|
||||||
|
}
|
||||||
|
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ insert into t_power (id, type_id)
|
|||||||
values (1010000, 1),
|
values (1010000, 1),
|
||||||
(1010100, 2),
|
(1010100, 2),
|
||||||
(1010101, 3),
|
(1010101, 3),
|
||||||
|
(100010000, 1),
|
||||||
(101010000, 1),
|
(101010000, 1),
|
||||||
(101010100, 2),
|
(101010100, 2),
|
||||||
(101010101, 3),
|
(101010101, 3),
|
||||||
@@ -30,10 +31,11 @@ values (1010000, 1),
|
|||||||
on duplicate key update type_id = values(type_id);
|
on duplicate key update type_id = values(type_id);
|
||||||
|
|
||||||
insert into t_menu (id, name, url, power_id, parent_id)
|
insert into t_menu (id, name, url, power_id, parent_id)
|
||||||
values (1010000, '公用', '/', id, null),
|
values (001010000, '公用', '/', id, null),
|
||||||
(101010000, '角色管理(权限相关)', '/power/role', id, null),
|
(100010000, '系统管理', '/system', id, null),
|
||||||
(102010000, '用户组管理(权限相关)', '/power/group', id, null),
|
(101010000, '角色管理(权限相关)', '/power/role', id, 100010000),
|
||||||
(103010000, '用户管理(权限相关)', '/power/user', id, null)
|
(102010000, '用户组管理(权限相关)', '/power/group', id, 100010000),
|
||||||
|
(103010000, '用户管理(权限相关)', '/power/user', id, 100010000)
|
||||||
on duplicate key update name = values(name),
|
on duplicate key update name = values(name),
|
||||||
url = values(url),
|
url = values(url),
|
||||||
power_id = values(power_id),
|
power_id = values(power_id),
|
||||||
|
|||||||
@@ -1,5 +1,88 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="top.fatweb.api.mapper.permission.UserMapper">
|
<mapper namespace="top.fatweb.api.mapper.permission.UserMapper">
|
||||||
|
<select id="getOneWithPowerByUsername" resultMap="userWithPowerMap">
|
||||||
|
select t_user.id as user_id,
|
||||||
|
t_user.username as user_username,
|
||||||
|
t_user.password as user_password,
|
||||||
|
t_user.locking as user_locking,
|
||||||
|
t_user.expiration as user_expiration,
|
||||||
|
t_user.credentials_expiration as user_credentials_expiration,
|
||||||
|
t_user.enable as user_enable,
|
||||||
|
t_user.last_login_time as user_last_login_time,
|
||||||
|
t_user.last_login_ip as user_last_login_ip,
|
||||||
|
t_user.create_time as user_create_time,
|
||||||
|
t_user.update_time as user_update_time,
|
||||||
|
t_user.deleted as user_deleted,
|
||||||
|
t_user.version as user_version,
|
||||||
|
tm.id as menu_id,
|
||||||
|
tm.name as menu_name,
|
||||||
|
tm.url as menu_url,
|
||||||
|
tm.power_id as menu_power_id,
|
||||||
|
tm.parent_id as menu_parent_id,
|
||||||
|
te.id as element_id,
|
||||||
|
te.name as element_name,
|
||||||
|
te.power_id as element_power_id,
|
||||||
|
te.menu_id as element_menu_id,
|
||||||
|
t.id as operation_id,
|
||||||
|
t.name as operation_name,
|
||||||
|
t.code as operation_code,
|
||||||
|
t.power_id as operation_power_id,
|
||||||
|
t.element_id as operation_element_id,
|
||||||
|
t.parent_id as operation_parent_id
|
||||||
|
from t_user
|
||||||
|
left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id
|
||||||
|
left join (select * from t_group where deleted = 0 and enable = 1) as tg on tg.id = tug.group_id
|
||||||
|
left join (select * from t_role_group where deleted = 0) as trg on tg.id = trg.group_id
|
||||||
|
left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id
|
||||||
|
left join (select * from t_role where deleted = 0 and enable = 1) as tr
|
||||||
|
on tr.id = trg.role_id or tr.id = tur.role_id
|
||||||
|
left join (select * from t_power_role where deleted = 0) as tpr on tr.id = tpr.role_id
|
||||||
|
left join t_power as tp on tp.id = tpr.power_id
|
||||||
|
left join t_menu as tm on tp.id = tm.power_id
|
||||||
|
left join t_element as te on tp.id = te.power_id
|
||||||
|
left join t_operation as t on tp.id = t.power_id
|
||||||
|
where t_user.deleted = 0
|
||||||
|
and t_user.username = #{username};
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<resultMap id="userBase" type="user">
|
||||||
|
<id property="id" column="user_id"/>
|
||||||
|
<result property="username" column="user_username"/>
|
||||||
|
<result property="locking" column="user_locking"/>
|
||||||
|
<result property="expiration" column="user_expiration"/>
|
||||||
|
<result property="credentialsExpiration" column="user_credentials_expiration"/>
|
||||||
|
<result property="enable" column="user_enable"/>
|
||||||
|
<result property="lastLoginTime" column="user_last_login_time"/>
|
||||||
|
<result property="lastLoginIp" column="user_last_login_ip"/>
|
||||||
|
<result property="createTime" column="user_create_time"/>
|
||||||
|
<result property="updateTime" column="user_update_time"/>
|
||||||
|
<result property="deleted" column="user_deleted"/>
|
||||||
|
<result property="version" column="user_version"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<resultMap id="userWithPowerMap" type="user" extends="userBase">
|
||||||
|
<result property="password" column="user_password"/>
|
||||||
|
<collection property="menus" ofType="menu">
|
||||||
|
<id property="id" column="menu_id"/>
|
||||||
|
<result property="name" column="menu_name"/>
|
||||||
|
<result property="url" column="menu_url"/>
|
||||||
|
<result property="powerId" column="menu_power_id"/>
|
||||||
|
<result property="parentId" column="menu_parent_id"/>
|
||||||
|
</collection>
|
||||||
|
<collection property="elements" ofType="element">
|
||||||
|
<id property="id" column="element_id"/>
|
||||||
|
<result property="name" column="element_name"/>
|
||||||
|
<result property="powerId" column="element_power_id"/>
|
||||||
|
<result property="menuId" column="element_menu_id"/>
|
||||||
|
</collection>
|
||||||
|
<collection property="operations" ofType="operation">
|
||||||
|
<id property="id" column="operation_id"/>
|
||||||
|
<result property="name" column="operation_name"/>
|
||||||
|
<result property="code" column="operation_code"/>
|
||||||
|
<result property="powerId" column="operation_power_id"/>
|
||||||
|
<result property="elementId" column="operation_element_id"/>
|
||||||
|
<result property="parentId" column="operation_parent_id"/>
|
||||||
|
</collection>
|
||||||
|
</resultMap>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user