func CreateUser(w http.ResponseWriter, r *http.Request) error { var u User err := json.NewDecoder(r.Body).Decode(&u) if err != nil { return &errors.Http{Code: http.StatusBadRequest, Message: err.Error()} } if !validation.ValidateEmail(u.Email) { return &errors.Http{Code: http.StatusPreconditionFailed, Message: emailError} } if !validation.ValidateLength(u.Password, passwordMinLen, passwordMaxLen) { return &errors.Http{Code: http.StatusPreconditionFailed, Message: passwordError} } gUrl := repository.GitServerUri() c := gandalf.Client{Endpoint: gUrl} if _, err := c.NewUser(u.Email, keyToMap(u.Keys)); err != nil { return &errors.Http{ Code: http.StatusInternalServerError, Message: "Could not communicate with git server. Aborting...", } } if err := u.Create(); err == nil { w.WriteHeader(http.StatusCreated) return nil } if u.Get() == nil { err = &errors.Http{Code: http.StatusConflict, Message: "This email is already registered"} } return err }
// createRepository forward creates a git repository using the // gandalf client. func (a *createRepository) forward(app *App, args ...interface{}) error { gUrl := repository.GitServerUri() var users []string for _, t := range app.GetTeams() { users = append(users, t.Users...) } c := gandalf.Client{Endpoint: gUrl} _, err := c.NewRepository(app.Name, users, false) return err }
// RemoveUser removes the user from the database and from gandalf server // // In order to successfuly remove a user, it's need that he/she is not the only one in a team, // otherwise the function will return an error // TODO: improve the team update, if possible func RemoveUser(w http.ResponseWriter, r *http.Request, u *User) error { //_, err := db.Session.Teams().UpdateAll(bson.M{"users": u.Email}, bson.M{"$pull": bson.M{"users": u.Email}}) gUrl := repository.GitServerUri() c := gandalf.Client{Endpoint: gUrl} alwdApps, err := allowedApps(u.Email) if err != nil { return err } if err := c.RevokeAccess(alwdApps, []string{u.Email}); err != nil { return err } teams, err := u.Teams() if err != nil { return err } for _, team := range teams { if len(team.Users) < 2 { msg := fmt.Sprintf(`This user is the last member of the team "%s", so it cannot be removed. Please remove the team, them remove the user.`, team.Name) return &errors.Http{Code: http.StatusForbidden, Message: msg} } err = team.removeUser(u) if err != nil { return err } // this can be done without the loop err = db.Session.Teams().Update(bson.M{"_id": team.Name}, team) if err != nil { return err } } if err := c.RemoveUser(u.Email); err != nil { return &errors.Http{Code: http.StatusInternalServerError, Message: "Could not communicate with git server. Aborting..."} } return db.Session.Users().Remove(bson.M{"email": u.Email}) }
// createRepository backward remove the git repository // using the gandalf client. func (a *createRepository) backward(app *App, args ...interface{}) { gUrl := repository.GitServerUri() c := gandalf.Client{Endpoint: gUrl} c.RemoveRepository(app.Name) }