1
1
package org .casbin ;
2
2
3
3
import org .casbin .jcasbin .main .Enforcer ;
4
- import org .casbin .jcasbin .util .function .CustomFunction ;
5
4
6
5
import java .io .BufferedWriter ;
7
6
import java .io .File ;
8
7
import java .io .FileWriter ;
9
8
import java .io .IOException ;
10
- import java .nio .charset .StandardCharsets ;
11
- import java .nio .file .Files ;
12
- import java .nio .file .Paths ;
13
- import java .util .ArrayList ;
14
- import java .util .List ;
15
- import java .util .regex .Matcher ;
16
9
import java .util .regex .Pattern ;
17
10
18
11
public class NewEnforcer extends Enforcer {
19
12
13
+ private static final Pattern MODEL_SECTION_PATTERN = Pattern .compile (
14
+ "\\ [request_definition\\ ].*?" +
15
+ "\\ [policy_definition\\ ].*?" +
16
+ "\\ [policy_effect\\ ].*?" +
17
+ "\\ [matchers\\ ]" ,
18
+ Pattern .DOTALL
19
+ );
20
+
21
+ private static final Pattern POLICY_LINE_PATTERN = Pattern .compile (
22
+ "^\\ s*(p|g),.*" ,
23
+ Pattern .MULTILINE
24
+ );
25
+
20
26
public NewEnforcer (String modelPath , String policyFile ) {
21
- super (parse (modelPath , ".conf" ), parse (policyFile , ".csv" ));
27
+ super (parse (modelPath , true ), parse (policyFile , false ));
22
28
}
23
29
24
- public static String parse (String string , String suffix ) {
25
- string = string .replace ("|" ,"\n " );
26
- boolean isFile = string .endsWith (suffix );
27
- if (suffix .equals (".conf" )) {
28
- if (isFile ) {
29
- try {
30
- simpleCheck (new String (Files .readAllBytes (Paths .get (string )), StandardCharsets .UTF_8 ));
31
- } catch (IOException e ) {
32
- throw new RuntimeException (e );
33
- }
34
- } else {
35
- simpleCheck (string );
30
+ public static String parse (String input , boolean isModel ) {
31
+ if (input == null || input .trim ().isEmpty ()) {
32
+ throw new IllegalArgumentException ("Input cannot be null or empty" );
33
+ }
34
+
35
+ // Check if input is an existing file
36
+ File file = new File (input );
37
+ if (file .exists () && file .isFile ()) {
38
+ return input ;
39
+ }
40
+
41
+ // If not a file, validate content format
42
+ if (isModel ) {
43
+ if (!isValidModelContent (input )) {
44
+ throw new IllegalArgumentException ("Invalid model format. Model must contain required sections: [request_definition], [policy_definition], [policy_effect], and [matchers]" );
45
+ }
46
+ } else {
47
+ if (!input .trim ().isEmpty () && !isValidPolicyContent (input )) {
48
+ throw new IllegalArgumentException ("Invalid policy format. Policy must contain lines starting with 'p,' or 'g,' or be empty" );
36
49
}
37
50
}
38
- return isFile ? string : writeToTempFile (string , suffix );
51
+
52
+ // If content is valid, write to temp file
53
+ return writeToTempFile (input );
54
+ }
55
+
56
+ private static boolean isValidModelContent (String content ) {
57
+ return MODEL_SECTION_PATTERN .matcher (content ).find ();
39
58
}
40
59
41
- public static String writeToTempFile (String str , String suffix ) {
60
+ private static boolean isValidPolicyContent (String content ) {
61
+ return content .trim ().isEmpty () || POLICY_LINE_PATTERN .matcher (content ).find ();
62
+ }
63
+
64
+ public static String writeToTempFile (String content ) {
42
65
File tempFile = null ;
43
66
try {
44
- tempFile = File .createTempFile ("default " , suffix );
67
+ tempFile = File .createTempFile ("casbin_temp_ " , "" );
45
68
tempFile .deleteOnExit ();
46
69
try (BufferedWriter writer = new BufferedWriter (new FileWriter (tempFile ))) {
47
- writer .write (str );
70
+ writer .write (content );
48
71
}
49
72
} catch (IOException e ) {
50
- e . printStackTrace ( );
73
+ throw new RuntimeException ( "Error creating temporary file" , e );
51
74
}
52
75
return tempFile .getAbsolutePath ();
53
76
}
54
-
55
- private static void simpleCheck (String fileString ) {
56
- fileString = fileString .replace (" " ,"" );
57
- String [] requiredSubstrings = {"[request_definition]" , "[policy_definition]" , "[policy_effect]" , "[matchers]" , "r=" , "p=" , "e=" , "m=" };
58
- List <String > missingSubstrings = new ArrayList <>();
59
-
60
- for (String substring : requiredSubstrings ) {
61
- Pattern pattern = Pattern .compile (Pattern .quote (substring ));
62
- Matcher matcher = pattern .matcher (fileString );
63
- if (!matcher .find ()) {
64
- missingSubstrings .add (substring );
65
- }
66
- }
67
-
68
- if (!missingSubstrings .isEmpty ()) {
69
- throw new RuntimeException ("missing required sections: " + String .join (", " , missingSubstrings ));
70
- }
71
- }
72
77
}
0 commit comments