Get groups by gid

merge-requests/6/head
meskio 5 years ago
parent bbe5e9721f
commit 1e0d028433
Signed by: meskio
GPG Key ID: 52B8F5AC97A2DA86
  1. 2
      examples/data.ldif
  2. 13
      ldap/group.go
  3. 18
      ldap/group_test.go
  4. 11
      server/admin.go
  5. 2
      tmpl/groups.html
  6. 4
      tmpl/user.html
  7. 2
      tmpl/users.html

@ -5,6 +5,7 @@ ou: People
dn: uid=user,ou=People,dc=nodomain
uid: user
cn: User
sn: user
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
@ -22,6 +23,7 @@ mailRoutingAddress: user@mail.nodomain
dn: uid=superuser,ou=People,dc=nodomain
uid: superuser
cn: SuperUser
sn: superuser
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount

@ -48,6 +48,19 @@ func (l Ldap) GetGroup(name string) (Group, error) {
}
// GetGID returns the group matching the gid
func (l Ldap) GetGID(gid int) (Group, error) {
filter := fmt.Sprintf("(&(objectClass=posixGroup)(gidNumber=%d))", gid)
groups, err := l.searchGroup(filter)
if err != nil {
return Group{}, err
}
if len(groups) == 0 {
return Group{}, errors.New("Can't find group " + strconv.Itoa(gid))
}
return groups[0], nil
}
// ListGroups returns all groups in ldap with members
func (l Ldap) ListGroups() ([]Group, error) {
filter := "(&(objectClass=posixGroup)(memberUid=*))"

@ -40,6 +40,24 @@ func TestGetGroup(t *testing.T) {
}
}
func TestGetGID(t *testing.T) {
l := testLdap(t)
group, err := l.GetGID(1)
if err != nil {
t.Fatalf("GetGID(1) failed: %v", err)
}
if group.Name != "adm" {
t.Errorf("Wrong group name not in adm: %v", group)
}
if group.Members[0] != "superuser" {
t.Errorf("superuser not part of adm: %v", group)
}
if group.GID != 1 {
t.Errorf("Wrong GID in adm: %v", group)
}
}
func TestListGroups(t *testing.T) {
l := testLdap(t)

@ -41,14 +41,19 @@ func (s *server) userHandler(w http.ResponseWriter, r *http.Request) {
s.errorHandler(w, r)
return
}
mainGroup, err := s.ldap.GetGID(user.GID)
if err != nil {
log.Println("An error ocurred retrieving user '", userName, "' main group: ", err)
}
groups, err := s.ldap.UserGroups(userName)
if err != nil {
log.Println("An error ocurred retrieving user '", userName, "' groups: ", err)
}
data := struct {
User ldap.User
Groups []ldap.Group
}{user, groups}
User ldap.User
MainGroup ldap.Group
Groups []ldap.Group
}{user, mainGroup, groups}
response.execute(data)
}

@ -6,6 +6,7 @@
<tr>
<th scope="col">Nombre</th>
<th scope="col">Miembros</th>
<th scope="col">GID</th>
</tr>
</thead>
<tbody>
@ -17,6 +18,7 @@
<a href="/users/{{.}}">{{.}}</a>,
{{end}}
</td>
<td>{{.GID}}</a></th>
</tr>
{{end}}
</tbody>

@ -17,7 +17,9 @@
</dd>
{{end}}
<dt>UID</dt><dd>{{.User.UID}}</dd>
<dt>GID</dt><dd>{{.User.GID}}</dd>
{{with .MainGroup}}
<dt>GID</dt><dd><a href="/groups/{{.Name}}">{{.Name}} ({{.GID}})</a></dd>
{{end}}
</dl>
</div>

@ -7,6 +7,7 @@
<th scope="col">Nombre</th>
<th scope="col">Shell</th>
<th scope="col">Home</th>
<th scope="col">Mail</th>
<th scope="col">UID</th>
<th scope="col">GID</th>
</tr>
@ -17,6 +18,7 @@
<th scope="row"><a href="/users/{{.Name}}">{{.Name}}</a></th>
<td>{{.Shell}}</td>
<td>{{.Home}}</td>
<td>{{.Mail}}</td>
<td>{{.UID}}</td>
<td>{{.GID}}</td>
</tr>

Loading…
Cancel
Save