feat : LDAP 增加分页

This commit is contained in:
wangzhengkun
2022-06-10 16:00:01 +08:00
parent 3976ec4459
commit 2e88a23aca
3 changed files with 94 additions and 30 deletions

View File

@@ -47,11 +47,11 @@ func (l *Ldap) Connect() error {
func (l *Ldap) Search(dn, filter string, sizeLimit, timeLimit int, attributes []string) ([]*ldap.Entry, error) { func (l *Ldap) Search(dn, filter string, sizeLimit, timeLimit int, attributes []string) ([]*ldap.Entry, error) {
searchRequest := ldap.NewSearchRequest(dn, searchRequest := ldap.NewSearchRequest(dn,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, sizeLimit, timeLimit, false, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, timeLimit, false,
filter, filter,
attributes, attributes,
nil) nil)
sr, err := l.Conn.Search(searchRequest) sr, err := l.Conn.SearchWithPaging(searchRequest, uint32(sizeLimit))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -81,5 +81,6 @@ func (l *Ldap) Login(dn, filter, password string, sizeLimit, timeLimit int) erro
return err return err
} }
defer l.Conn.Close() defer l.Conn.Close()
return nil return nil
} }

View File

@@ -0,0 +1,32 @@
package ldap
import (
"fmt"
"github.com/go-ldap/ldap/v3"
"testing"
)
func TestLdapClient(t *testing.T) {
client := NewLdapClient("172.16.10.89", "389", "CN=zhengkun2,CN=Users,DC=ko,DC=com", "Calong@2015", false)
err := client.Connect()
if err != nil {
fmt.Println(err.Error())
return
}
for i := 1; i < 1200; i++ {
username := fmt.Sprintf("kubepi%d", i)
email := username + "@fit2cloud.com"
userdn := "CN=" + username + ",CN=Users,DC=ko,DC=com"
add := ldap.NewAddRequest(userdn, nil)
add.Attribute("objectClass", []string{"top", "person", "organizationalPerson", "user"})
add.Attribute("cn", []string{username})
add.Attribute("sAMAccountName", []string{username})
add.Attribute("mail", []string{email})
err = client.Conn.Add(add)
if err != nil {
fmt.Println(err.Error())
return
}
}
}

View File

@@ -43,7 +43,7 @@
</div> </div>
</el-form-item> </el-form-item>
<el-form-item style="width: 100%" prop="enable"> <el-form-item style="width: 100%" prop="enable">
<el-checkbox v-model="form.enable">{{ $t('commons.enable.true') }}</el-checkbox> <el-checkbox v-model="form.enable">{{ $t("commons.enable.true") }}</el-checkbox>
</el-form-item> </el-form-item>
<div style="float: right"> <div style="float: right">
<el-form-item> <el-form-item>
@@ -67,10 +67,10 @@
$t("business.user.ldap_remake") $t("business.user.ldap_remake")
}} }}
</el-button> </el-button>
<!-- <el-button @click="sync" :disabled="isSubmitGoing" v-has-permissions="{resource:'ldap',verb:'create'}">{{--> <!-- <el-button @click="sync" :disabled="isSubmitGoing" v-has-permissions="{resource:'ldap',verb:'create'}">{{-->
<!-- $t("commons.button.sync")--> <!-- $t("commons.button.sync")-->
<!-- }}--> <!-- }}-->
<!-- </el-button>--> <!-- </el-button>-->
<el-button type="primary" @click="onSubmit" :disabled="isSubmitGoing" <el-button type="primary" @click="onSubmit" :disabled="isSubmitGoing"
v-has-permissions="{resource:'ldap',verb:'create'}">{{ $t("commons.button.confirm") }} v-has-permissions="{resource:'ldap',verb:'create'}">{{ $t("commons.button.confirm") }}
</el-button> </el-button>
@@ -103,8 +103,14 @@
</div> </div>
</el-form> </el-form>
</el-dialog> </el-dialog>
<el-dialog :visible.sync="importUserPageOpen" :title="$t('business.user.import_user')"> <el-dialog :visible.sync="importUserPageOpen" :title="$t('business.user.import_user')" style="height: 900px">
<complex-table :data="users" style="width: 100%" :selects.sync="selects" :loading="connectLoading"> <div style="text-align: right;margin-bottom: 10px">
<el-input v-model="searchName" style="width: 30%" size="mini" clearable @blur="handleSearch"/>
</div>
<el-table
:data="tableUsers.slice((pageConfig.currentPage-1)*pageConfig.pageSize,pageConfig.currentPage*pageConfig.pageSize)"
:loading="connectLoading"
@selection-change="handleSelectionChange">
<el-table-column type="selection" fix :selectable="importAvailable"></el-table-column> <el-table-column type="selection" fix :selectable="importAvailable"></el-table-column>
<el-table-column :label="$t('commons.table.name')" prop="name" min-width="100" fix> <el-table-column :label="$t('commons.table.name')" prop="name" min-width="100" fix>
</el-table-column> </el-table-column>
@@ -117,17 +123,21 @@
{{ row.available ? $t("commons.bool.true") : $t("commons.bool.false") }} {{ row.available ? $t("commons.bool.true") : $t("commons.bool.false") }}
</template> </template>
</el-table-column> </el-table-column>
</complex-table> </el-table>
<div style="height: 30px;margin-top: 10px"> <div style="height: 30px;margin-top: 10px;margin-bottom: 20px;text-align: right">
<div style="float: right"> <el-pagination
<el-button @click="importUserPageOpen=false" :disabled="importLoading" :current-page.sync="pageConfig.currentPage"
v-has-permissions="{resource:'ldap',verb:'create'}"> :page-size="pageConfig.pageSize"
{{ $t("commons.button.cancel") }} layout="prev, pager, next"
</el-button> :total="tableUsers.length">
<el-button type="primary" @click="userImport" :disabled="importLoading" </el-pagination>
v-has-permissions="{resource:'ldap',verb:'create'}">{{ $t("commons.button.confirm") }} <el-button @click="importUserPageOpen=false" :disabled="importLoading"
</el-button> v-has-permissions="{resource:'ldap',verb:'create'}">
</div> {{ $t("commons.button.cancel") }}
</el-button>
<el-button type="primary" @click="userImport" :disabled="importLoading"
v-has-permissions="{resource:'ldap',verb:'create'}">{{ $t("commons.button.confirm") }}
</el-button>
</div> </div>
</el-dialog> </el-dialog>
</layout-content> </layout-content>
@@ -144,7 +154,7 @@ import ComplexTable from "@/components/complex-table"
export default { export default {
name: "LDAP", name: "LDAP",
components: { ComplexTable,LayoutContent }, components: { ComplexTable, LayoutContent },
props: {}, props: {},
data () { data () {
return { return {
@@ -187,7 +197,13 @@ export default {
importUserPageOpen: false, importUserPageOpen: false,
importLoading: false, importLoading: false,
connectLoading: false, connectLoading: false,
oldConfig: {} oldConfig: {},
pageConfig: {
currentPage: 1,
pageSize: 10,
},
tableUsers: [],
searchName: ""
} }
}, },
methods: { methods: {
@@ -219,14 +235,18 @@ export default {
if (!isFormReady) { if (!isFormReady) {
return return
} }
this.tableUsers = []
this.loading = true
this.connectLoading = true this.connectLoading = true
testConnect(this.form).then(res => { testConnect(this.form).then(res => {
this.users = res.data this.users = res.data
this.tableUsers = this.users
this.$message({ this.$message({
type: "success", type: "success",
message: this.$t("business.user.test_result", { count: res.data.length }) message: this.$t("business.user.test_result", { count: res.data.length })
}) })
}).finally(() => { }).finally(() => {
this.loading = false
this.connectLoading = false this.connectLoading = false
}) })
}, },
@@ -236,15 +256,17 @@ export default {
}, },
openImportPage () { openImportPage () {
this.importUserPageOpen = true this.importUserPageOpen = true
this.searchName = ""
if (this.users.length === 0) { if (this.users.length === 0) {
this.connectTest() this.connectTest()
} }
this.tableUsers = this.users
}, },
importAvailable(row) { importAvailable (row) {
return row.available return row.available
}, },
remake(){ remake () {
this.form = JSON.parse(JSON.stringify( this.oldConfig)) this.form = JSON.parse(JSON.stringify(this.oldConfig))
}, },
loginTest () { loginTest () {
let isFormReady = false let isFormReady = false
@@ -256,6 +278,7 @@ export default {
if (!isFormReady) { if (!isFormReady) {
return return
} }
this.loginLoading = true
testLogin(this.loginForm).then(() => { testLogin(this.loginForm).then(() => {
this.$message({ this.$message({
type: "success", type: "success",
@@ -265,12 +288,12 @@ export default {
this.loginLoading = false this.loginLoading = false
}) })
}, },
userImport(){ userImport () {
if (this.selects.length === 0) { if (this.selects.length === 0) {
return return
} }
let req = { let req = {
users : this.selects users: this.selects
} }
this.importLoading = true this.importLoading = true
importUser(req).then(res => { importUser(req).then(res => {
@@ -280,10 +303,10 @@ export default {
message: this.$t("business.user.import_user_success") message: this.$t("business.user.import_user_success")
}) })
this.importUserPageOpen = false this.importUserPageOpen = false
}else { } else {
this.$message({ this.$message({
type: "failed", type: "failed",
message: this.$t("business.user.import_user_failed",{user:res.failures}) message: this.$t("business.user.import_user_failed", { user: res.failures })
}) })
} }
}).finally(() => { }).finally(() => {
@@ -335,11 +358,19 @@ export default {
getLdap().then((res) => { getLdap().then((res) => {
if (res.data.length > 0) { if (res.data.length > 0) {
this.form = res.data[0] this.form = res.data[0]
this.oldConfig = res.data[0] this.oldConfig = res.data[0]
} }
}).finally(() => { }).finally(() => {
this.loading = false this.loading = false
}) })
},
handleSelectionChange(val) {
this.selects = val
},
handleSearch(){
this.tableUsers =this.users.filter(user => {
return user.name.indexOf(this.searchName) > -1
})
} }
}, },
created () { created () {