本文整理汇总了Golang中github.com/tsuru/tsuru/auth.Token.Permissions方法的典型用法代码示例。如果您正苦于以下问题:Golang Token.Permissions方法的具体用法?Golang Token.Permissions怎么用?Golang Token.Permissions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/tsuru/tsuru/auth.Token
的用法示例。
在下文中一共展示了Token.Permissions方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: eventList
// title: event list
// path: /events
// method: GET
// produce: application/json
// responses:
// 200: OK
// 204: No content
func eventList(w http.ResponseWriter, r *http.Request, t auth.Token) error {
r.ParseForm()
filter := &event.Filter{}
dec := form.NewDecoder(nil)
dec.IgnoreUnknownKeys(true)
dec.IgnoreCase(true)
err := dec.DecodeValues(&filter, r.Form)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse event filters: %s", err)}
}
filter.PruneUserValues()
filter.Permissions, err = t.Permissions()
if err != nil {
return err
}
events, err := event.List(filter)
if err != nil {
return err
}
if len(events) == 0 {
w.WriteHeader(http.StatusNoContent)
return nil
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(events)
}
示例2: canUseRole
func canUseRole(t auth.Token, roleName, contextValue string) error {
role, err := permission.FindRole(roleName)
if err != nil {
if err == permission.ErrRoleNotFound {
return &errors.HTTP{
Code: http.StatusNotFound,
Message: err.Error(),
}
}
return err
}
userPerms, err := t.Permissions()
if err != nil {
return err
}
perms := role.PermissionsFor(contextValue)
for _, p := range perms {
if !permission.CheckFromPermList(userPerms, p.Scheme, p.Context) {
return &errors.HTTP{
Code: http.StatusForbidden,
Message: fmt.Sprintf("User not authorized to use permission %s", p.String()),
}
}
}
return nil
}
示例3: teamList
func teamList(w http.ResponseWriter, r *http.Request, t auth.Token) error {
rec.Log(t.GetUserName(), "list-teams")
permsForTeam := permission.PermissionRegistry.PermissionsWithContextType(permission.CtxTeam)
teams, err := auth.ListTeams()
if err != nil {
return err
}
teamsMap := map[string][]string{}
perms, err := t.Permissions()
if err != nil {
return err
}
for _, team := range teams {
teamCtx := permission.Context(permission.CtxTeam, team.Name)
var parent *permission.PermissionScheme
for _, p := range permsForTeam {
if parent != nil && parent.IsParent(p) {
continue
}
if permission.CheckFromPermList(perms, p, teamCtx) {
parent = p
teamsMap[team.Name] = append(teamsMap[team.Name], p.FullName())
}
}
}
if len(teamsMap) == 0 {
w.WriteHeader(http.StatusNoContent)
return nil
}
var result []map[string]interface{}
for name, permissions := range teamsMap {
result = append(result, map[string]interface{}{
"name": name,
"permissions": permissions,
})
}
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(result)
if err != nil {
return err
}
n, err := w.Write(b)
if err != nil {
return err
}
if n != len(b) {
return &errors.HTTP{Code: http.StatusInternalServerError, Message: "Failed to write response body."}
}
return nil
}
示例4: userInfo
func userInfo(w http.ResponseWriter, r *http.Request, t auth.Token) error {
user, err := t.User()
if err != nil {
return err
}
perms, err := t.Permissions()
if err != nil {
return err
}
userData, err := createApiUser(perms, user, nil)
if err != nil {
return err
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(userData)
}
示例5: listUsers
// title: user list
// path: /users
// method: GET
// produce: application/json
// responses:
// 200: OK
// 401: Unauthorized
func listUsers(w http.ResponseWriter, r *http.Request, t auth.Token) error {
userEmail := r.URL.Query().Get("userEmail")
roleName := r.URL.Query().Get("role")
contextValue := r.URL.Query().Get("context")
users, err := auth.ListUsers()
if err != nil {
return err
}
apiUsers := make([]apiUser, 0, len(users))
roleMap := make(map[string]*permission.Role)
includeAll := permission.Check(t, permission.PermUserUpdate)
perms, err := t.Permissions()
if err != nil {
return err
}
for _, user := range users {
usrData, err := createAPIUser(perms, &user, roleMap, includeAll)
if err != nil {
return err
}
if usrData == nil {
continue
}
if userEmail == "" && roleName == "" {
apiUsers = append(apiUsers, *usrData)
}
if userEmail != "" && usrData.Email == userEmail {
apiUsers = append(apiUsers, *usrData)
}
if roleName != "" {
for _, role := range usrData.Roles {
if role.Name == roleName {
if contextValue != "" && role.ContextValue == contextValue {
apiUsers = append(apiUsers, *usrData)
break
}
if contextValue == "" {
apiUsers = append(apiUsers, *usrData)
break
}
}
}
}
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(apiUsers)
}
示例6: listUsers
func listUsers(w http.ResponseWriter, r *http.Request, t auth.Token) error {
users, err := auth.ListUsers()
if err != nil {
return err
}
apiUsers := make([]apiUser, 0, len(users))
roleMap := make(map[string]*permission.Role)
perms, err := t.Permissions()
if err != nil {
return err
}
for _, user := range users {
usrData, err := createApiUser(perms, &user, roleMap)
if err != nil {
return err
}
if usrData != nil {
apiUsers = append(apiUsers, *usrData)
}
}
return json.NewEncoder(w).Encode(apiUsers)
}
示例7: teamList
// title: team list
// path: /teams
// method: GET
// produce: application/json
// responses:
// 200: List teams
// 204: No content
// 401: Unauthorized
func teamList(w http.ResponseWriter, r *http.Request, t auth.Token) error {
permsForTeam := permission.PermissionRegistry.PermissionsWithContextType(permission.CtxTeam)
teams, err := auth.ListTeams()
if err != nil {
return err
}
teamsMap := map[string][]string{}
perms, err := t.Permissions()
if err != nil {
return err
}
for _, team := range teams {
teamCtx := permission.Context(permission.CtxTeam, team.Name)
var parent *permission.PermissionScheme
for _, p := range permsForTeam {
if parent != nil && parent.IsParent(p) {
continue
}
if permission.CheckFromPermList(perms, p, teamCtx) {
parent = p
teamsMap[team.Name] = append(teamsMap[team.Name], p.FullName())
}
}
}
if len(teamsMap) == 0 {
w.WriteHeader(http.StatusNoContent)
return nil
}
var result []map[string]interface{}
for name, permissions := range teamsMap {
result = append(result, map[string]interface{}{
"name": name,
"permissions": permissions,
})
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(result)
}