• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

casbin / xorm-adapter / 8609606726

09 Apr 2024 02:46AM UTC coverage: 71.715% (-1.7%) from 73.461%
8609606726

push

github

web-flow
feat: pass context down to xorm (#65)

* feat: pass context down to xorm

* fix: update README

20 of 20 new or added lines in 1 file covered. (100.0%)

4 existing lines in 1 file now uncovered.

322 of 449 relevant lines covered (71.71%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.71
/adapter.go
1
// Copyright 2017 The casbin Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package xormadapter
16

17
import (
18
        "context"
19
        "errors"
20
        "log"
21
        "runtime"
22
        "strings"
23

24
        "github.com/casbin/casbin/v2/model"
25
        "github.com/casbin/casbin/v2/persist"
26
        "github.com/lib/pq"
27
        "xorm.io/xorm"
28
)
29

30
// TableName  if tableName=="" , adapter will use default tablename "casbin_rule".
31
func (the *CasbinRule) TableName() string {
1✔
32
        if len(the.tableName) == 0 {
2✔
33
                return "casbin_rule"
1✔
34
        }
1✔
35
        return the.tableName
1✔
36
}
37

38
// CasbinRule  .
39
type CasbinRule struct {
40
        Ptype string `xorm:"varchar(100) index not null default ''"`
41
        V0    string `xorm:"varchar(100) index not null default ''"`
42
        V1    string `xorm:"varchar(100) index not null default ''"`
43
        V2    string `xorm:"varchar(100) index not null default ''"`
44
        V3    string `xorm:"varchar(100) index not null default ''"`
45
        V4    string `xorm:"varchar(100) index not null default ''"`
46
        V5    string `xorm:"varchar(100) index not null default ''"`
47

48
        tableName string `xorm:"-"`
49
}
50

51
// Adapter represents the Xorm adapter for policy storage.
52
type Adapter struct {
53
        driverName     string
54
        dataSourceName string
55
        dbSpecified    bool
56
        isFiltered     bool
57
        engine         *xorm.Engine
58
        tablePrefix    string
59
        tableName      string
60
}
61

62
// Filter  .
63
type Filter struct {
64
        Ptype []string
65
        V0    []string
66
        V1    []string
67
        V2    []string
68
        V3    []string
69
        V4    []string
70
        V5    []string
71
}
72

73
// finalizer is the destructor for Adapter.
74
func finalizer(a *Adapter) {
1✔
75
        if a.engine == nil {
1✔
76
                return
×
77
        }
×
78

79
        err := a.engine.Close()
1✔
80
        if err != nil {
1✔
81
                log.Printf("close xorm adapter engine failed, err: %v", err)
×
82
        }
×
83
}
84

85
// NewAdapter is the constructor for Adapter.
86
// dbSpecified is an optional bool parameter. The default value is false.
87
// It's up to whether you have specified an existing DB in dataSourceName.
88
// If dbSpecified == true, you need to make sure the DB in dataSourceName exists.
89
// If dbSpecified == false, the adapter will automatically create a DB named "casbin".
90
func NewAdapter(driverName string, dataSourceName string, dbSpecified ...bool) (*Adapter, error) {
1✔
91
        a := &Adapter{
1✔
92
                driverName:     driverName,
1✔
93
                dataSourceName: dataSourceName,
1✔
94
        }
1✔
95

1✔
96
        if len(dbSpecified) == 0 {
2✔
97
                a.dbSpecified = false
1✔
98
        } else if len(dbSpecified) == 1 {
1✔
99
                a.dbSpecified = dbSpecified[0]
×
100
        } else {
×
101
                return nil, errors.New("invalid parameter: dbSpecified")
×
102
        }
×
103

104
        // Open the DB, create it if not existed.
105
        err := a.open()
1✔
106
        if err != nil {
1✔
107
                return nil, err
×
108
        }
×
109

110
        // Call the destructor when the object is released.
111
        runtime.SetFinalizer(a, finalizer)
1✔
112

1✔
113
        return a, nil
1✔
114
}
115

116
// NewAdapterWithTableName  .
117
func NewAdapterWithTableName(driverName string, dataSourceName string, tableName string, tablePrefix string, dbSpecified ...bool) (*Adapter, error) {
1✔
118
        a := &Adapter{
1✔
119
                driverName:     driverName,
1✔
120
                dataSourceName: dataSourceName,
1✔
121
                tableName:      tableName,
1✔
122
                tablePrefix:    tablePrefix,
1✔
123
        }
1✔
124

1✔
125
        if len(dbSpecified) == 0 {
2✔
126
                a.dbSpecified = false
1✔
127
        } else if len(dbSpecified) == 1 {
1✔
128
                a.dbSpecified = dbSpecified[0]
×
129
        } else {
×
130
                return nil, errors.New("invalid parameter: dbSpecified")
×
131
        }
×
132

133
        // Open the DB, create it if not existed.
134
        err := a.open()
1✔
135
        if err != nil {
1✔
136
                return nil, err
×
137
        }
×
138

139
        // Call the destructor when the object is released.
140
        runtime.SetFinalizer(a, finalizer)
1✔
141

1✔
142
        return a, nil
1✔
143
}
144

145
// NewAdapterByEngine  .
146
func NewAdapterByEngine(engine *xorm.Engine) (*Adapter, error) {
×
147
        a := &Adapter{
×
148
                engine: engine,
×
149
        }
×
150

×
151
        err := a.createTable()
×
152
        if err != nil {
×
153
                return nil, err
×
154
        }
×
155

156
        return a, nil
×
157
}
158

159
// NewAdapterByEngineWithTableName  .
160
func NewAdapterByEngineWithTableName(engine *xorm.Engine, tableName string, tablePrefix string) (*Adapter, error) {
×
161
        a := &Adapter{
×
162
                engine:      engine,
×
163
                tableName:   tableName,
×
164
                tablePrefix: tablePrefix,
×
165
        }
×
166

×
167
        err := a.createTable()
×
168
        if err != nil {
×
169
                return nil, err
×
170
        }
×
171

172
        return a, nil
×
173
}
174

175
func (a *Adapter) getFullTableName() string {
1✔
176
        if a.tablePrefix != "" {
2✔
177
                return a.tablePrefix + a.tableName
1✔
178
        }
1✔
179
        return a.tableName
1✔
180
}
181

182
func (a *Adapter) createDatabase() error {
1✔
183
        var err error
1✔
184
        var engine *xorm.Engine
1✔
185
        if a.driverName == "postgres" {
2✔
186
                engine, err = xorm.NewEngine(a.driverName, a.dataSourceName+" dbname=postgres")
1✔
187
        } else {
2✔
188
                engine, err = xorm.NewEngine(a.driverName, a.dataSourceName)
1✔
189
        }
1✔
190
        if err != nil {
1✔
191
                return err
×
192
        }
×
193

194
        if a.driverName == "postgres" {
2✔
195
                if _, err = engine.Exec("CREATE DATABASE casbin"); err != nil {
1✔
196
                        // 42P04 is        duplicate_database
×
197
                        if pqerr, ok := err.(*pq.Error); ok && pqerr.Code == "42P04" {
×
198
                                _ = engine.Close()
×
199
                                return nil
×
200
                        }
×
201
                }
202
        } else if a.driverName != "sqlite3" {
2✔
203
                _, err = engine.Exec("CREATE DATABASE IF NOT EXISTS casbin")
1✔
204
        }
1✔
205
        if err != nil {
1✔
206
                _ = engine.Close()
×
207
                return err
×
208
        }
×
209

210
        return engine.Close()
1✔
211
}
212

213
func (a *Adapter) open() error {
1✔
214
        var err error
1✔
215
        var engine *xorm.Engine
1✔
216

1✔
217
        if a.dbSpecified {
1✔
218
                engine, err = xorm.NewEngine(a.driverName, a.dataSourceName)
×
219
                if err != nil {
×
220
                        return err
×
221
                }
×
222
        } else {
1✔
223
                if err = a.createDatabase(); err != nil {
1✔
224
                        return err
×
225
                }
×
226

227
                if a.driverName == "postgres" {
2✔
228
                        engine, err = xorm.NewEngine(a.driverName, a.dataSourceName+" dbname=casbin")
1✔
229
                } else if a.driverName == "sqlite3" {
2✔
230
                        engine, err = xorm.NewEngine(a.driverName, a.dataSourceName)
×
231
                } else {
1✔
232
                        engine, err = xorm.NewEngine(a.driverName, a.dataSourceName+"casbin")
1✔
233
                }
1✔
234
                if err != nil {
1✔
235
                        return err
×
236
                }
×
237
        }
238

239
        a.engine = engine
1✔
240

1✔
241
        return a.createTable()
1✔
242
}
243

244
func (a *Adapter) createTable() error {
1✔
245
        return a.engine.Sync2(&CasbinRule{tableName: a.getFullTableName()})
1✔
246
}
1✔
247

248
func (a *Adapter) dropTable() error {
1✔
249
        return a.engine.DropTables(&CasbinRule{tableName: a.getFullTableName()})
1✔
250
}
1✔
251

252
func loadPolicyLine(line *CasbinRule, model model.Model) {
1✔
253
        var p = []string{line.Ptype,
1✔
254
                line.V0, line.V1, line.V2, line.V3, line.V4, line.V5}
1✔
255
        var lineText string
1✔
256
        if line.V5 != "" {
1✔
257
                lineText = strings.Join(p, ", ")
×
258
        } else if line.V4 != "" {
1✔
259
                lineText = strings.Join(p[:6], ", ")
×
260
        } else if line.V3 != "" {
1✔
261
                lineText = strings.Join(p[:5], ", ")
×
262
        } else if line.V2 != "" {
2✔
263
                lineText = strings.Join(p[:4], ", ")
1✔
264
        } else if line.V1 != "" {
3✔
265
                lineText = strings.Join(p[:3], ", ")
1✔
266
        } else if line.V0 != "" {
1✔
267
                lineText = strings.Join(p[:2], ", ")
×
268
        }
×
269

270
        persist.LoadPolicyLine(lineText, model)
1✔
271
}
272

273
// LoadPolicy loads policy from database.
274
func (a *Adapter) LoadPolicy(model model.Model) error {
1✔
275
        return a.LoadPolicyCtx(context.Background(), model)
1✔
276
}
1✔
277

278
// LoadPolicyCtx loads policy from database.
279
func (a *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error {
1✔
280
        lines := make([]*CasbinRule, 0, 64)
1✔
281

1✔
282
        if err := a.engine.Context(ctx).Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
1✔
283
                return err
×
284
        }
×
285

286
        for _, line := range lines {
2✔
287
                loadPolicyLine(line, model)
1✔
288
        }
1✔
289

290
        return nil
1✔
291
}
292

293
func (a *Adapter) genPolicyLine(ptype string, rule []string) *CasbinRule {
1✔
294
        line := CasbinRule{Ptype: ptype, tableName: a.getFullTableName()}
1✔
295

1✔
296
        l := len(rule)
1✔
297
        if l > 0 {
2✔
298
                line.V0 = rule[0]
1✔
299
        }
1✔
300
        if l > 1 {
2✔
301
                line.V1 = rule[1]
1✔
302
        }
1✔
303
        if l > 2 {
2✔
304
                line.V2 = rule[2]
1✔
305
        }
1✔
306
        if l > 3 {
1✔
307
                line.V3 = rule[3]
×
308
        }
×
309
        if l > 4 {
1✔
310
                line.V4 = rule[4]
×
311
        }
×
312
        if l > 5 {
1✔
313
                line.V5 = rule[5]
×
314
        }
×
315

316
        return &line
1✔
317
}
318

319
// SavePolicy saves policy to database.
320
func (a *Adapter) SavePolicy(model model.Model) error {
1✔
321
        return a.SavePolicyCtx(context.Background(), model)
1✔
322
}
1✔
323

324
// SavePolicyCtx saves policy to database.
325
func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
1✔
326
        err := a.dropTable()
1✔
327
        if err != nil {
1✔
328
                return err
×
329
        }
×
330
        err = a.createTable()
1✔
331
        if err != nil {
1✔
332
                return err
×
333
        }
×
334

335
        lines := make([]*CasbinRule, 0, 64)
1✔
336

1✔
337
        for ptype, ast := range model["p"] {
2✔
338
                for _, rule := range ast.Policy {
2✔
339
                        line := a.genPolicyLine(ptype, rule)
1✔
340
                        lines = append(lines, line)
1✔
341
                }
1✔
342
        }
343

344
        for ptype, ast := range model["g"] {
2✔
345
                for _, rule := range ast.Policy {
2✔
346
                        line := a.genPolicyLine(ptype, rule)
1✔
347
                        lines = append(lines, line)
1✔
348
                }
1✔
349
        }
350

351
        // check whether the policy is empty
352
        if len(lines) == 0 {
1✔
UNCOV
353
                return nil
×
UNCOV
354
        }
×
355

356
        _, err = a.engine.Context(ctx).Insert(&lines)
1✔
357

1✔
358
        return err
1✔
359
}
360

361
// AddPolicy adds a policy rule to the storage.
362
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
1✔
363
        return a.AddPolicyCtx(context.Background(), sec, ptype, rule)
1✔
364
}
1✔
365

366
// AddPolicyCtx adds a policy rule to the storage.
367
func (a *Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
1✔
368
        line := a.genPolicyLine(ptype, rule)
1✔
369
        _, err := a.engine.Context(ctx).InsertOne(line)
1✔
370
        return err
1✔
371
}
1✔
372

373
// AddPolicies adds multiple policy rule to the storage.
374
func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
1✔
375
        _, err := a.engine.Transaction(func(tx *xorm.Session) (interface{}, error) {
2✔
376
                for _, rule := range rules {
2✔
377
                        line := a.genPolicyLine(ptype, rule)
1✔
378
                        _, err := tx.InsertOne(line)
1✔
379
                        if err != nil {
1✔
380
                                return nil, err
×
381
                        }
×
382
                }
383
                return nil, nil
1✔
384
        })
385
        return err
1✔
386
}
387

388
// RemovePolicy removes a policy rule from the storage.
389
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
1✔
390
        return a.RemovePolicyCtx(context.Background(), sec, ptype, rule)
1✔
391
}
1✔
392

393
// RemovePolicyCtx removes a policy rule from the storage.
394
func (a *Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
1✔
395
        line := a.genPolicyLine(ptype, rule)
1✔
396
        _, err := a.engine.Context(ctx).Delete(line)
1✔
397
        return err
1✔
398
}
1✔
399

400
// RemovePolicies removes multiple policy rule from the storage.
401
func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error {
1✔
402
        _, err := a.engine.Transaction(func(tx *xorm.Session) (interface{}, error) {
2✔
403
                for _, rule := range rules {
2✔
404
                        line := a.genPolicyLine(ptype, rule)
1✔
405
                        _, err := tx.Delete(line)
1✔
406
                        if err != nil {
1✔
407
                                return nil, nil
×
408
                        }
×
409
                }
410
                return nil, nil
1✔
411
        })
412
        return err
1✔
413
}
414

415
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
416
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
1✔
417
        return a.RemoveFilteredPolicyCtx(context.Background(), sec, ptype, fieldIndex, fieldValues...)
1✔
418
}
1✔
419

420
// RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage.
421
func (a *Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error {
1✔
422
        line := CasbinRule{Ptype: ptype, tableName: a.getFullTableName()}
1✔
423

1✔
424
        idx := fieldIndex + len(fieldValues)
1✔
425
        if fieldIndex <= 0 && idx > 0 {
2✔
426
                line.V0 = fieldValues[0-fieldIndex]
1✔
427
        }
1✔
428
        if fieldIndex <= 1 && idx > 1 {
1✔
UNCOV
429
                line.V1 = fieldValues[1-fieldIndex]
×
UNCOV
430
        }
×
431
        if fieldIndex <= 2 && idx > 2 {
1✔
432
                line.V2 = fieldValues[2-fieldIndex]
×
433
        }
×
434
        if fieldIndex <= 3 && idx > 3 {
1✔
435
                line.V3 = fieldValues[3-fieldIndex]
×
436
        }
×
437
        if fieldIndex <= 4 && idx > 4 {
1✔
438
                line.V4 = fieldValues[4-fieldIndex]
×
439
        }
×
440
        if fieldIndex <= 5 && idx > 5 {
1✔
441
                line.V5 = fieldValues[5-fieldIndex]
×
442
        }
×
443

444
        _, err := a.engine.Context(ctx).Delete(&line)
1✔
445
        return err
1✔
446
}
447

448
// LoadFilteredPolicy loads only policy rules that match the filter.
449
func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error {
1✔
450
        filterValue, ok := filter.(Filter)
1✔
451
        if !ok {
1✔
452
                return errors.New("invalid filter type")
×
453
        }
×
454

455
        lines := make([]*CasbinRule, 0, 64)
1✔
456
        if err := a.filterQuery(a.engine.NewSession(), filterValue).Table(&CasbinRule{tableName: a.getFullTableName()}).Find(&lines); err != nil {
1✔
457
                return err
×
458
        }
×
459

460
        for _, line := range lines {
2✔
461
                loadPolicyLine(line, model)
1✔
462
        }
1✔
463
        a.isFiltered = true
1✔
464
        return nil
1✔
465
}
466

467
// IsFiltered returns true if the loaded policy has been filtered.
468
func (a *Adapter) IsFiltered() bool {
1✔
469
        return a.isFiltered
1✔
470
}
1✔
471

472
func (a *Adapter) filterQuery(session *xorm.Session, filter Filter) *xorm.Session {
1✔
473
        filterValue := [7]struct {
1✔
474
                col string
1✔
475
                val []string
1✔
476
        }{
1✔
477
                {"ptype", filter.Ptype},
1✔
478
                {"v0", filter.V0},
1✔
479
                {"v1", filter.V1},
1✔
480
                {"v2", filter.V2},
1✔
481
                {"v3", filter.V3},
1✔
482
                {"v4", filter.V4},
1✔
483
                {"v5", filter.V5},
1✔
484
        }
1✔
485

1✔
486
        for idx := range filterValue {
2✔
487
                switch len(filterValue[idx].val) {
1✔
488
                case 0:
1✔
489
                        continue
1✔
490
                case 1:
1✔
491
                        session.And(filterValue[idx].col+" = ?", filterValue[idx].val[0])
1✔
492
                default:
1✔
493
                        session.In(filterValue[idx].col, filterValue[idx].val)
1✔
494
                }
495
        }
496

497
        return session
1✔
498
}
499

500
// UpdatePolicy update oldRule to newPolicy permanently
501
func (a *Adapter) UpdatePolicy(sec string, ptype string, oldRule, newPolicy []string) error {
1✔
502
        oRule := a.genPolicyLine(ptype, oldRule)
1✔
503
        _, err := a.engine.Update(a.genPolicyLine(ptype, newPolicy), oRule)
1✔
504
        return err
1✔
505
}
1✔
506

507
// UpdatePolicies updates some policy rules to storage, like db, redis.
508
func (a *Adapter) UpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error {
1✔
509
        session := a.engine.NewSession()
1✔
510
        defer session.Close()
1✔
511

1✔
512
        if err := session.Begin(); err != nil {
1✔
513
                return err
×
514
        }
×
515

516
        for i, oldRule := range oldRules {
2✔
517
                nRule, oRule := a.genPolicyLine(ptype, newRules[i]), a.genPolicyLine(ptype, oldRule)
1✔
518
                if _, err := session.Update(nRule, oRule); err != nil {
1✔
519
                        return err
×
520
                }
×
521
        }
522

523
        return session.Commit()
1✔
524
}
525

526
func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) ([][]string, error) {
1✔
527
        // UpdateFilteredPolicies deletes old rules and adds new rules.
1✔
528
        line := &CasbinRule{}
1✔
529

1✔
530
        line.Ptype = ptype
1✔
531
        if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
2✔
532
                line.V0 = fieldValues[0-fieldIndex]
1✔
533
        }
1✔
534
        if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
2✔
535
                line.V1 = fieldValues[1-fieldIndex]
1✔
536
        }
1✔
537
        if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
2✔
538
                line.V2 = fieldValues[2-fieldIndex]
1✔
539
        }
1✔
540
        if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
1✔
541
                line.V3 = fieldValues[3-fieldIndex]
×
542
        }
×
543
        if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
1✔
544
                line.V4 = fieldValues[4-fieldIndex]
×
545
        }
×
546
        if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
1✔
547
                line.V5 = fieldValues[5-fieldIndex]
×
548
        }
×
549

550
        newP := make([]CasbinRule, 0, len(newPolicies))
1✔
551
        oldP := make([]CasbinRule, 0)
1✔
552
        for _, newRule := range newPolicies {
2✔
553
                newP = append(newP, *a.genPolicyLine(ptype, newRule))
1✔
554
        }
1✔
555
        tx := a.engine.NewSession().Table(&CasbinRule{tableName: a.getFullTableName()})
1✔
556
        defer tx.Close()
1✔
557

1✔
558
        if err := tx.Begin(); err != nil {
1✔
559
                return nil, err
×
560
        }
×
561

562
        for i := range newP {
2✔
563
                str, args := line.queryString()
1✔
564
                if err := tx.Where(str, args...).Find(&oldP); err != nil {
1✔
565
                        return nil, tx.Rollback()
×
566
                }
×
567
                if _, err := tx.Where(str.(string), args...).Delete(&CasbinRule{tableName: a.getFullTableName()}); err != nil {
1✔
568
                        return nil, tx.Rollback()
×
569
                }
×
570
                if _, err := tx.Insert(&newP[i]); err != nil {
1✔
571
                        return nil, tx.Rollback()
×
572
                }
×
573
        }
574

575
        // return deleted rulues
576
        oldPolicies := make([][]string, 0)
1✔
577
        for _, v := range oldP {
2✔
578
                oldPolicy := v.toStringPolicy()
1✔
579
                oldPolicies = append(oldPolicies, oldPolicy)
1✔
580
        }
1✔
581
        return oldPolicies, tx.Commit()
1✔
582
}
583

584
func (c *CasbinRule) toStringPolicy() []string {
1✔
585
        policy := make([]string, 0)
1✔
586
        if c.Ptype != "" {
2✔
587
                policy = append(policy, c.Ptype)
1✔
588
        }
1✔
589
        if c.V0 != "" {
2✔
590
                policy = append(policy, c.V0)
1✔
591
        }
1✔
592
        if c.V1 != "" {
2✔
593
                policy = append(policy, c.V1)
1✔
594
        }
1✔
595
        if c.V2 != "" {
2✔
596
                policy = append(policy, c.V2)
1✔
597
        }
1✔
598
        if c.V3 != "" {
1✔
599
                policy = append(policy, c.V3)
×
600
        }
×
601
        if c.V4 != "" {
1✔
602
                policy = append(policy, c.V4)
×
603
        }
×
604
        if c.V5 != "" {
1✔
605
                policy = append(policy, c.V5)
×
606
        }
×
607
        return policy
1✔
608
}
609

610
func (c *CasbinRule) queryString() (interface{}, []interface{}) {
1✔
611
        queryArgs := []interface{}{c.Ptype}
1✔
612

1✔
613
        queryStr := "ptype = ?"
1✔
614
        if c.V0 != "" {
2✔
615
                queryStr += " and v0 = ?"
1✔
616
                queryArgs = append(queryArgs, c.V0)
1✔
617
        }
1✔
618
        if c.V1 != "" {
2✔
619
                queryStr += " and v1 = ?"
1✔
620
                queryArgs = append(queryArgs, c.V1)
1✔
621
        }
1✔
622
        if c.V2 != "" {
2✔
623
                queryStr += " and v2 = ?"
1✔
624
                queryArgs = append(queryArgs, c.V2)
1✔
625
        }
1✔
626
        if c.V3 != "" {
1✔
627
                queryStr += " and v3 = ?"
×
628
                queryArgs = append(queryArgs, c.V3)
×
629
        }
×
630
        if c.V4 != "" {
1✔
631
                queryStr += " and v4 = ?"
×
632
                queryArgs = append(queryArgs, c.V4)
×
633
        }
×
634
        if c.V5 != "" {
1✔
635
                queryStr += " and v5 = ?"
×
636
                queryArgs = append(queryArgs, c.V5)
×
637
        }
×
638

639
        return queryStr, queryArgs
1✔
640
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc