本文整理汇总了Golang中github.com/cloudwan/gohan/server/middleware.IdentityService.GetTenantName方法的典型用法代码示例。如果您正苦于以下问题:Golang IdentityService.GetTenantName方法的具体用法?Golang IdentityService.GetTenantName怎么用?Golang IdentityService.GetTenantName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/server/middleware.IdentityService
的用法示例。
在下文中一共展示了IdentityService.GetTenantName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UpdateResource
// UpdateResource updates the resource specified by the schema and ID using the dataMap
func UpdateResource(
context middleware.Context,
dataStore db.DB, identityService middleware.IdentityService,
resourceSchema *schema.Schema,
resourceID string, dataMap map[string]interface{},
) error {
context["id"] = resourceID
//load environment
environmentManager := extension.GetManager()
environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
if !ok {
return fmt.Errorf("No environment for schema")
}
auth := context["auth"].(schema.Authorization)
//load policy
policy, err := loadPolicy(context, "update", strings.Replace(resourceSchema.GetSingleURL(), ":id", resourceID, 1), auth)
if err != nil {
return err
}
context["policy"] = policy
//fillup default values
if tenantID, ok := dataMap["tenant_id"]; ok && tenantID != nil {
dataMap["tenant_name"], err = identityService.GetTenantName(tenantID.(string))
}
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
//check policy
err = policy.Check(schema.ActionUpdate, auth, dataMap)
delete(dataMap, "tenant_name")
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
context["resource"] = dataMap
if err := extension.HandleEvent(context, environment, "pre_update"); err != nil {
return err
}
if resourceData, ok := context["resource"].(map[string]interface{}); ok {
dataMap = resourceData
}
if err := InTransaction(
context, dataStore,
transaction.GetIsolationLevel(resourceSchema, schema.ActionUpdate),
func() error {
return UpdateResourceInTransaction(context, resourceSchema, resourceID, dataMap, policy.GetTenantIDFilter(schema.ActionUpdate, auth.TenantID()))
},
); err != nil {
return err
}
if err := extension.HandleEvent(context, environment, "post_update"); err != nil {
return err
}
if err := ApplyPolicyForResource(context, resourceSchema); err != nil {
return ResourceError{err, "", NotFound}
}
return nil
}
示例2: CreateResource
// CreateResource creates the resource specified by the schema and dataMap
func CreateResource(
context middleware.Context,
dataStore db.DB,
identityService middleware.IdentityService,
resourceSchema *schema.Schema,
dataMap map[string]interface{},
) error {
manager := schema.GetManager()
// Load environment
environmentManager := extension.GetManager()
environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
if !ok {
return fmt.Errorf("No environment for schema")
}
auth := context["auth"].(schema.Authorization)
//LoadPolicy
policy, err := loadPolicy(context, "create", resourceSchema.GetPluralURL(), auth)
if err != nil {
return err
}
_, err = resourceSchema.GetPropertyByID("tenant_id")
if _, ok := dataMap["tenant_id"]; err == nil && !ok {
dataMap["tenant_id"] = context["tenant_id"]
}
if tenantID, ok := dataMap["tenant_id"]; ok && tenantID != nil {
dataMap["tenant_name"], err = identityService.GetTenantName(tenantID.(string))
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
}
//Apply policy for api input
err = policy.Check(schema.ActionCreate, auth, dataMap)
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
delete(dataMap, "tenant_name")
// apply property filter
err = policy.ApplyPropertyConditionFilter(schema.ActionCreate, dataMap, nil)
if err != nil {
return ResourceError{err, err.Error(), Unauthorized}
}
context["resource"] = dataMap
if id, ok := dataMap["id"]; !ok || id == "" {
dataMap["id"] = uuid.NewV4().String()
}
context["id"] = dataMap["id"]
if err := extension.HandleEvent(context, environment, "pre_create"); err != nil {
return err
}
if resourceData, ok := context["resource"].(map[string]interface{}); ok {
dataMap = resourceData
}
//Validation
err = resourceSchema.ValidateOnCreate(dataMap)
if err != nil {
return ResourceError{err, fmt.Sprintf("Validation error: %s", err), WrongData}
}
resource, err := manager.LoadResource(resourceSchema.ID, dataMap)
if err != nil {
return err
}
//Fillup default
err = resource.PopulateDefaults()
if err != nil {
return err
}
context["resource"] = resource.Data()
if err := InTransaction(
context, dataStore,
transaction.GetIsolationLevel(resourceSchema, schema.ActionCreate),
func() error {
return CreateResourceInTransaction(context, resource)
},
); err != nil {
return err
}
if err := extension.HandleEvent(context, environment, "post_create"); err != nil {
return err
}
if err := ApplyPolicyForResource(context, resourceSchema); err != nil {
return ResourceError{err, "", Unauthorized}
}
return nil
}