Commit eb8a0359 authored by ale's avatar ale

Move some code to the 'util' section

parent 5806a8b8
......@@ -606,35 +606,3 @@ func (tx *backendTX) CanAccessResource(_ context.Context, username string, rsrc
return owner == username
}
}
// Find the uid= in the DN. Return an empty string on failure.
func ownerFromDN(dn string) string {
parsed, err := ldap.ParseDN(dn)
if err != nil {
return ""
}
// Start looking for uid from the right. The strict
// greater-than clause ignores the first RDN (so that the
// owner of a user is not itself).
for i := len(parsed.RDNs) - 1; i > 0; i-- {
attr := parsed.RDNs[i].Attributes[0]
if attr.Type == "uid" {
return attr.Value
}
}
return ""
}
const oneDay = 86400
func encodeShadowTimestamp(t time.Time) string {
d := t.UTC().Unix() / oneDay
return strconv.FormatInt(d, 10)
}
func decodeShadowTimestamp(s string) (t time.Time) {
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
t = time.Unix(i*oneDay, 0).UTC()
}
return
}
......@@ -454,10 +454,6 @@ func (h *databaseResourceHandler) SearchQuery() *queryTemplate {
}
}
func joinDN(parts ...string) string {
return strings.Join(parts, ",")
}
func newDefaultResourceRegistry(baseDN string) *resourceRegistry {
reg := newResourceRegistry()
reg.register(as.ResourceTypeEmail, &emailResourceHandler{baseDN: baseDN})
......
......@@ -2,6 +2,9 @@ package backend
import (
"os"
"strconv"
"strings"
"time"
"gopkg.in/ldap.v2"
)
......@@ -78,3 +81,42 @@ func isObjectClass(entry *ldap.Entry, class string) bool {
}
return false
}
// Join some RDNs.
func joinDN(parts ...string) string {
return strings.Join(parts, ",")
}
// Find the uid= in the DN. Return an empty string on failure.
func ownerFromDN(dn string) string {
parsed, err := ldap.ParseDN(dn)
if err != nil {
return ""
}
// Start looking for uid from the right. The strict
// greater-than clause ignores the first RDN (so that the
// owner of a user is not itself).
for i := len(parsed.RDNs) - 1; i > 0; i-- {
attr := parsed.RDNs[i].Attributes[0]
if attr.Type == "uid" {
return attr.Value
}
}
return ""
}
// Functions to encode/decode shadow(5) timestamps (number of days
// since the epoch).
const oneDay = 86400
func encodeShadowTimestamp(t time.Time) string {
d := t.UTC().Unix() / oneDay
return strconv.FormatInt(d, 10)
}
func decodeShadowTimestamp(s string) (t time.Time) {
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
t = time.Unix(i*oneDay, 0).UTC()
}
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment