@@ -40,6 +40,25 @@ type CasbinRule struct {
40
40
V5 string
41
41
}
42
42
43
+ // Config represents the configuration for the Redis adapter.
44
+ type Config struct {
45
+ // Network is the network type, e.g., "tcp", "unix"
46
+ Network string
47
+ // Address is the Redis server address, e.g., "127.0.0.1:6379"
48
+ Address string
49
+ // Key is the Redis key to store Casbin rules (default: "casbin_rules")
50
+ Key string
51
+ // Username for Redis authentication (optional)
52
+ Username string
53
+ // Password for Redis authentication (optional)
54
+ Password string
55
+ // TLSConfig for secure connections (optional)
56
+ TLSConfig * tls.Config
57
+ // Pool is an existing Redis connection pool (optional)
58
+ // If provided, Network, Address, Username, Password, and TLSConfig are ignored
59
+ Pool * redis.Pool
60
+ }
61
+
43
62
// Adapter represents the Redis adapter for policy storage.
44
63
type Adapter struct {
45
64
network string
@@ -78,94 +97,148 @@ func finalizer(a *Adapter) {
78
97
}
79
98
}
80
99
81
- func newAdapter (network string , address string , key string ,
82
- username string , password string ) (* Adapter , error ) {
100
+ // NewAdapter creates a new Redis adapter with the provided configuration.
101
+ func NewAdapter (config * Config ) (* Adapter , error ) {
102
+ if config == nil {
103
+ return nil , errors .New ("config cannot be nil" )
104
+ }
105
+
83
106
a := & Adapter {}
84
- a .network = network
85
- a .address = address
86
- a .key = key
87
- a .username = username
88
- a .password = password
89
107
90
- // Open the DB, create it if not existed.
91
- err := a .open ()
108
+ // Set default key if not provided
109
+ if config .Key == "" {
110
+ a .key = "casbin_rules"
111
+ } else {
112
+ a .key = config .Key
113
+ }
114
+
115
+ // If a pool is provided, use it
116
+ if config .Pool != nil {
117
+ a ._pool = config .Pool
118
+ } else {
119
+ // Otherwise, create a new connection
120
+ if config .Network == "" {
121
+ return nil , errors .New ("network is required when not using a pool" )
122
+ }
123
+ if config .Address == "" {
124
+ return nil , errors .New ("address is required when not using a pool" )
125
+ }
126
+
127
+ a .network = config .Network
128
+ a .address = config .Address
129
+ a .username = config .Username
130
+ a .password = config .Password
131
+ a .tlsConfig = config .TLSConfig
132
+
133
+ // Open the DB connection
134
+ err := a .open ()
135
+ if err != nil {
136
+ return nil , err
137
+ }
138
+ }
92
139
93
140
// Call the destructor when the object is released.
94
141
runtime .SetFinalizer (a , finalizer )
95
142
96
- return a , err
143
+ return a , nil
97
144
}
98
145
99
- // NewAdapter is the constructor for Adapter.
100
- func NewAdapter (network string , address string ) (* Adapter , error ) {
101
- return newAdapter (network , address , "casbin_rules" , "" , "" )
146
+ // Legacy constructor functions (deprecated)
147
+ // These are kept for backward compatibility but should be avoided in new code
148
+
149
+ // NewAdapterBasic is the basic constructor for Adapter.
150
+ // Deprecated: Use NewAdapter with Config struct instead.
151
+ func NewAdapterBasic (network string , address string ) (* Adapter , error ) {
152
+ config := & Config {
153
+ Network : network ,
154
+ Address : address ,
155
+ }
156
+ return NewAdapter (config )
102
157
}
103
158
159
+ // NewAdapterWithUser creates adapter with user credentials.
160
+ // Deprecated: Use NewAdapter with Config struct instead.
104
161
func NewAdapterWithUser (network string , address string , username string , password string ) (* Adapter , error ) {
105
- return newAdapter (network , address , "casbin_rules" , username , password )
162
+ config := & Config {
163
+ Network : network ,
164
+ Address : address ,
165
+ Username : username ,
166
+ Password : password ,
167
+ }
168
+ return NewAdapter (config )
106
169
}
107
170
108
- // NewAdapterWithPassword is the constructor for Adapter.
171
+ // NewAdapterWithPassword creates adapter with password authentication.
172
+ // Deprecated: Use NewAdapter with Config struct instead.
109
173
func NewAdapterWithPassword (network string , address string , password string ) (* Adapter , error ) {
110
- return newAdapter (network , address , "casbin_rules" , "" , password )
174
+ config := & Config {
175
+ Network : network ,
176
+ Address : address ,
177
+ Password : password ,
178
+ }
179
+ return NewAdapter (config )
111
180
}
112
181
113
- // NewAdapterWithKey is the constructor for Adapter.
182
+ // NewAdapterWithKey creates adapter with custom key.
183
+ // Deprecated: Use NewAdapter with Config struct instead.
114
184
func NewAdapterWithKey (network string , address string , key string ) (* Adapter , error ) {
115
- return newAdapter (network , address , key , "" , "" )
185
+ config := & Config {
186
+ Network : network ,
187
+ Address : address ,
188
+ Key : key ,
189
+ }
190
+ return NewAdapter (config )
116
191
}
117
192
118
- // NewAdapterWithPool is the constructor for Adapter.
193
+ // NewAdapterWithPool creates adapter with connection pool.
194
+ // Deprecated: Use NewAdapter with Config struct instead.
119
195
func NewAdapterWithPool (pool * redis.Pool ) (* Adapter , error ) {
120
- a := & Adapter {}
121
- a .key = "casbin_rules"
122
-
123
- conn := pool .Get ()
124
- defer a .release (conn )
125
-
126
- a ._conn = conn
127
- a ._pool = pool
128
-
129
- // Call the destructor when the object is released.
130
- runtime .SetFinalizer (a , finalizer )
131
-
132
- return a , nil
196
+ config := & Config {
197
+ Pool : pool ,
198
+ }
199
+ return NewAdapter (config )
133
200
}
134
201
135
- // NewAdapterWithPoolAndOptions is the constructor for Adapter.
202
+ // NewAdapterWithPoolAndOptions creates adapter with pool and options.
203
+ // Deprecated: Use NewAdapter with Config struct instead.
136
204
func NewAdapterWithPoolAndOptions (pool * redis.Pool , options ... Option ) (* Adapter , error ) {
137
- a := & Adapter {}
138
- a .key = "casbin_rules"
205
+ config := & Config {
206
+ Pool : pool ,
207
+ }
208
+ a , err := NewAdapter (config )
209
+ if err != nil {
210
+ return nil , err
211
+ }
212
+
213
+ // Apply options for backward compatibility
139
214
for _ , option := range options {
140
215
option (a )
141
216
}
142
217
143
- conn := pool .Get ()
144
- defer a .release (conn )
145
-
146
- a ._conn = conn
147
- a ._pool = pool
148
-
149
- // Call the destructor when the object is released.
150
- runtime .SetFinalizer (a , finalizer )
151
-
152
218
return a , nil
153
219
}
154
220
155
221
type Option func (* Adapter )
156
222
223
+ // NewAdapterWithOption creates adapter with options pattern.
224
+ // Deprecated: Use NewAdapter with Config struct instead.
157
225
func NewAdapterWithOption (options ... Option ) (* Adapter , error ) {
158
226
a := & Adapter {}
159
227
for _ , option := range options {
160
228
option (a )
161
229
}
162
- // Open the DB, create it if not existed.
163
- err := a .open ()
164
230
165
- // Call the destructor when the object is released.
166
- runtime .SetFinalizer (a , finalizer )
231
+ // Convert to new config-based approach
232
+ config := & Config {
233
+ Network : a .network ,
234
+ Address : a .address ,
235
+ Key : a .key ,
236
+ Username : a .username ,
237
+ Password : a .password ,
238
+ TLSConfig : a .tlsConfig ,
239
+ }
167
240
168
- return a , err
241
+ return NewAdapter ( config )
169
242
}
170
243
171
244
func WithAddress (address string ) Option {
@@ -191,6 +264,7 @@ func WithNetwork(network string) Option {
191
264
a .network = network
192
265
}
193
266
}
267
+
194
268
func WithKey (key string ) Option {
195
269
return func (a * Adapter ) {
196
270
a .key = key
0 commit comments