本文整理汇总了Golang中github.com/couchbase/sync_gateway/db.PrincipalConfig类的典型用法代码示例。如果您正苦于以下问题:Golang PrincipalConfig类的具体用法?Golang PrincipalConfig怎么用?Golang PrincipalConfig使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PrincipalConfig类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: updatePrincipal
// Handles PUT and POST for a user or a role.
func (h *handler) updatePrincipal(name string, isUser bool) error {
h.assertAdminOnly()
// Unmarshal the request body into a PrincipalConfig struct:
body, _ := h.readBody()
var newInfo db.PrincipalConfig
var err error
if err = json.Unmarshal(body, &newInfo); err != nil {
return err
}
if h.rq.Method == "POST" {
// On POST, take the name from the "name" property in the request body:
if newInfo.Name == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Missing name property")
}
} else {
// ON PUT, verify the name matches, if given:
if newInfo.Name == nil {
newInfo.Name = &name
} else if *newInfo.Name != name {
return base.HTTPErrorf(http.StatusBadRequest, "Name mismatch (can't change name)")
}
}
internalName := internalUserName(*newInfo.Name)
newInfo.Name = &internalName
replaced, err := h.db.UpdatePrincipal(newInfo, isUser, h.rq.Method != "POST")
if err != nil {
return err
} else if replaced {
// on update with a new password, remove previous user sessions
if newInfo.Password != nil {
err = h.db.DeleteUserSessions(*newInfo.Name)
if err != nil {
return err
}
}
h.writeStatus(http.StatusOK, "OK")
} else {
h.writeStatus(http.StatusCreated, "Created")
}
return nil
}
示例2: marshalPrincipal
func marshalPrincipal(princ auth.Principal) ([]byte, error) {
name := externalUserName(princ.Name())
info := db.PrincipalConfig{
Name: &name,
ExplicitChannels: princ.ExplicitChannels().AsSet(),
}
if user, ok := princ.(auth.User); ok {
info.Channels = user.InheritedChannels().AsSet()
info.Email = user.Email()
info.Disabled = user.Disabled()
info.ExplicitRoleNames = user.ExplicitRoles().AllChannels()
info.RoleNames = user.RoleNames().AllChannels()
} else {
info.Channels = princ.Channels().AsSet()
}
return json.Marshal(info)
}