github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/web/account.go (about) 1 package web 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "net/url" 8 "path" 9 "strings" 10 ) 11 12 func (s *Service) account(w http.ResponseWriter, r *http.Request) { 13 // Get the session service from the request context 14 sessionService, err := getSessionService(r) 15 if err != nil { 16 http.Error(w, err.Error(), http.StatusInternalServerError) 17 return 18 } 19 20 session, err := sessionService.GetUserSession() 21 currentUser, err := s.GetOauthService().FindUserByUsername(session.Username) 22 if err != nil { 23 http.Error(w, err.Error(), http.StatusInternalServerError) 24 return 25 } 26 27 var f = UpdateUserForm{User: currentUser} 28 29 // Render the template 30 errMsg, _ := sessionService.GetFlashMessage() 31 renderTemplate(w, "account.html", map[string]interface{}{ 32 "error": errMsg, 33 "currentUser": currentUser, 34 "form": &f, 35 "username": session.Username, 36 "queryString": getQueryString(r.URL.Query()), 37 }) 38 } 39 40 func (s *Service) updateUser(w http.ResponseWriter, r *http.Request) { 41 // Get the session service from the request context 42 sessionService, err := getSessionService(r) 43 if err != nil { 44 http.Error(w, err.Error(), http.StatusInternalServerError) 45 return 46 } 47 48 session, err := sessionService.GetUserSession() 49 currentUser, err := s.GetOauthService().FindUserByUsername(session.Username) 50 if err != nil { 51 http.Error(w, err.Error(), http.StatusInternalServerError) 52 return 53 } 54 55 r.ParseMultipartForm(32 << 20) 56 57 var f = UpdateUserForm{User: currentUser} 58 err = parseForm(&f, r.PostForm) 59 60 if err != nil { 61 http.Error(w, err.Error(), http.StatusInternalServerError) 62 return 63 } 64 65 f.Valid() 66 67 filename, err := uploadFile("avatar_url", r) 68 69 if err == nil { 70 71 // http.Error(w, err.Error(), http.StatusInternalServerError) 72 // f.AddError("AvatarURL", err.Error()) 73 avatarUrl, err := s.mapAssetToSuite(filename) 74 if err != nil { 75 // http.Error(w, err.Error(), http.StatusInternalServerError) 76 f.AddError("AvatarURL", err.Error()) 77 } 78 79 if len(avatarUrl) > 0 { 80 s.GetOauthService().UpdateUser(currentUser, map[string]interface{}{"AvatarURL": avatarUrl}) 81 } 82 } 83 84 password := r.Form.Get("password") 85 86 if len(password) > 0 { 87 if err := s.GetOauthService().SetPassword(currentUser, password); err != nil { 88 // http.Error(w, err.Error(), http.StatusInternalServerError) 89 f.AddError("Password", err.Error()) 90 // return 91 } 92 } 93 94 params := f.Diff() 95 if len(params) > 0 { 96 s.GetOauthService().UpdateUser(currentUser, params) 97 } 98 99 errMsg, _ := sessionService.GetFlashMessage() 100 renderTemplate(w, "index.html", map[string]interface{}{ 101 "error": errMsg, 102 "form": &f, 103 "currentUser": currentUser, 104 "username": session.Username, 105 "queryString": getQueryString(r.URL.Query()), 106 }) 107 } 108 109 func (s *Service) mapAssetToSuite(filename string) (uri string, err error) { 110 config := s.GetConfig() 111 if config == nil { 112 return "", fmt.Errorf("invalid config") 113 } 114 115 log.Printf("map: %#v", config.AssetsMappings) 116 117 for _, asset := range config.AssetsMappings { 118 if strings.Index(filename, asset.Dir) == 0 { 119 rest := filename[len(asset.Dir):] 120 u, err := url.Parse(asset.Host) 121 if err != nil { 122 continue 123 } 124 125 u.Path = path.Join(u.Path, rest) 126 return u.String(), nil 127 } 128 } 129 130 return "", fmt.Errorf("don't have mapping asset to host path config") 131 }