mirror of https://github.com/go-gorm/gorm.git
Add tests for controlling field permission with tag
This commit is contained in:
parent
29cd35219f
commit
a46d48ccb3
|
@ -123,17 +123,23 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := field.TagSettings["<-"]; ok {
|
if v, ok := field.TagSettings["<-"]; ok {
|
||||||
if !strings.Contains(v, "create") {
|
if v != "<-" {
|
||||||
field.Creatable = false
|
if !strings.Contains(v, "create") {
|
||||||
|
field.Creatable = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(v, "update") {
|
||||||
|
field.Updatable = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(v, "update") {
|
field.Readable = false
|
||||||
field.Updatable = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := field.TagSettings["->"]; ok {
|
if _, ok := field.TagSettings["->"]; ok {
|
||||||
field.Readable = false
|
field.Creatable = false
|
||||||
|
field.Updatable = false
|
||||||
|
field.Readable = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbName, ok := field.TagSettings["COLUMN"]; ok {
|
if dbName, ok := field.TagSettings["COLUMN"]; ok {
|
||||||
|
|
|
@ -216,3 +216,34 @@ func TestAdvancedDataTypeValuerAndSetter(t *testing.T) {
|
||||||
}
|
}
|
||||||
checkField(t, userSchema, reflectValue, newValues2)
|
checkField(t, userSchema, reflectValue, newValues2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserWithPermissionControl struct {
|
||||||
|
ID uint
|
||||||
|
Name string `gorm:"-"`
|
||||||
|
Name2 string `gorm:"->"`
|
||||||
|
Name3 string `gorm:"<-"`
|
||||||
|
Name4 string `gorm:"<-:create"`
|
||||||
|
Name5 string `gorm:"<-:update"`
|
||||||
|
Name6 string `gorm:"<-:create,update"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseFieldWithPermission(t *testing.T) {
|
||||||
|
user, err := schema.Parse(&UserWithPermissionControl{}, &sync.Map{}, schema.NamingStrategy{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to parse user with permission, got error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := []schema.Field{
|
||||||
|
{Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, Creatable: true, Updatable: true, Readable: true},
|
||||||
|
{Name: "Name", DBName: "name", BindNames: []string{"Name"}, DataType: schema.String, Tag: `gorm:"-"`, Creatable: false, Updatable: false, Readable: false},
|
||||||
|
{Name: "Name2", DBName: "name2", BindNames: []string{"Name2"}, DataType: schema.String, Tag: `gorm:"->"`, Creatable: false, Updatable: false, Readable: true},
|
||||||
|
{Name: "Name3", DBName: "name3", BindNames: []string{"Name3"}, DataType: schema.String, Tag: `gorm:"<-"`, Creatable: true, Updatable: true, Readable: false},
|
||||||
|
{Name: "Name4", DBName: "name4", BindNames: []string{"Name4"}, DataType: schema.String, Tag: `gorm:"<-:create"`, Creatable: true, Updatable: false, Readable: false},
|
||||||
|
{Name: "Name5", DBName: "name5", BindNames: []string{"Name5"}, DataType: schema.String, Tag: `gorm:"<-:update"`, Creatable: false, Updatable: true, Readable: false},
|
||||||
|
{Name: "Name6", DBName: "name6", BindNames: []string{"Name6"}, DataType: schema.String, Tag: `gorm:"<-:create,update"`, Creatable: true, Updatable: true, Readable: false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range fields {
|
||||||
|
checkSchemaField(t, user, &f, func(f *schema.Field) {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*
|
||||||
if parsedField, ok := s.FieldsByName[f.Name]; !ok {
|
if parsedField, ok := s.FieldsByName[f.Name]; !ok {
|
||||||
t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
|
t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
|
||||||
} else {
|
} else {
|
||||||
tests.AssertObjEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "DBDataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "Tag", "TagSettings")
|
tests.AssertObjEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "DBDataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "Readable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "Tag", "TagSettings")
|
||||||
|
|
||||||
if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field {
|
if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field {
|
||||||
t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
|
t.Errorf("schema %v failed to look up field with dbname %v", s, f.DBName)
|
||||||
|
|
|
@ -48,6 +48,7 @@ func checkUserSchema(t *testing.T, user *schema.Schema) {
|
||||||
checkSchemaField(t, user, &f, func(f *schema.Field) {
|
checkSchemaField(t, user, &f, func(f *schema.Field) {
|
||||||
f.Creatable = true
|
f.Creatable = true
|
||||||
f.Updatable = true
|
f.Updatable = true
|
||||||
|
f.Readable = true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,11 +84,11 @@ func checkUserSchema(t *testing.T, user *schema.Schema) {
|
||||||
JoinTable: JoinTable{Name: "UserSpeak", Table: "user_speaks", Fields: []schema.Field{
|
JoinTable: JoinTable{Name: "UserSpeak", Table: "user_speaks", Fields: []schema.Field{
|
||||||
{
|
{
|
||||||
Name: "UserID", DBName: "user_id", BindNames: []string{"UserID"}, DataType: schema.Uint,
|
Name: "UserID", DBName: "user_id", BindNames: []string{"UserID"}, DataType: schema.Uint,
|
||||||
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, PrimaryKey: true, Size: 64,
|
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, Readable: true, PrimaryKey: true, Size: 64,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "LanguageCode", DBName: "language_code", BindNames: []string{"LanguageCode"}, DataType: schema.String,
|
Name: "LanguageCode", DBName: "language_code", BindNames: []string{"LanguageCode"}, DataType: schema.String,
|
||||||
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, PrimaryKey: true,
|
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, Readable: true, PrimaryKey: true,
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
References: []Reference{{"ID", "User", "UserID", "UserSpeak", "", true}, {"Code", "Language", "LanguageCode", "UserSpeak", "", false}},
|
References: []Reference{{"ID", "User", "UserID", "UserSpeak", "", true}, {"Code", "Language", "LanguageCode", "UserSpeak", "", false}},
|
||||||
|
@ -97,11 +98,11 @@ func checkUserSchema(t *testing.T, user *schema.Schema) {
|
||||||
JoinTable: JoinTable{Name: "user_friends", Table: "user_friends", Fields: []schema.Field{
|
JoinTable: JoinTable{Name: "user_friends", Table: "user_friends", Fields: []schema.Field{
|
||||||
{
|
{
|
||||||
Name: "UserID", DBName: "user_id", BindNames: []string{"UserID"}, DataType: schema.Uint,
|
Name: "UserID", DBName: "user_id", BindNames: []string{"UserID"}, DataType: schema.Uint,
|
||||||
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, PrimaryKey: true, Size: 64,
|
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, Readable: true, PrimaryKey: true, Size: 64,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "FriendID", DBName: "friend_id", BindNames: []string{"FriendID"}, DataType: schema.Uint,
|
Name: "FriendID", DBName: "friend_id", BindNames: []string{"FriendID"}, DataType: schema.Uint,
|
||||||
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, PrimaryKey: true, Size: 64,
|
Tag: `gorm:"primarykey"`, Creatable: true, Updatable: true, Readable: true, PrimaryKey: true, Size: 64,
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
References: []Reference{{"ID", "User", "UserID", "user_friends", "", true}, {"ID", "User", "FriendID", "user_friends", "", false}},
|
References: []Reference{{"ID", "User", "UserID", "user_friends", "", true}, {"ID", "User", "FriendID", "user_friends", "", false}},
|
||||||
|
@ -137,6 +138,7 @@ func TestParseSchemaWithAdvancedDataType(t *testing.T) {
|
||||||
checkSchemaField(t, user, &f, func(f *schema.Field) {
|
checkSchemaField(t, user, &f, func(f *schema.Field) {
|
||||||
f.Creatable = true
|
f.Creatable = true
|
||||||
f.Updatable = true
|
f.Updatable = true
|
||||||
|
f.Readable = true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue