graph: Fix handling of required attributes for users and groups
Fix a possible panic when checking for missing attribute ('err' was used
without actually being set). Return more helpful messages when required
attributes of a user or group are missing.
This commit is contained in:
1 parent
1d25f03deb
commit
6fffe04120
2 files changed
+23
-17
No files matched your search
@@ -42,7 +42,7 @@ func (g Graph) PostGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isNilOrEmpty(grp.DisplayName) {
|
if _, ok := grp.GetDisplayNameOk(); !ok {
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "Missing Required Attribute")
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "Missing Required Attribute")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ func (g Graph) PostGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Disallow user-supplied IDs. It's supposed to be readonly. We're either
|
// Disallow user-supplied IDs. It's supposed to be readonly. We're either
|
||||||
// generating them in the backend ourselves or rely on the Backend's
|
// generating them in the backend ourselves or rely on the Backend's
|
||||||
// storage (e.g. LDAP) to provide a unique ID.
|
// storage (e.g. LDAP) to provide a unique ID.
|
||||||
if !isNilOrEmpty(grp.Id) {
|
if _, ok := grp.GetIdOk(); ok {
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "group id is a read-only attribute")
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "group id is a read-only attribute")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,26 +60,36 @@ func (g Graph) PostUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isNilOrEmpty(u.DisplayName) || isNilOrEmpty(u.OnPremisesSamAccountName) || isNilOrEmpty(u.Mail) {
|
if _, ok := u.GetDisplayNameOk(); !ok {
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, err.Error())
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "missing required Attribute: 'displayName'")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if accountName, ok := u.GetOnPremisesSamAccountNameOk(); ok {
|
||||||
|
if !isValidUsername(*accountName) {
|
||||||
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest,
|
||||||
|
fmt.Sprintf("username '%s' must be at least the local part of an email", *u.OnPremisesSamAccountName))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "missing required Attribute: 'onPremisesSamAccountName'")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidUsername(*u.OnPremisesSamAccountName) {
|
if mail, ok := u.GetMailOk(); ok {
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest,
|
if !isValidEmail(*mail) {
|
||||||
fmt.Sprintf("username '%s' must be at least the local part of an email", *u.OnPremisesSamAccountName))
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest,
|
||||||
return
|
fmt.Sprintf("'%s' is not a valid email address", *u.Mail))
|
||||||
}
|
return
|
||||||
if !isValidEmail(*u.Mail) {
|
}
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest,
|
} else {
|
||||||
fmt.Sprintf("'%s' is not a valid email address", *u.Mail))
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "missing required Attribute: 'mail'")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disallow user-supplied IDs. It's supposed to be readonly. We're either
|
// Disallow user-supplied IDs. It's supposed to be readonly. We're either
|
||||||
// generating them in the backend ourselves or rely on the Backend's
|
// generating them in the backend ourselves or rely on the Backend's
|
||||||
// storage (e.g. LDAP) to provide a unique ID.
|
// storage (e.g. LDAP) to provide a unique ID.
|
||||||
if !isNilOrEmpty(u.Id) {
|
if _, ok := u.GetIdOk(); ok {
|
||||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user id is a read-only attribute")
|
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user id is a read-only attribute")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -202,10 +212,6 @@ func (g Graph) PatchUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNilOrEmpty(s *string) bool {
|
|
||||||
return s == nil || *s == ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// We want to allow email addresses as usernames so they show up when using them in ACLs on storages that allow integration with our glauth LDAP service
|
// We want to allow email addresses as usernames so they show up when using them in ACLs on storages that allow integration with our glauth LDAP service
|
||||||
// so we are adding a few restrictions from https://stackoverflow.com/questions/6949667/what-are-the-real-rules-for-linux-usernames-on-centos-6-and-rhel-6
|
// so we are adding a few restrictions from https://stackoverflow.com/questions/6949667/what-are-the-real-rules-for-linux-usernames-on-centos-6-and-rhel-6
|
||||||
// names should not start with numbers
|
// names should not start with numbers
|
||||||
|
|||||||
Reference in new issue
Block a user