Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When binding roles, the user list is changed to the drop-down selection mode (在绑定角色的时候用户列表改成下拉选中的模式) #3518 #3522

Merged
merged 17 commits into from
Aug 6, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.stereotype.Component;

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

import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.ROLE_INFO_ROW_MAPPER;

Expand Down Expand Up @@ -134,4 +135,11 @@ public void deleteRole(String role, String username) {
}
}

@Override
public List<String> findRolesLikeRoleName(String role) {
String sql = "SELECT role FROM roles WHERE role like ? ";
List<String> users = databaseOperate.queryMany(sql, new String[] {"%" + role + "%"}, String.class);
return users;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.stereotype.Component;

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

import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.USER_ROW_MAPPER;

Expand Down Expand Up @@ -117,4 +118,11 @@ public Page<User> getUsers(int pageNo, int pageSize) {
}
return pageInfo;
}

@Override
public List<String> findUserLikeUsername(String username) {
String sql = "SELECT username FROM users WHERE username like ? ";
List<String> users = databaseOperate.queryMany(sql, new String[] {"%" + username + "%"}, String.class);
return users;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.ROLE_INFO_ROW_MAPPER;

Expand Down Expand Up @@ -152,6 +153,13 @@ public void deleteRole(String role, String username) {
}
}

@Override
public List<String> findRolesLikeRoleName(String role) {
String sql = "SELECT role FROM roles WHERE role like '%' ? '%'";
List<String> users = this.jt.queryForList(sql, new String[]{role}, String.class);
return users;
}

private static final class RoleInfoRowMapper implements RowMapper<RoleInfo> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.USER_ROW_MAPPER;

Expand Down Expand Up @@ -145,4 +146,11 @@ public Page<User> getUsers(int pageNo, int pageSize) {
throw e;
}
}

@Override
public List<String> findUserLikeUsername(String username) {
String sql = "SELECT username FROM users WHERE username like '%' ? '%'";
List<String> users = this.jt.queryForList(sql, new String[]{username}, String.class);
return users;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alibaba.nacos.config.server.model.Page;

import java.util.List;

/**
* Role CRUD service.
*
Expand All @@ -37,4 +39,5 @@ public interface RolePersistService {

void deleteRole(String role, String username);

List<String> findRolesLikeRoleName(String role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.User;

import java.util.List;

/**
* User CRUD service.
*
Expand All @@ -37,5 +39,7 @@ public interface UserPersistService {
User findUserByUsername(String username);

Page<User> getUsers(int pageNo, int pageSize);


List<String> findUserLikeUsername(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* Role operation controller.
*
Expand Down Expand Up @@ -58,6 +60,18 @@ public Object getRoles(@RequestParam int pageNo, @RequestParam int pageSize,
return roleService.getRolesFromDatabase(username, pageNo, pageSize);
}

/**
* Fuzzy matching role name .
*
* @param role role id
* @return role list
*/
@GetMapping("/search")
@Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "roles", action = ActionTypes.READ)
public List<String> searchRoles(@RequestParam String role) {
return roleService.findRolesLikeRoleName(role);
}

/**
* Add a role to a user
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,17 @@ public RestResult<String> updatePassword(@RequestParam(value = "oldPassword") St
}
return rr;
}


/**
* Fuzzy matching username.
*
* @param username username
* @return Matched username
*/
@GetMapping("/search")
@Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE)
public List<String> searchUsersLikeUsername(@RequestParam String username) {
return userDetailsService.findUserLikeUsername(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,8 @@ public void addPermission(String role, String resource, String action) {
public void deletePermission(String role, String resource, String action) {
permissionPersistService.deletePermission(role, resource, action);
}

public List<String> findRolesLikeRoleName(String role) {
return rolePersistService.findRolesLikeRoleName(role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -99,7 +100,11 @@ public User getUser(String username) {
public User getUserFromDatabase(String username) {
return userPersistService.findUserByUsername(username);
}


public List<String> findUserLikeUsername(String username) {
return userPersistService.findUserLikeUsername(username);
}

public void createUser(String username, String password) {
userPersistService.createUser(username, password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import PropTypes from 'prop-types';
import { Field, Form, Input, Select, Dialog, ConfigProvider } from '@alifd/next';
import { connect } from 'react-redux';
import { getNamespaces } from '../../../reducers/namespace';
import { searchRoles } from '../../../reducers/authority';

const FormItem = Form.Item;
const { Option } = Select;
Expand All @@ -28,7 +29,7 @@ const formItemLayout = {
wrapperCol: { span: 19 },
};

@connect(state => ({ namespaces: state.namespace.namespaces }), { getNamespaces })
@connect(state => ({ namespaces: state.namespace.namespaces }), { getNamespaces, searchRoles })
@ConfigProvider.config
class NewPermissions extends React.Component {
static displayName = 'NewPermissions';
Expand All @@ -39,11 +40,15 @@ class NewPermissions extends React.Component {
locale: PropTypes.object,
visible: PropTypes.bool,
getNamespaces: PropTypes.func,
getRoles: PropTypes.func,
onOk: PropTypes.func,
onCancel: PropTypes.func,
namespaces: PropTypes.array,
};

state = {
dataSource: [],
};
componentDidMount() {
this.props.getNamespaces();
}
Expand All @@ -68,6 +73,14 @@ class NewPermissions extends React.Component {
return null;
}

handleChange = value => {
if (value.length > 0) {
searchRoles(value).then(val => {
this.setState({ dataSource: val });
});
}
};

render() {
const { getError } = this.field;
const { visible, onOk, onCancel, locale, namespaces } = this.props;
Expand All @@ -88,7 +101,14 @@ class NewPermissions extends React.Component {
>
<Form style={{ width: 400 }} {...formItemLayout} field={this.field}>
<FormItem label={locale.role} required help={getError('role')}>
<Input name="role" trim placeholder={locale.rolePlaceholder} />
<Select.AutoComplete
name="role"
style={{ width: 316 }}
filterLocal={false}
placeholder={locale.rolePlaceholder}
onChange={this.handleChange}
dataSource={this.state.dataSource}
/>
</FormItem>
<FormItem label={locale.resource} required help={getError('resource')}>
<Select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

import React from 'react';
import PropTypes from 'prop-types';
import { Field, Form, Input, Dialog, ConfigProvider } from '@alifd/next';
import { connect } from 'react-redux';
import { Field, Form, Input, Dialog, ConfigProvider, Select } from '@alifd/next';
import { searchUsers } from '../../../reducers/authority';

const FormItem = Form.Item;

const formItemLayout = {
labelCol: { fixedSpan: 4 },
wrapperCol: { span: 19 },
};

@connect(state => ({ users: state.authority.users }), { searchUsers })
@ConfigProvider.config
class NewRole extends React.Component {
static displayName = 'NewRole';
Expand All @@ -35,9 +37,14 @@ class NewRole extends React.Component {
locale: PropTypes.object,
visible: PropTypes.bool,
onOk: PropTypes.func,
getUsers: PropTypes.func,
onCancel: PropTypes.func,
};

state = {
dataSource: [],
};

check() {
const { locale } = this.props;
const errors = {
Expand All @@ -57,6 +64,14 @@ class NewRole extends React.Component {
return null;
}

handleChange = value => {
if (value.length > 0) {
searchUsers(value).then(val => {
this.setState({ dataSource: val });
});
}
};

render() {
const { locale } = this.props;
const { getError } = this.field;
Expand All @@ -81,7 +96,14 @@ class NewRole extends React.Component {
<Input name="role" trim placeholder={locale.rolePlaceholder} />
</FormItem>
<FormItem label={locale.username} required help={getError('username')}>
<Input name="username" placeholder={locale.usernamePlaceholder} />
<Select.AutoComplete
name="username"
style={{ width: 316 }}
filterLocal={false}
placeholder={locale.usernamePlaceholder}
onChange={this.handleChange}
dataSource={this.state.dataSource}
/>
</FormItem>
</Form>
</Dialog>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ const getUsers = params => dispatch =>
const createUser = ([username, password]) =>
request.post('v1/auth/users', { username, password }).then(res => successMsg(res));

/**
* 通过username 模糊匹配
* @param {*} param0
*/
const searchUsers = username =>
request.get('v1/auth/users/search', { params: { username } }).then(res => successMsg(res));

/**
* 删除用户
* @param {*} username
Expand All @@ -82,6 +89,13 @@ const passwordReset = ([username, newPassword]) =>
const getRoles = params => dispatch =>
request.get('v1/auth/roles', { params }).then(data => dispatch({ type: ROLE_LIST, data }));

/**
* 通过username 模糊匹配
* @param {*} param0
*/
const searchRoles = role =>
request.get('v1/auth/roles/search', { params: { role } }).then(res => successMsg(res));

/**
* 创建角色
* @param {*} param0
Expand Down Expand Up @@ -133,10 +147,12 @@ export default (state = initialState, action) => {
};

export {
searchUsers,
getUsers,
createUser,
deleteUser,
passwordReset,
searchRoles,
getRoles,
createRole,
deleteRole,
Expand Down
12 changes: 6 additions & 6 deletions console/src/main/resources/static/js/main.js

Large diffs are not rendered by default.