-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use role template strings for predefined roles
Signed-off-by: Khor Shu Heng <khor.heng@gojek.com>
- Loading branch information
1 parent
776ba32
commit 5dc98ed
Showing
4 changed files
with
164 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,48 @@ | ||
package enforcer | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
"github.com/caraml-dev/mlp/api/models" | ||
) | ||
|
||
const ( | ||
MLPAdminRole = "mlp.administrator" | ||
ProjectReaderRole = "mlp.projects.reader" | ||
MLPAdminRole = "mlp.administrator" | ||
MLPProjectsReaderRole = "mlp.projects.reader" | ||
MLPProjectReaderRole = "mlp.projects.{{ .ProjectId }}.reader" | ||
MLPProjectAdminRole = "mlp.projects.{{ .ProjectId }}.administrator" | ||
) | ||
|
||
func ParseRole(role string, templateContext map[string]string) (string, error) { | ||
roleParser, err := template.New("role").Parse(role) | ||
if err != nil { | ||
return "", err | ||
} | ||
var parseResultBytes bytes.Buffer | ||
err = roleParser.Execute(&parseResultBytes, templateContext) | ||
if err != nil { | ||
return "", err | ||
} | ||
return parseResultBytes.String(), nil | ||
} | ||
|
||
func ParseProjectRole(roleTemplateString string, project *models.Project) (string, error) { | ||
parsedRole, err := ParseRole(roleTemplateString, map[string]string{"ProjectId": project.ID.String()}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return parsedRole, nil | ||
} | ||
|
||
func ParseProjectRoles(roleTemplateStrings []string, project *models.Project) ([]string, error) { | ||
roles := make([]string, len(roleTemplateStrings)) | ||
for i, roleTemplateString := range roleTemplateStrings { | ||
parsedRole, err := ParseProjectRole(roleTemplateString, project) | ||
roles[i] = parsedRole | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return roles, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package enforcer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/caraml-dev/mlp/api/models" | ||
) | ||
|
||
func TestParseRole(t *testing.T) { | ||
type args struct { | ||
role string | ||
templateContext map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
"parse role with project id", | ||
args{ | ||
role: MLPProjectReaderRole, | ||
templateContext: map[string]string{"ProjectId": "1"}, | ||
}, | ||
"mlp.projects.1.reader", | ||
false, | ||
}, | ||
{ | ||
"parse plain string role without template context", | ||
args{ | ||
role: MLPAdminRole, | ||
templateContext: nil, | ||
}, | ||
"mlp.administrator", | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ParseRole(tt.args.role, tt.args.templateContext) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ParseRole() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("ParseRole() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseProjectRole(t *testing.T) { | ||
type args struct { | ||
role string | ||
project *models.Project | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
"parse role with project id", | ||
args{ | ||
role: MLPProjectReaderRole, | ||
project: &models.Project{ | ||
ID: 1, | ||
}, | ||
}, | ||
"mlp.projects.1.reader", | ||
false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ParseProjectRole(tt.args.role, tt.args.project) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ParseProjectRole() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("ParseProjectRole() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters