-
Notifications
You must be signed in to change notification settings - Fork 0
/
principals.clj
54 lines (46 loc) · 1.78 KB
/
principals.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(ns pathological.principals
(:refer-clojure :exclude [name])
(:require
[pathological.file-systems :as fs])
(:import
[java.nio.file FileSystem]))
(defrecord UserPrincipal [name underlying]
java.nio.file.attribute.UserPrincipal
(getName [_] name))
(defrecord GroupPrincipal [name underlying]
java.nio.file.attribute.GroupPrincipal
(getName [_] name))
(defn lookup-principal-by-name
([name] (lookup-principal-by-name fs/*file-system* name))
([^FileSystem file-system name]
(let [lookup-service (.getUserPrincipalLookupService file-system)
user-principal (.lookupPrincipalByName lookup-service name)]
user-principal)))
(defn lookup-principal-by-group-name
([name] (lookup-principal-by-group-name fs/*file-system* name))
([^FileSystem file-system name]
(let [lookup-service (.getUserPrincipalLookupService file-system)
group-principal (.lookupPrincipalByGroupName lookup-service name)]
group-principal)))
(defn ->user-principal
([name] (->user-principal fs/*file-system* name))
([file-system name]
(let [underlying
(lookup-principal-by-name file-system name)]
(->UserPrincipal name underlying))))
(defn <-user-principal
([^java.nio.file.attribute.UserPrincipal principal]
(if-not (instance? UserPrincipal principal)
(->UserPrincipal (.getName principal) principal)
principal)))
(defn ->group-principal
([name] (->group-principal fs/*file-system* name))
([file-system name]
(let [underlying
(lookup-principal-by-group-name file-system name)]
(->GroupPrincipal name underlying))))
(defn <-group-principal
([^java.nio.file.attribute.GroupPrincipal principal]
(if-not (instance? GroupPrincipal principal)
(->GroupPrincipal (.getName principal) principal)
principal)))