本文整理汇总了Golang中github.com/itsyouonline/identityserver/db/organization.NewManager函数的典型用法代码示例。如果您正苦于以下问题:Golang NewManager函数的具体用法?Golang NewManager怎么用?Golang NewManager使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewManager函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Get
// Get the list organizations a user is owner or member of
// It is handler for GET /users/{username}/organizations
func (api UsersusernameorganizationsAPI) Get(w http.ResponseWriter, r *http.Request) {
username := mux.Vars(r)["username"]
orgMgr := organizationdb.NewManager(r)
orgs, err := orgMgr.AllByUser(username)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
type UserOrganizations struct {
Member []string `json:"member"`
Owner []string `json:"owner"`
}
userOrgs := UserOrganizations{
Member: []string{},
Owner: []string{},
}
for _, org := range orgs {
if exists(username, org.Owners) {
userOrgs.Owner = append(userOrgs.Owner, org.Globalid)
} else {
userOrgs.Member = append(userOrgs.Member, org.Globalid)
}
}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(&userOrgs)
}
示例2: Set2faValidityTime
// Set2faValidityTime is the handler for PUT /organizations/globalid/2fa/validity
// Sets the 2fa validity time for the organization, in days
func (api OrganizationsAPI) Set2faValidityTime(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
body := struct {
SecondsValidity int `json:"secondsvalidity"`
}{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
log.Error("Error while setting 2FA validity time: ", err.Error())
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
mgr := organization.NewManager(r)
seconds := body.SecondsValidity
if seconds < 0 {
seconds = 0
} else if seconds > 2678400 {
seconds = 2678400
}
err := mgr.SetValidity(globalid, seconds)
if err != nil {
log.Error("Error while setting 2FA validity time: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
示例3: DeleteDns
func (api OrganizationsAPI) DeleteDns(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
dnsName := mux.Vars(r)["dnsname"]
orgMgr := organization.NewManager(r)
organization, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "getting organization", err)
}
return
}
sort.Strings(organization.DNS)
if sort.SearchStrings(organization.DNS, dnsName) == len(organization.DNS) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
err = orgMgr.RemoveDNS(organization, dnsName)
if err != nil {
log.Error("Error removing DNS name", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
}
示例4: Get2faValidityTime
// Get2faValidityTime is the handler for GET /organizations/globalid/2fa/validity
// Get the 2fa validity time for the organization, in seconds
func (api OrganizationsAPI) Get2faValidityTime(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
mgr := organization.NewManager(r)
validity, err := mgr.GetValidity(globalid)
if err != nil && err != mgo.ErrNotFound {
log.Error("Error while getting validity duration: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err == mgo.ErrNotFound {
log.Error("Error while getting validity duration: organization nout found")
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
response := struct {
SecondsValidity int `json:"secondsvalidity"`
}{
SecondsValidity: validity,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
示例5: globalidrolesrolePost
// Accept membership in organization
// It is handler for POST /users/{username}/organizations/{globalid}/roles/{role}
func (api UsersusernameorganizationsAPI) globalidrolesrolePost(w http.ResponseWriter, r *http.Request) {
username := mux.Vars(r)["username"]
role := mux.Vars(r)["role"]
organization := mux.Vars(r)["globalid"]
var j invitations.JoinOrganizationInvitation
if err := json.NewDecoder(r.Body).Decode(&j); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
orgReqMgr := invitations.NewInvitationManager(r)
orgRequest, err := orgReqMgr.Get(username, organization, role, invitations.RequestPending)
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
// TODO: Save member
orgMgr := organizationdb.NewManager(r)
if org, err := orgMgr.GetByName(organization); err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else {
if invitations.RoleOwner == orgRequest.Role {
// Accepted Owner role
if err := orgMgr.SaveOwner(org, username); err != nil {
log.Error("Failed to save owner: ", username)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
} else {
// Accepted member role
if err := orgMgr.SaveMember(org, username); err != nil {
log.Error("Failed to save member: ", username)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
}
orgRequest.Status = invitations.RequestAccepted
if err := orgReqMgr.Save(orgRequest); err != nil {
log.Error("Failed to update org request status: ", orgRequest.Organization)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(orgRequest)
}
示例6: AddOrganizationOwner
// AddOrganizationOwner It is handler for POST /organizations/{globalid}/owners
func (api OrganizationsAPI) AddOrganizationOwner(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
var s searchMember
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
orgMgr := organization.NewManager(r)
org, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "getting organization", err)
}
return
}
u, err := SearchUser(r, s.SearchString)
if err != nil {
log.Error(err)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
for _, membername := range org.Owners {
if membername == u.Username {
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
return
}
}
// Create JoinRequest
invitationMgr := invitations.NewInvitationManager(r)
orgReq := &invitations.JoinOrganizationInvitation{
Role: invitations.RoleOwner,
Organization: globalid,
User: u.Username,
Status: invitations.RequestPending,
Created: db.DateTime(time.Now()),
}
if err := invitationMgr.Save(orgReq); err != nil {
log.Error("Error inviting owner: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(orgReq)
}
示例7: actualOrganizationCreation
func (api OrganizationsAPI) actualOrganizationCreation(org organization.Organization, w http.ResponseWriter, r *http.Request) {
if strings.TrimSpace(org.Globalid) == itsyouonlineGlobalID {
log.Debug("Duplicate organization")
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
return
}
if !org.IsValid() {
log.Debug("Invalid organization")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
username := context.Get(r, "authenticateduser").(string)
orgMgr := organization.NewManager(r)
logoMgr := organization.NewLogoManager(r)
count, err := orgMgr.CountByUser(username)
if err != nil {
log.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if count >= MAX_ORGANIZATIONS_PER_USER {
log.Error("Reached organization limit for user ", username)
writeErrorResponse(w, 422, "maximum_amount_of_organizations_reached")
return
}
err = orgMgr.Create(&org)
if err != nil && err != db.ErrDuplicate {
log.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err == db.ErrDuplicate {
log.Debug("Duplicate organization")
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
return
}
err = logoMgr.Create(&org)
if err != nil && err != db.ErrDuplicate {
log.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(&org)
}
示例8: GetOrganizationTree
// GetOrganizationTree is the handler for GET /organizations/{globalid}/tree
// Get organization tree.
func (api OrganizationsAPI) GetOrganizationTree(w http.ResponseWriter, r *http.Request) {
var requestedOrganization = mux.Vars(r)["globalid"]
//TODO: validate input
parentGlobalID := ""
var parentGlobalIDs = make([]string, 0, 1)
for _, localParentID := range strings.Split(requestedOrganization, ".") {
if parentGlobalID == "" {
parentGlobalID = localParentID
} else {
parentGlobalID = parentGlobalID + "." + localParentID
}
parentGlobalIDs = append(parentGlobalIDs, parentGlobalID)
}
orgMgr := organization.NewManager(r)
parentOrganizations, err := orgMgr.GetOrganizations(parentGlobalIDs)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
suborganizations, err := orgMgr.GetSubOrganizations(requestedOrganization)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
allOrganizations := append(parentOrganizations, suborganizations...)
sort.Sort(byGlobalID(allOrganizations))
//Build a treestructure
var orgTree *OrganizationTreeItem
orgTreeIndex := make(map[string]*OrganizationTreeItem)
for _, org := range allOrganizations {
newTreeItem := &OrganizationTreeItem{GlobalID: org.Globalid, Children: make([]*OrganizationTreeItem, 0, 0)}
orgTreeIndex[org.Globalid] = newTreeItem
if orgTree == nil {
orgTree = newTreeItem
} else {
path := strings.Split(org.Globalid, ".")
localName := path[len(path)-1]
parentTreeItem := orgTreeIndex[strings.TrimSuffix(org.Globalid, "."+localName)]
parentTreeItem.Children = append(parentTreeItem.Children, newTreeItem)
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(orgTree)
}
示例9: GetOrganization
// GetOrganization Get organization info
// It is handler for GET /organizations/{globalid}
func (api OrganizationsAPI) GetOrganization(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
orgMgr := organization.NewManager(r)
org, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "getting organization", err)
}
return
}
json.NewEncoder(w).Encode(org)
}
示例10: UpdateDns
func (api OrganizationsAPI) UpdateDns(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
oldDns := mux.Vars(r)["dnsname"]
body := struct {
Name string
}{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if !isValidDNSName(body.Name) {
log.Debug("Invalid DNS name: ", body.Name)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
orgMgr := organization.NewManager(r)
organization, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "getting organization", err)
}
return
}
err = orgMgr.UpdateDNS(organization, oldDns, body.Name)
if err != nil {
log.Error("Error updating DNS name", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
response := struct {
Name string `json:"name"`
}{
Name: body.Name,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
}
示例11: DeleteOrgOwner
// DeleteOrgOwner is the handler for Delete /organizations/globalid/orgowner/globalid2
// Removes an organization as an owner of this one.
func (api OrganizationsAPI) DeleteOrgOwner(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
orgOwner := mux.Vars(r)["globalid2"]
mgr := organization.NewManager(r)
if !mgr.Exists(globalid) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
// check if OrgOwner is an owner of the organization
isOwner, err := mgr.OrganizationIsOwner(globalid, orgOwner)
if err != nil {
log.Error("Error while removing another organization as owner: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if !isOwner {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
// now that we know OrgOwner is an OrgOwner of {globalid}, check if the authenticated user is an owner of the OrgOwner
// the user is known to be an owner of {globalid} since we've required the organization:owner scope
authenticateduser := context.Get(r, "authenticateduser").(string)
isOwner, err = mgr.IsOwner(orgOwner, authenticateduser)
if err != nil {
log.Error("Error while removing another organization as owner: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if !isOwner {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
err = mgr.RemoveOrganization(globalid, orgOwner)
if err != nil {
log.Error("Error while removing another organization as owner: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
示例12: globalidrolesroleDelete
// It is handler for DELETE /users/{username}/organizations/{globalid}/roles/{role}
func (api UsersusernameorganizationsAPI) globalidrolesroleDelete(w http.ResponseWriter, r *http.Request) {
username := mux.Vars(r)["username"]
role := mux.Vars(r)["role"]
organization := mux.Vars(r)["globalid"]
orgReqMgr := invitations.NewInvitationManager(r)
orgRequest, err := orgReqMgr.Get(username, organization, role, invitations.RequestPending)
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
orgMgr := organizationdb.NewManager(r)
if org, err := orgMgr.GetByName(organization); err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else {
if invitations.RoleOwner == orgRequest.Role {
// Rejected Owner role
if err := orgMgr.RemoveOwner(org, username); err != nil {
log.Error("Failed to remove owner: ", username)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
} else {
// Rejected member role
if err := orgMgr.RemoveMember(org, username); err != nil {
log.Error("Failed to reject member: ", username)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
}
orgRequest.Status = invitations.RequestRejected
if err := orgReqMgr.Save(orgRequest); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
示例13: UpdateOrganizationMemberShip
func (api OrganizationsAPI) UpdateOrganizationMemberShip(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
var membership Membership
if err := json.NewDecoder(r.Body).Decode(&membership); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
orgMgr := organization.NewManager(r)
org, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "updating organization membership", err)
}
return
}
var oldRole string
for _, v := range org.Members {
if v == membership.Username {
oldRole = "members"
}
}
for _, v := range org.Owners {
if v == membership.Username {
oldRole = "owners"
}
}
err = orgMgr.UpdateMembership(globalid, membership.Username, oldRole, membership.Role)
if err != nil {
handleServerError(w, "updating organization membership", err)
return
}
org, err = orgMgr.GetByName(globalid)
if err != nil {
handleServerError(w, "getting organization", err)
}
json.NewEncoder(w).Encode(org)
}
示例14: FilterPossibleScopes
//FilterPossibleScopes filters the requestedScopes to the relevant ones that are possible
// For example, a `user:memberof:orgid1` is not possible if the user is not a member the `orgid1` organization and there is no outstanding invite for this organization
// If allowInvitations is true, invitations to organizations allows the "user:memberof:organization" as possible scopes
func (service *Service) FilterPossibleScopes(r *http.Request, username string, requestedScopes []string, allowInvitations bool) (possibleScopes []string, err error) {
possibleScopes = make([]string, 0, len(requestedScopes))
orgmgr := organizationdb.NewManager(r)
invitationMgr := invitations.NewInvitationManager(r)
for _, rawscope := range requestedScopes {
scope := strings.TrimSpace(rawscope)
if strings.HasPrefix(scope, "user:memberof:") {
orgid := strings.TrimPrefix(scope, "user:memberof:")
isMember, err := orgmgr.IsMember(orgid, username)
if err != nil {
return nil, err
}
if isMember {
possibleScopes = append(possibleScopes, scope)
continue
}
isOwner, err := orgmgr.IsOwner(orgid, username)
if err != nil {
return nil, err
}
if isOwner {
possibleScopes = append(possibleScopes, scope)
continue
}
if allowInvitations {
hasInvite, err := invitationMgr.HasInvite(orgid, username)
if err != nil {
log.Error("FilterPossibleScopes: Error while checking if user has invite for organization: ", err)
return nil, err
}
if hasInvite {
possibleScopes = append(possibleScopes, scope)
}
}
} else {
possibleScopes = append(possibleScopes, scope)
}
}
return
}
示例15: UpdateOrganization
// UpdateOrganization Updates organization info
// It is handler for PUT /organizations/{globalid}
func (api OrganizationsAPI) UpdateOrganization(w http.ResponseWriter, r *http.Request) {
globalid := mux.Vars(r)["globalid"]
var org organization.Organization
if err := json.NewDecoder(r.Body).Decode(&org); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
orgMgr := organization.NewManager(r)
oldOrg, err := orgMgr.GetByName(globalid)
if err != nil {
if err == mgo.ErrNotFound {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
handleServerError(w, "getting organization", err)
}
return
}
if org.Globalid != globalid {
http.Error(w, "Changing globalid or id is Forbidden!", http.StatusForbidden)
return
}
// Update only certain fields
oldOrg.PublicKeys = org.PublicKeys
oldOrg.DNS = org.DNS
if err := orgMgr.Save(oldOrg); err != nil {
log.Error("Error while saving organization: ", err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(oldOrg)
}