func LoadPolygonFromFile(ctx *gin.Context, conn *store.Connection) { filename := "./tnassemble.geojson" f_coll := new(geojson.FeatureCollection) file, _ := os.Open(filename) log.Debugf(conn.Context, fmt.Sprintf("file %#v", file)) jsonParser := json.NewDecoder(file) log.Debugf(conn.Context, fmt.Sprintf("jsonParser %#v", jsonParser)) jsonParser.Decode(&f_coll) log.Debugf(conn.Context, fmt.Sprintf("profiles %#v", f_coll)) for _, feature := range f_coll.Features { p := make([]Point, 0) constituency := &Constituency{} prop := feature.Properties constituency.Assemb_Const = prop["ac_name"].(string) constituency.Parl_Const = prop["pc_name"].(string) constituency.State = prop["state"].(string) geom, _ := feature.GetGeometry() multiline := geom.GetGeometry() mli := multiline.(geojson.MultiLine) for _, coord := range mli { for _, point := range coord { lon := float64(point[0]) lat := float64(point[1]) p = append(p, Point{Lat: lat, Lng: lon}) } } constituency.Points = p conn.Add(constituency) } file.Close() }
func VerifyVolunteerRequest(c *gin.Context, user *auth.User) bool { r := c.Request conn := store.New(c) conn.Get(user) ctx := appengine.NewContext(r) session := auth.GetSession(c) log.Debugf(ctx, fmt.Sprintf("details %#v", session)) req_id := session.Get("vol_req_id") log.Debugf(ctx, fmt.Sprintf("Vol_Req_id %#v", req_id)) log.Debugf(ctx, "Vol_Req_id %v", req_id) if req_id != nil { session.Delete("vol_req_id") session.Save() if req_id == user.Temp_Req_Id { user.Role = "volunteer" user.Active = "false" conn.Add(user) NotifyAdmin(c, user) return true } } else { log.Debugf(ctx, "Vol_Req_id notfound") return false } return false }
func IsAuthenticated(c *gin.Context) bool { aecontext := appengine.NewContext(c.Request) log.Debugf(aecontext, "inside auth") email := SessionGet(c, gin.AuthUserKey) log.Debugf(aecontext, "email %#v", email) if email != nil { SessionSet(c, "timestamp", time.Now().Format("2006-01-02 15-04-05")) GetUser(c) return true } return false }
func FBLogin(c *gin.Context) { // grab the code fragment aecontext := appengine.NewContext(c.Request) code := c.Query("code") fbConfig := FBConfig() token, err := fbConfig.Exchange(aecontext, code) if err != nil { log.Errorf(aecontext, err.Error()) } client := urlfetch.Client(aecontext) response, err := client.Get("https://graph.facebook.com/v2.5/me?fields=id,name,email&access_token=" + token.AccessToken) log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken)) // handle err. You need to change this into something more robust // such as redirect back to home page with error message if err != nil { c.String(http.StatusOK, "Error") } var fb_user FB_User log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response)) Bind(response, &fb_user) log.Debugf(aecontext, fmt.Sprintf("token %#v", token)) user := &auth.User{ Email: fb_user.Email, Name: fb_user.Username, Token: *token, LoginType: "facebook", } var redir_url = "" if service.VerifyVolunteerRequest(c, user) { redir_url = "/volsuccess" } log.Debugf(aecontext, fmt.Sprintf("user %#v", user)) if !auth.IsAuthenticated(c) { auth.CreateSession(c, user) } if redir_url == "" { auth.Redirect(c, redir_url) } else { auth.Redirect(c) } }
func GoogleLogin(c *gin.Context) { // grab the code fragment aecontext := appengine.NewContext(c.Request) code := c.Query("code") googleconf := GoogleConfig() token, err := googleconf.Exchange(aecontext, code) if err != nil { log.Errorf(aecontext, err.Error()) } client := urlfetch.Client(aecontext) response, err := client.Get("https://www.googleapis.com/userinfo/v2/me?access_token=" + token.AccessToken) log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken)) // handle err. You need to change this into something more robust // such as redirect back to home page with error message if err != nil { c.String(http.StatusOK, "Error") } var goog_user GoogleUser log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response)) Bind(response, &goog_user) log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", goog_user)) user := &auth.User{ Email: goog_user.Email, Name: goog_user.Username, Picture: goog_user.PictureUrl, } log.Debugf(aecontext, fmt.Sprintf("user %#v", user)) if !auth.IsAuthenticated(c) { auth.CreateSession(c, user) } auth.Redirect(c) }
func NotifyAdmin(c *gin.Context, user *auth.User) { r := c.Request ctx := appengine.NewContext(r) addr := "*****@*****.**" msg := &mail.Message{ Sender: "*****@*****.**", To: []string{addr}, Subject: fmt.Sprintf("User %s raised volunteer request", user.Email), Body: fmt.Sprintf("User Msg %s %s", user.Message), } log.Debugf(ctx, "Mail msg %v", msg) if err := mail.Send(ctx, msg); err != nil { log.Errorf(ctx, "Couldn't send email: %v", err) } }
func SendEmailVolunteer(c *gin.Context, user *auth.User) { r := c.Request ctx := appengine.NewContext(r) addr := user.Email url := createConfirmationURL(user) msg := &mail.Message{ Sender: "*****@*****.**", To: []string{addr}, Subject: "Confirm your volunteer request", Body: fmt.Sprintf(confirmMessage, url), } log.Debugf(ctx, "Mail msg %v", msg) if err := mail.Send(ctx, msg); err != nil { log.Errorf(ctx, "Couldn't send email: %v", err) } }
func RefreshFacebookTokens(c *gin.Context) { if auth.IsAuthenticated(c) { user := auth.GetUser(c) if user.LoginType == "facebook" { aecontext := appengine.NewContext(c.Request) conf := FBConfig() toksource := conf.TokenSource(aecontext, &user.Token) sourceToken := oauth2.ReuseTokenSource(&user.Token, toksource) client := oauth2.NewClient(aecontext, sourceToken) client.Get("...") t, _ := sourceToken.Token() user.Token = *t auth.UpdateUser(c, &user) log.Debugf(aecontext, fmt.Sprintf("refresh tokens %#v", t)) } } }
func AddUser(c *gin.Context, user *User) { aecontext := appengine.NewContext(c.Request) conn := store.New(c) resp := conn.Get(user) if resp.Err_msg != "" { user.Active = "false" if user.Role == "" { user.Role = "support" } user.Likes = make([]string, 0) user.Unlikes = make([]string, 0) resp := conn.Add(user) if resp.Err_msg != "" { log.Debugf(aecontext, "user not stored due to %s", resp.Err_msg) } } }
func DeleteProfile(c *gin.Context) { conn := store.New(c) status := http.StatusOK var resp store.Response prof_id, _ := strconv.ParseInt(c.Param("prof_id"), 10, 64) det_id, _ := strconv.ParseInt(c.Param("det_id"), 10, 64) det_key := conn.DatastoreKeyWithKind("ProfileDetails", det_id) prof_in := &Profile{Id: prof_id, Parent: det_key} conn.Get(prof_in) if auth.IsAuthorized(c, prof_in.Consti) { log.Debugf(conn.Context, fmt.Sprintf("profiles %#v", prof_in)) resp = conn.Remove(conn.Goon.Key(prof_in)) resp = conn.Remove(prof_in.Parent) } else { status = http.StatusUnauthorized } auth.SessionSave(c) c.JSON(status, resp) }
func PutProfile(c *gin.Context) { conn := store.New(c) status := http.StatusOK prof_out := &ProfileOut{} c.BindJSON(prof_out) var resp store.Response keys := make(map[string]int64) if auth.IsAuthorized(c, prof_out.Consti) { det_id := NumberToInt(prof_out.Details["id"]) det_key := conn.DatastoreKeyWithKind("ProfileDetails", det_id) det_list := &datastore.PropertyList{} log.Debugf(conn.Context, fmt.Sprintf("before conv %#v", det_list)) det_list = MapToPropertyList(prof_out.Details) log.Debugf(conn.Context, fmt.Sprintf("after conv %#v", det_list)) det_key = conn.PropListPut(det_list, det_key) log.Debugf(conn.Context, fmt.Sprintf("details %#v", det_list)) prof_in := &Profile{Id: prof_out.Id, Parent: det_key} conn.Get(prof_in) prof_in.Parent = det_key prof_in.Comments = prof_out.Comments prof_in.Surveys = prof_out.Surveys if prof_in.Comments == nil { prof_in.Comments = make([]Comment, 0) } if prof_in.Surveys == nil { prof_in.Surveys = make([]Survey, 0) } else { for i, surv := range prof_in.Surveys { if surv.Id == "" { prof_in.Surveys[i].Id = uuid.NewV4().String() } } } prof_in.Consti = prof_out.Consti prof_in.Meta = prof_out.Meta resp = conn.Add(prof_in) keys["prof_id"] = resp.Data.(*datastore.Key).IntID() keys["det_id"] = det_key.IntID() for i, surv := range prof_in.Surveys { if surv.Prof_Id == "" { t_p_id := strconv.FormatInt(keys["prof_id"], 10) t_det_id := strconv.FormatInt(keys["det_id"], 10) prof_in.Surveys[i].Prof_Id = t_p_id prof_in.Surveys[i].Det_Id = t_det_id } } conn.Add(prof_in) log.Debugf(conn.Context, fmt.Sprintf("%#v", prof_in)) } else { status = http.StatusUnauthorized } auth.SessionSave(c) c.JSON(status, keys) }