diff --git a/changelog/unreleased/add-uid-gid.md b/changelog/unreleased/add-uid-gid.md new file mode 100644 index 0000000..cab2b7c --- /dev/null +++ b/changelog/unreleased/add-uid-gid.md @@ -0,0 +1,5 @@ +Enhancement: Add option to create user with uidnumber and gidnumber + +We have added an option to pass uidnumber and gidnumber to the ocis api while creating a new user + +https://github.com/owncloud/ocis-ocs/pull/34 diff --git a/pkg/service/v0/data/user.go b/pkg/service/v0/data/user.go index c43cc9c..6181352 100644 --- a/pkg/service/v0/data/user.go +++ b/pkg/service/v0/data/user.go @@ -14,6 +14,8 @@ type User struct { DisplayName string `json:"displayname" xml:"displayname"` Email string `json:"email" xml:"email"` Quota *Quota `json:"quota" xml:"quota"` + UIDNumber int64 `json:"uidnumber" xml:"uidnumber"` + GIDNumber int64 `json:"gidnumber" xml:"gidnumber"` } // Quota holds quota information diff --git a/pkg/service/v0/users.go b/pkg/service/v0/users.go index 42b87f3..0a71607 100644 --- a/pkg/service/v0/users.go +++ b/pkg/service/v0/users.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "fmt" "net/http" + "strconv" "strings" "github.com/cs3org/reva/pkg/user" @@ -60,6 +61,8 @@ func (o Ocs) GetUser(w http.ResponseWriter, r *http.Request) { Username: account.PreferredName, DisplayName: account.DisplayName, Email: account.Mail, + UIDNumber: account.UidNumber, + GIDNumber: account.GidNumber, Enabled: account.AccountEnabled, // FIXME only return quota for users/{userid} endpoint (not /user) // TODO query storage registry for free space? of home storage, maybe... @@ -81,6 +84,28 @@ func (o Ocs) AddUser(w http.ResponseWriter, r *http.Request) { username := r.PostFormValue("username") displayname := r.PostFormValue("displayname") email := r.PostFormValue("email") + uid := r.PostFormValue("uidnumber") + gid := r.PostFormValue("gidnumber") + + var uidNumber, gidNumber int64 + var err error + + if uid != "" { + uidNumber, err = strconv.ParseInt(uid, 10, 64) + if err != nil { + render.Render(w, r, response.ErrRender(data.MetaBadRequest.StatusCode, "Cannot use the uidnumber provided")) + o.logger.Error().Err(err).Str("userid", userid).Msg("Cannot use the uidnumber provided") + return + } + } + if gid != "" { + gidNumber, err = strconv.ParseInt(gid, 10, 64) + if err != nil { + render.Render(w, r, response.ErrRender(data.MetaBadRequest.StatusCode, "Cannot use the gidnumber provided")) + o.logger.Error().Err(err).Str("userid", userid).Msg("Cannot use the gidnumber provided") + return + } + } // fallbacks /* TODO decide if we want to make these fallbacks. Keep in mind: @@ -94,18 +119,28 @@ func (o Ocs) AddUser(w http.ResponseWriter, r *http.Request) { } */ - account, err := o.getAccountService().CreateAccount(r.Context(), &accounts.CreateAccountRequest{ - Account: &accounts.Account{ - DisplayName: displayname, - PreferredName: username, - OnPremisesSamAccountName: username, - PasswordProfile: &accounts.PasswordProfile{ - Password: password, - }, - Id: userid, - Mail: email, - AccountEnabled: true, + newAccount := &accounts.Account{ + DisplayName: displayname, + PreferredName: username, + OnPremisesSamAccountName: username, + PasswordProfile: &accounts.PasswordProfile{ + Password: password, }, + Id: userid, + Mail: email, + AccountEnabled: true, + } + + if uidNumber != 0 { + newAccount.UidNumber = uidNumber + } + + if gidNumber != 0 { + newAccount.GidNumber = gidNumber + } + + account, err := o.getAccountService().CreateAccount(r.Context(), &accounts.CreateAccountRequest{ + Account: newAccount, }) if err != nil { merr := merrors.FromError(err) @@ -130,6 +165,8 @@ func (o Ocs) AddUser(w http.ResponseWriter, r *http.Request) { Username: account.PreferredName, DisplayName: account.DisplayName, Email: account.Mail, + UIDNumber: account.UidNumber, + GIDNumber: account.UidNumber, Enabled: account.AccountEnabled, })) }