本文整理汇总了Golang中github.com/tsuru/tsuru/auth.GetUserByEmail函数的典型用法代码示例。如果您正苦于以下问题:Golang GetUserByEmail函数的具体用法?Golang GetUserByEmail怎么用?Golang GetUserByEmail使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GetUserByEmail函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestDissociateRole
func (s *S) TestDissociateRole(c *check.C) {
_, err := permission.NewRole("test", "team")
c.Assert(err, check.IsNil)
token := userWithPermission(c, permission.Permission{
Scheme: permission.PermRoleUpdateDissociate,
Context: permission.Context(permission.CtxGlobal, ""),
})
u, err := auth.GetUserByEmail("[email protected]")
c.Assert(err, check.IsNil)
err = u.AddRole("test", "myteam")
c.Assert(err, check.IsNil)
c.Assert(u.Roles, check.HasLen, 2)
req, err := http.NewRequest("DELETE", "/roles/test/user/[email protected]?context=myteam", nil)
c.Assert(err, check.IsNil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "bearer "+token.GetValue())
recorder := httptest.NewRecorder()
server := RunServer(true)
server.ServeHTTP(recorder, req)
c.Assert(err, check.IsNil)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
u, err = auth.GetUserByEmail("[email protected]")
c.Assert(err, check.IsNil)
c.Assert(u.Roles, check.HasLen, 1)
}
示例2: createUser
func createUser(w http.ResponseWriter, r *http.Request) error {
var u auth.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.StatusBadRequest, Message: emailError}
}
if !validation.ValidateLength(u.Password, passwordMinLen, passwordMaxLen) {
return &errors.HTTP{Code: http.StatusBadRequest, Message: passwordError}
}
if _, err = auth.GetUserByEmail(u.Email); err == nil {
return &errors.HTTP{Code: http.StatusConflict, Message: "This email is already registered"}
}
gURL := repository.ServerURL()
c := gandalf.Client{Endpoint: gURL}
if _, err := c.NewUser(u.Email, keyToMap(u.Keys)); err != nil {
return fmt.Errorf("Failed to create user in the git server: %s", err)
}
u.Quota = quota.Unlimited
if limit, err := config.GetInt("quota:apps-per-user"); err == nil && limit > -1 {
u.Quota.Limit = limit
}
if err := u.Create(); err == nil {
rec.Log(u.Email, "create-user")
w.WriteHeader(http.StatusCreated)
return nil
}
return err
}
示例3: removeUser
// removeUser removes the user from the database and from repository server
//
// If the user is the only one in a team an error will be returned.
func removeUser(w http.ResponseWriter, r *http.Request, t auth.Token) error {
u, err := t.User()
if err != nil {
return err
}
email := r.URL.Query().Get("user")
if email != "" {
if !permission.Check(t, permission.PermUserDelete) {
return permission.ErrUnauthorized
}
u, err = auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
}
appNames, err := deployableApps(u)
if err != nil {
return err
}
manager := repository.Manager()
for _, name := range appNames {
manager.RevokeAccess(name, u.Email)
}
rec.Log(u.Email, "remove-user")
if err := manager.RemoveUser(u.Email); err != nil {
log.Errorf("Failed to remove user from repository manager: %s", err)
}
return app.AuthScheme.Remove(u)
}
示例4: addUserToTeam
func addUserToTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
teamName := r.URL.Query().Get(":team")
email := r.URL.Query().Get(":user")
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "add-user-to-team", "team="+teamName, "user="+email)
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
team, err := auth.GetTeam(teamName)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: "Team not found"}
}
if !team.ContainsUser(u) {
msg := fmt.Sprintf("You are not authorized to add new users to the team %s", team.Name)
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
user, err := auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: "User not found"}
}
actions := []*action.Action{
&addUserToTeamInRepositoryAction,
&addUserToTeamInDatabaseAction,
}
pipeline := action.NewPipeline(actions...)
return pipeline.Execute(user, team)
}
示例5: TestChangeUserQuota
func (s *QuotaSuite) TestChangeUserQuota(c *check.C) {
conn, err := db.Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
user := &auth.User{
Email: "[email protected]",
Password: "qwe123",
Quota: quota.Quota{Limit: 4, InUse: 2},
}
_, err = nativeScheme.Create(user)
c.Assert(err, check.IsNil)
defer conn.Users().Remove(bson.M{"email": user.Email})
body := bytes.NewBufferString("limit=40")
request, _ := http.NewRequest("PUT", "/users/[email protected]/quota", body)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", "bearer "+s.token.GetValue())
recorder := httptest.NewRecorder()
handler := RunServer(true)
handler.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
user, err = auth.GetUserByEmail(user.Email)
c.Assert(err, check.IsNil)
c.Assert(user.Quota.Limit, check.Equals, 40)
c.Assert(user.Quota.InUse, check.Equals, 2)
}
示例6: regenerateAPIToken
// title: regenerate token
// path: /users/api-key
// method: POST
// produce: application/json
// responses:
// 200: OK
// 401: Unauthorized
// 404: User not found
func regenerateAPIToken(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
email := r.URL.Query().Get("user")
if email == "" {
email = t.GetUserName()
}
allowed := permission.Check(t, permission.PermUserUpdateToken,
permission.Context(permission.CtxUser, email),
)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: userTarget(email),
Kind: permission.PermUserUpdateToken,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermUserReadEvents, permission.Context(permission.CtxUser, email)),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
u, err := auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
apiKey, err := u.RegenerateAPIKey()
if err != nil {
return err
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(apiKey)
}
示例7: resetPassword
// title: reset password
// path: /users/{email}/password
// method: POST
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
// 403: Forbidden
// 404: Not found
func resetPassword(w http.ResponseWriter, r *http.Request) (err error) {
managed, ok := app.AuthScheme.(auth.ManagedScheme)
if !ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: nonManagedSchemeMsg}
}
r.ParseForm()
email := r.URL.Query().Get(":email")
token := r.FormValue("token")
evt, err := event.New(&event.Opts{
Target: userTarget(email),
Kind: permission.PermUserUpdateReset,
RawOwner: event.Owner{Type: event.OwnerTypeUser, Name: email},
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermUserReadEvents, permission.Context(permission.CtxUser, email)),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
u, err := auth.GetUserByEmail(email)
if err != nil {
if err == auth.ErrUserNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
} else if e, ok := err.(*errors.ValidationError); ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: e.Error()}
}
return err
}
if token == "" {
return managed.StartPasswordReset(u)
}
return managed.ResetPassword(u, token)
}
示例8: TestChangeUserQuota
func (s *QuotaSuite) TestChangeUserQuota(c *check.C) {
conn, err := db.Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
user := &auth.User{
Email: "[email protected]",
Password: "qwe123",
Quota: quota.Quota{Limit: 4, InUse: 2},
}
_, err = nativeScheme.Create(user)
c.Assert(err, check.IsNil)
defer conn.Users().Remove(bson.M{"email": user.Email})
body := bytes.NewBufferString("limit=40")
request, _ := http.NewRequest("PUT", "/users/[email protected]/quota", body)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", "bearer "+s.token.GetValue())
recorder := httptest.NewRecorder()
handler := RunServer(true)
handler.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
user, err = auth.GetUserByEmail(user.Email)
c.Assert(err, check.IsNil)
c.Assert(user.Quota.Limit, check.Equals, 40)
c.Assert(user.Quota.InUse, check.Equals, 2)
c.Assert(eventtest.EventDesc{
Target: event.Target{Type: event.TargetTypeUser, Value: user.Email},
Owner: s.token.GetUserName(),
Kind: "user.update.quota",
StartCustomData: []map[string]interface{}{
{"name": ":email", "value": user.Email},
{"name": "limit", "value": "40"},
},
}, eventtest.HasEvent)
}
示例9: dissociateRole
// title: dissociate role from user
// path: /roles/{name}/user/{email}
// method: DELETE
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
// 404: Role not found
func dissociateRole(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
if !permission.Check(t, permission.PermRoleUpdateDissociate) {
return permission.ErrUnauthorized
}
roleName := r.URL.Query().Get(":name")
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypeRole, Value: roleName},
Kind: permission.PermRoleUpdateDissociate,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermRoleReadEvents),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
email := r.URL.Query().Get(":email")
contextValue := r.URL.Query().Get("context")
user, err := auth.GetUserByEmail(email)
if err != nil {
return err
}
err = canUseRole(t, roleName, contextValue)
if err != nil {
return err
}
err = runWithPermSync([]auth.User{*user}, func() error {
return user.RemoveRole(roleName, contextValue)
})
return err
}
示例10: removeUserFromTeam
func removeUserFromTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
email := r.URL.Query().Get(":user")
teamName := r.URL.Query().Get(":team")
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "remove-user-from-team", "team="+teamName, "user="+email)
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
team, err := auth.GetTeam(teamName)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: "Team not found"}
}
if !team.ContainsUser(u) {
msg := fmt.Sprintf("You are not authorized to remove a member from the team %s", team.Name)
return &errors.HTTP{Code: http.StatusUnauthorized, Message: msg}
}
if len(team.Users) == 1 {
msg := "You can not remove this user from this team, because it is the last user within the team, and a team can not be orphaned"
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
user, err := auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
err = removeUserFromTeamInDatabase(user, team)
if err != nil {
return err
}
return removeUserFromTeamInRepository(user, team)
}
示例11: TestOAuthRemove
func (s *S) TestOAuthRemove(c *check.C) {
scheme := OAuthScheme{}
s.rsps["/token"] = `access_token=my_token`
s.rsps["/user"] = `{"email":"[email protected]"}`
params := make(map[string]string)
params["code"] = "abcdefg"
params["redirectUrl"] = "http://localhost"
token, err := scheme.Login(params)
c.Assert(err, check.IsNil)
u, err := token.User()
c.Assert(err, check.IsNil)
err = scheme.Remove(u)
c.Assert(err, check.IsNil)
conn, err := db.Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
var tokens []Token
coll := collection()
defer coll.Close()
err = coll.Find(bson.M{"useremail": "[email protected]"}).All(&tokens)
c.Assert(err, check.IsNil)
c.Assert(tokens, check.HasLen, 0)
_, err = auth.GetUserByEmail("[email protected]")
c.Assert(err, check.Equals, auth.ErrUserNotFound)
}
示例12: removeUser
// removeUser removes the user from the database and from repository server
//
// If the user is the only one in a team an error will be returned.
func removeUser(w http.ResponseWriter, r *http.Request, t auth.Token) error {
u, err := t.User()
if err != nil {
return err
}
email := r.URL.Query().Get("user")
if email != "" && u.IsAdmin() {
u, err = auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
} else if u.IsAdmin() {
return &errors.HTTP{Code: http.StatusBadRequest, Message: "please specify the user you want to remove"}
} else if email != "" {
return &errors.HTTP{Code: http.StatusForbidden, Message: "you're not allowed to remove this user"}
}
alwdApps, err := u.AllowedApps()
if err != nil {
return err
}
manager := repository.Manager()
for _, app := range alwdApps {
manager.RevokeAccess(app, u.Email)
}
teams, err := u.Teams()
if err != nil {
return err
}
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
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, then 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 = conn.Teams().Update(bson.M{"_id": team.Name}, team)
if err != nil {
return err
}
}
rec.Log(u.Email, "remove-user")
if err := manager.RemoveUser(u.Email); err != nil {
log.Errorf("Failed to remove user from repository manager: %s", err)
}
return app.AuthScheme.Remove(u)
}
示例13: TestOAuthCreate
func (s *S) TestOAuthCreate(c *gocheck.C) {
scheme := OAuthScheme{}
user := auth.User{Email: "[email protected]"}
_, err := scheme.Create(&user)
c.Assert(err, gocheck.IsNil)
dbUser, err := auth.GetUserByEmail(user.Email)
c.Assert(err, gocheck.IsNil)
c.Assert(dbUser.Email, gocheck.Equals, user.Email)
c.Assert(s.testHandler.Url, gocheck.Equals, "")
}
示例14: TestSamlCreate
func (s *S) TestSamlCreate(c *check.C) {
scheme := SAMLAuthScheme{}
user := auth.User{Email: "[email protected]"}
_, err := scheme.Create(&user)
c.Assert(err, check.IsNil)
dbUser, err := auth.GetUserByEmail(user.Email)
c.Assert(err, check.IsNil)
c.Assert(dbUser.Email, check.Equals, user.Email)
c.Assert(dbUser.Password, check.Equals, "")
}
示例15: TestOAuthCreate
func (s *S) TestOAuthCreate(c *check.C) {
scheme := OAuthScheme{}
user := auth.User{Email: "[email protected]", Password: "something"}
_, err := scheme.Create(&user)
c.Assert(err, check.IsNil)
dbUser, err := auth.GetUserByEmail(user.Email)
c.Assert(err, check.IsNil)
c.Assert(dbUser.Email, check.Equals, user.Email)
c.Assert(dbUser.Password, check.Equals, "")
c.Assert(repositorytest.Users(), check.DeepEquals, []string{user.Email})
}