Skip to content

Commit

Permalink
feat(ldap): show groups in a better format (#55)
Browse files Browse the repository at this point in the history
* feat(ldap): show list of groups

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

* fix(ldap): rename group search button
  • Loading branch information
chiptus committed Nov 11, 2020
1 parent 085ee04 commit dc43708
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
29 changes: 21 additions & 8 deletions api/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions api/portainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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">
Expand Down
13 changes: 11 additions & 2 deletions app/portainer/settings/authentication/ldap/ldap.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dc43708

Please sign in to comment.