func PreventReauth(session sessions.Session, r render.Render) { _, ok := session.Get("id").(int64) if ok { session.AddFlash("warning: You are already signed in!") r.Redirect("/dashboard") } }
func Login(session sessions.Session, re render.Render, r *http.Request) { client_id := common.Config.OAuth2Client_ID letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") b := make([]rune, 10) for i := range b { b[i] = letters[rand.Intn(len(letters))] } session.AddFlash(string(b), "state") redirectBack := r.URL.Query().Get("redirect_back") ref := r.Referer() if redirectBack == "true" && ref != "" { session.Set("redirect_to", ref) } else { session.Set("redirect_to", nil) } query := url.Values{} query.Set("client_id", client_id) query.Set("state", string(b)) query.Set("scope", "repo") dest := url.URL{ Scheme: "https", Host: "github.com", Path: "/login/oauth/authorize", RawQuery: query.Encode(), } re.Redirect(dest.String()) }
// SectionsDelete deletes a section from user's CV func SectionsDelete(r render.Render, tokens oauth2.Tokens, session sessions.Session, params martini.Params) { sectionID, _ := strconv.Atoi(params["section_id"]) section := &Section{} db.Delete(section, sectionID) session.AddFlash("You have successfully deleted a section from your CV.", "success") r.Redirect(config.AppUrl+"/sections", 302) }
// ShowPrivate renders private user's CV func ShowPrivate(r render.Render, req *http.Request, tokens oauth2.Tokens, session sessions.Session, params martini.Params) { pd := NewPageData(tokens, session) username := params["username"] if pd.User.Username == username { Show(r, req, username) } else { session.AddFlash("This is not your CV.", "error") r.Redirect(config.AppUrl+"/dashboard", 302) } }
// SaveSocial saves dashboard page func SaveSocial(r render.Render, tokens oauth2.Tokens, session sessions.Session, social SocialNetworksForm, err binding.Errors) { pd := NewPageData(tokens, session) log.Printf("[SaveSocial] social: %s", social) user := pd.User user.LinkedIn = strings.Replace(strings.Replace(social.LinkedIn, "https://", "", -1), "http://", "", -1) user.Facebook = strings.Replace(strings.Replace(social.Facebook, "https://", "", -1), "http://", "", -1) user.Twitter = strings.Replace(strings.Replace(social.Twitter, "https://", "", -1), "http://", "", -1) user.GitHub = strings.Replace(strings.Replace(social.GitHub, "https://", "", -1), "http://", "", -1) user.Instagram = strings.Replace(strings.Replace(social.Instagram, "https://", "", -1), "http://", "", -1) db.Save(user) session.AddFlash("You have successfully updated your BareCV profile.", "success") r.Redirect(config.AppUrl+"/cv", 302) }
func EnsureAuth(session sessions.Session, r render.Render, req *http.Request, c martini.Context) { id, ok := session.Get("id").(int64) if !ok || id == 0 { session.AddFlash("warning: You must login first!") session.Set("previous_url", req.RequestURI) r.Redirect("/signin") } else if ok { var user models.User err := utils.ORM.First(&user, id).Error if err != nil { r.Error(500) return } c.Map(user) } }
// SettingsSave saves user's settings func SettingsSave(r render.Render, tokens oauth2.Tokens, session sessions.Session, settings SettingsForm, err binding.Errors, req *http.Request) { pd := NewPageData(tokens, session) log.Printf("[SettingsSave] settings: %s", settings) log.Printf("[SettingsSave] settings.SearchIndexingEnabled: %s", settings.SearchIndexingEnabled) userSettings := pd.Settings userSettings.Color = settings.Color userSettings.Font = settings.Font userSettings.GoogleAnalytics = settings.GoogleAnalytics userSettings.SearchIndexingEnabled = settings.SearchIndexingEnabled == "on" userSettings.PrivacyLevel = settings.PrivacyLevel if userSettings.PrivacyLevel == PrivacyHash { userSettings.Hash = RandSeq(32) } db.Save(userSettings) session.AddFlash("You have successfully saved your settings.", "success") r.Redirect(config.AppUrl+"/settings", 302) }
// AccountSave renders user's account page func AccountSave(r render.Render, tokens oauth2.Tokens, session sessions.Session, username UsernameForm, err binding.Errors) { pd := NewPageData(tokens, session) if len(username.Username) > 0 { existingUser := &User{Username: username.Username} db.Where(existingUser).First(existingUser) if existingUser.ID > 0 { err.Add([]string{"username"}, "RequiredError", "This username is already taken.") } log.Printf("[AccountSave] existing user: %s", existingUser) } if err.Len() == 0 { user := pd.User if user.Username != username.Username { path := filepath.Join(HomeDir(), "app/public/files/"+user.Username) if len(user.Username) == 0 { path = filepath.Join(HomeDir(), "app/public/files/"+username.Username) } if _, err := os.Stat(path); os.IsNotExist(err) || len(user.Username) == 0 { os.Mkdir(path, 0755) } if _, err := os.Stat(path); err == nil { newPath := filepath.Join(HomeDir(), "app/public/files/"+username.Username) os.Rename(path, newPath) } user.Username = username.Username db.Save(user) } session.AddFlash("You have successfully updated your BareCV username / domain.", "success") r.Redirect(config.AppUrl+"/account", 302) } else { pd.Errors = &err log.Printf("[AccountSave] errors: %s", err[0].Classification) r.HTML(200, "account", pd) } }
// Save saves dashboard page func Save(r render.Render, tokens oauth2.Tokens, session sessions.Session, profile ProfileForm, err binding.Errors) { pd := NewPageData(tokens, session) log.Printf("[Save] profile: %s", profile) if err.Len() == 0 { user := pd.User user.Name = profile.Name user.Profession = profile.Profession user.Email = profile.Email user.Phone = profile.Phone user.Website = profile.Website user.Address = profile.Address db.Save(user) session.AddFlash("You have successfully updated your BareCV profile.", "success") r.Redirect(config.AppUrl+"/cv", 302) } else { pd.Errors = &err log.Printf("[Save] errors: %s", err[0].FieldNames) r.HTML(200, "cv", pd) } }
// AccountRedirect redirects user to the account page with a flash message func AccountRedirect(r render.Render, tokens oauth2.Tokens, session sessions.Session) { session.AddFlash("First you must choose a BareCV username.", "error") r.Redirect(config.AppUrl+"/account", 302) }
// SectionsPost saves new section to the database func SectionsPost(r render.Render, tokens oauth2.Tokens, session sessions.Session, params martini.Params, req *http.Request) { pd := NewPageData(tokens, session) req.ParseForm() pd.SectionType, _ = strconv.Atoi(params["type"]) pd.Section = &Section{} action := req.Form.Get("action") log.Printf("[SectionPost] action: %s", action) var title, subtitle, left, right string errors := &binding.Errors{} req.ParseForm() if pd.SectionType == TypeTitle { title = req.Form.Get("title") pd.Section.Title = title log.Printf("[SectionsPost] title: %s", title) if len(title) == 0 { errors.Add([]string{"title"}, "RequiredError", "This field is required.") } } else if pd.SectionType == TypeSubtitle { subtitle = req.Form.Get("subtitle") pd.Section.Subtitle = subtitle if len(subtitle) == 0 { errors.Add([]string{"subtitle"}, "RequiredError", "This field is required.") } } else if pd.SectionType == TypeParagraph { left = req.Form.Get("left") right = req.Form.Get("right") pd.Section.Left = left pd.Section.Right = right if len(left) == 0 { errors.Add([]string{"left"}, "RequiredError", "This field is required.") } if len(right) == 0 { errors.Add([]string{"right"}, "RequiredError", "This field is required.") } } if errors.Len() == 0 { section := &Section{} if action == "update" { sectionID, _ := strconv.Atoi(params["section_id"]) db.First(section, sectionID) } else { section.Type = pd.SectionType section.User = *pd.User section.OrderID = pd.User.GetLastSectionOrderId() } if pd.SectionType == TypeTitle { section.Title = title } else if pd.SectionType == TypeSubtitle { section.Subtitle = subtitle } else if pd.SectionType == TypeParagraph { section.Left = left section.Right = right } db.Save(section) if action == "update" { session.AddFlash("You have successfully updated a section of your CV.", "success") } else { session.AddFlash("You have successfully added a new section to your CV.", "success") } r.Redirect(config.AppUrl+"/sections", 302) } else { pd.Errors = errors log.Printf("[Save] errors: %s", errors) r.HTML(200, "sections-new", pd) } }