feat(ldap): show groups in a better format (#55)

* feat(ldap): show list of groups

* feat(ldap): show only the cn part of the username

* fix(ldap): rename group search button
This commit is contained in:
Chaim Lev-Ari
2020-11-11 22:33:24 +02:00
committed by GitHub
parent 085ee043d9
commit dc437084f2
6 changed files with 38 additions and 20 deletions
+21 -8
View File
@@ -165,6 +165,7 @@ func (*Service) SearchUsers(settings *portainer.LDAPSettings) ([]string, error)
// SearchGroups searches for groups with the specified settings
func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAPUser, error) {
type groupSet map[string]bool
connection, err := createConnection(settings)
if err != nil {
@@ -179,7 +180,7 @@ func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAP
}
}
users := []portainer.LDAPUser{}
userGroups := map[string]groupSet{}
for _, searchSettings := range settings.GroupSearchSettings {
searchRequest := ldap.NewSearchRequest(
@@ -190,25 +191,37 @@ func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAP
nil,
)
// Deliberately skip errors on the search request so that we can jump to other search settings
// if any issue arise with the current one.
sr, err := connection.Search(searchRequest)
if err != nil {
return users, err
return nil, err
}
for _, entry := range sr.Entries {
members := entry.GetAttributeValues(searchSettings.GroupAttribute)
for _, username := range members {
user := portainer.LDAPUser{
Name: username,
Group: entry.GetAttributeValue("cn"),
_, ok := userGroups[username]
if !ok {
userGroups[username] = groupSet{}
}
users = append(users, user)
userGroups[username][entry.GetAttributeValue("cn")] = true
}
}
}
users := []portainer.LDAPUser{}
for username, groups := range userGroups {
groupList := []string{}
for group := range groups {
groupList = append(groupList, group)
}
user := portainer.LDAPUser{
Name: username,
Groups: groupList,
}
users = append(users, user)
}
return users, nil
}
+2 -2
View File
@@ -422,8 +422,8 @@ type (
// LDAPUser represents a LDAP user
LDAPUser struct {
Name string
Group string
Name string
Groups []string
}
// LicenseInfo represents aggregated information about an instance license
@@ -52,7 +52,7 @@
</div>
<div class="col-sm-12" style="margin-top: 10px;">
<button class="btn btm-sm btn-primary" type="button" ng-click="$ctrl.search()">
Display Groups
Display User/Group matching
</button>
</div>
</div>
@@ -20,7 +20,7 @@
</div>
<div class="col-sm-12" style="margin-top: 10px;">
<button class="btn btm-sm btn-primary" type="button" ng-click="$ctrl.search()">
Display Groups
Display User/Group matching
</button>
</div>
</div>
@@ -28,11 +28,7 @@
</a>
</th>
<th>
<a ng-click="$ctrl.changeOrderBy('Name')">
Group Name
<i class="fa fa-sort-alpha-down" aria-hidden="true" ng-if="$ctrl.state.orderBy === 'Name' && !$ctrl.state.reverseOrder"></i>
<i class="fa fa-sort-alpha-up" aria-hidden="true" ng-if="$ctrl.state.orderBy === 'Name' && $ctrl.state.reverseOrder"></i>
</a>
Groups
</th>
</tr>
</thead>
@@ -45,7 +41,7 @@
{{ item.Name }}
</td>
<td>
{{ item.Group }}
<p ng-repeat="group in item.Groups" style="margin: 0;">{{ group }}</p>
</td>
</tr>
<tr ng-if="!$ctrl.dataset">
@@ -6,8 +6,17 @@ export function LDAPService(LDAP) {
return LDAP.users({ ldapSettings }).$promise;
}
function groups(ldapSettings) {
return LDAP.groups({ ldapSettings }).$promise;
async function groups(ldapSettings) {
const userGroups = await LDAP.groups({ ldapSettings }).$promise;
return userGroups.map(({ Name, Groups }) => {
let name = Name;
if (Name.includes(',') && Name.includes('=')) {
const [cnName] = Name.split(',');
const split = cnName.split('=');
name = split[1];
}
return { Groups, Name: name };
});
}
function check(ldapSettings) {