Exemple #1
0
//
// /users?key=[email, surname, objectguid, etc..]&val=<corresponding key value>
func usersGetHandler(req *auburn.HttpTransaction) {
	if req.Request.Method == "GET" {
		matches := make([]User, 0)

		key := req.Query("key")
		val := req.Query("val")
		domain := req.Query("domain")

		if len(key) == 0 {
			req.Error("Missing key from URL", 400)
			return
		}

		if len(val) == 0 {
			req.Error("Missing val from URL", 400)
			return
		}

		key = strings.ToLower(key)
		val = strings.ToLower(val)
		domain = strings.ToLower(domain)

		for _, v := range users {
			propMatch := false

			if key == "userprincipalname" {
				if strings.Contains(strings.ToLower(v.UserPrincipalName), val) {
					propMatch = true
				}
			} else if key == "email" {
				if strings.Contains(strings.ToLower(v.Email), val) {
					propMatch = true
				}
			} else if key == "surname" {
				if strings.Contains(strings.ToLower(v.Surname), val) {
					propMatch = true
				}
			} else if key == "givenname" {
				if strings.Contains(strings.ToLower(v.GivenName), val) {
					propMatch = true
				}
			} else if key == "objectguid" {
				if strings.Contains(strings.ToLower(v.ObjectGuid), val) {
					propMatch = true
				}
			}

			if propMatch {
				if len(domain) == 0 {
					matches = append(matches, v)
				} else if len(domain) > 0 && strings.Contains(strings.ToLower(v.Domain), domain) {
					matches = append(matches, v)
				}
			}
		}

		req.RespondWithJSON(matches)
	}
}
Exemple #2
0
func usersPostHandler(req *auburn.HttpTransaction) {
	if req.Request.Method == "POST" {
		user := User{}
		err := req.UnmarshalJsonBody(&user)

		if err != nil {
			req.Error(err.Error(), 500)
			return
		}

		if len(user.ObjectGuid) == 0 {
			req.Error("Object GUID required", 400)
			return
		}

		exists := false

		for i, v := range users {
			if v.ObjectGuid == user.ObjectGuid {
				users[i] = user
				users[i].Updated = time.Now()
				exists = true
			}
		}

		if !exists {
			user.Updated = time.Now()
			users = append(users, user)
		}

		req.RespondWithText("OK")
	}
}