Skip to content

Commit

Permalink
Add ldap support (DataLinkDC#2029)
Browse files Browse the repository at this point in the history
* fix BusException logic error

if BusException.msg is empty, use I18n message

* add ldap ui

* add ldap login support

* add some i81n config

* fix code style
  • Loading branch information
gaoyan1998 authored Jun 7, 2023
1 parent 295146e commit 26979fd
Show file tree
Hide file tree
Showing 23 changed files with 699 additions and 31 deletions.
4 changes: 4 additions & 0 deletions dinky-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
61 changes: 61 additions & 0 deletions dinky-admin/src/main/java/org/dinky/controller/LdapController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.controller;

import org.dinky.data.model.SystemConfiguration;
import org.dinky.data.result.Result;
import org.dinky.service.LdapService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/ldap")
@RequiredArgsConstructor
public class LdapController {

@Autowired LdapService ldapService;

/** Gets the LDAP configuration status */
@GetMapping("/ldapEnableStatus")
public Result<Boolean> ldapStatus() {
return Result.succeed(
SystemConfiguration.getInstances().getLdapEnable().getValue(), "获取成功");
}

// @PostMapping("/listUser")
// public ProTableResult<User> listUser(@RequestBody LdapConfig ldapConfig) {
// List<User> users = ldapService.listUsers(ldapConfig);
// return ProTableResult.<User>builder()
// .success(true)
// .data(users)
// .total((long) users.size())
//// .current(1)
//// .pageSize(1)
// .build();
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.dinky.data.dto.ModifyPasswordDTO;
import org.dinky.data.enums.Status;
import org.dinky.data.model.User;
import org.dinky.data.model.UserTenant;
import org.dinky.data.params.AssignRoleParams;
import org.dinky.data.result.ProTableResult;
import org.dinky.data.result.Result;
Expand All @@ -42,7 +41,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;

import cn.hutool.core.lang.Dict;
Expand Down Expand Up @@ -214,16 +212,7 @@ public Result<Void> assignRole(@RequestBody AssignRoleParams assignRoleParams) {
@GetMapping("/getUserListByTenantId")
public Result<Dict> getUserListByTenantId(@RequestParam("id") Integer id) {
List<User> userList = userService.list();
List<UserTenant> userTenants =
userTenantService
.getBaseMapper()
.selectList(
new LambdaQueryWrapper<UserTenant>()
.eq(UserTenant::getTenantId, id));
List<Integer> userIds = new ArrayList<>();
for (UserTenant userTenant : userTenants) {
userIds.add(userTenant.getUserId());
}
List<Integer> userIds = userService.getUserIdsByTeantId(id);
Dict result = Dict.create().set("users", userList).set("userIds", userIds);
return Result.succeed(result);
}
Expand Down
1 change: 1 addition & 0 deletions dinky-admin/src/main/java/org/dinky/data/dto/LoginDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public class LoginDTO {
private String password;
private Integer tenantId;
private boolean autoLogin;
private boolean ldapLogin;
}
37 changes: 37 additions & 0 deletions dinky-admin/src/main/java/org/dinky/data/enums/UserType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.data.enums;

public enum UserType {
LDAP(0, "LDAP"),
LOCAL(1, "LOCAL");

private final int code;
private final String type;

public int getCode() {
return this.code;
}

UserType(int code, String type) {
this.code = code;
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.data.exception;

import org.dinky.data.enums.Status;

import lombok.Data;

/**
* AuthException About login error
*
* @since 2021/5/28 14:21
*/
@Data
public class AuthException extends Exception {

private Status status;

public AuthException(Status status) {
super(status.getMsg());
this.status = status;
}

public AuthException(Throwable cause, Status status) {
super(status.getMsg(), cause);
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.data.model;

import javax.naming.directory.Attributes;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LdapUserIdentification {

private String absoluteDn;
private String relativeDn;
private Attributes attributes;
}
2 changes: 2 additions & 0 deletions dinky-admin/src/main/java/org/dinky/data/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class User implements Serializable {

private String nickname;

private int userType;

private String worknum;

private byte[] avatar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/** assign role params */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AssignUserToTenantParams {
Integer tenantId;
List<Integer> userIds;
Expand Down
39 changes: 39 additions & 0 deletions dinky-admin/src/main/java/org/dinky/service/LdapService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.service;

import org.dinky.data.dto.LoginDTO;
import org.dinky.data.exception.AuthException;
import org.dinky.data.model.User;

import javax.naming.NamingException;

public interface LdapService {
// List<User> listUsers(LdapConfig ldapConfig);

/**
* Authenticates the user based on the provided login credentials. Throws AuthException if
* authentication fails.
*
* @param loginDTO The login user info
* @return ldap user
*/
User authenticate(LoginDTO userDTO) throws AuthException, NamingException;
}
7 changes: 7 additions & 0 deletions dinky-admin/src/main/java/org/dinky/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,11 @@ public interface UserService extends ISuperService<User> {

/** user loginout */
void outLogin();

/**
* get user ids where user in given tenant id
*
* @return role select permissions list
*/
List<Integer> getUserIdsByTeantId(int id);
}
Loading

0 comments on commit 26979fd

Please sign in to comment.