github.com/saadullahsaeed/fragmenta-cms@v1.5.4/src/pages/actions/setup.go (about) 1 package pageactions 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 8 "github.com/fragmenta/auth" 9 "github.com/fragmenta/mux" 10 "github.com/fragmenta/server" 11 "github.com/fragmenta/server/log" 12 "github.com/fragmenta/view" 13 14 "github.com/fragmenta/fragmenta-cms/src/pages" 15 "github.com/fragmenta/fragmenta-cms/src/users" 16 ) 17 18 // HandleSetupShow responds to GET /fragmenta/setup 19 func HandleSetupShow(w http.ResponseWriter, r *http.Request) error { 20 21 // Check we have no users, if not bail out 22 if users.Count() != 0 { 23 return server.NotAuthorizedError(nil, "Users already exist") 24 } 25 26 // Render the template 27 view := view.NewRenderer(w, r) 28 view.Template("pages/views/setup.html.got") 29 return view.Render() 30 } 31 32 // HandleSetup responds to POST /fragmenta/setup 33 func HandleSetup(w http.ResponseWriter, r *http.Request) error { 34 35 // Check we have no users, if not bail out 36 if users.Count() != 0 { 37 return server.NotAuthorizedError(nil, "Users already exist") 38 } 39 40 // Fetch the params 41 params, err := mux.Params(r) 42 if err != nil { 43 return server.InternalError(err) 44 } 45 46 // Convert the password param to a password_hash 47 hash, err := auth.HashPassword(params.Get("password")) 48 if err != nil { 49 return server.InternalError(err, "Problem hashing password") 50 } 51 52 // Take the details given and create the first user 53 userParams := map[string]string{ 54 "email": params.Get("email"), 55 "password_hash": hash, 56 "name": nameFromEmail(params.Get("email")), 57 "status": "100", 58 "role": "100", 59 "title": "Administrator", 60 } 61 62 user := users.New() 63 uid, err := user.Create(userParams) 64 if err != nil { 65 return server.InternalError(err) 66 } 67 68 user, err = users.Find(uid) 69 if err != nil { 70 return server.InternalError(err, "Error creating user") 71 } 72 // Login this user automatically - save cookie 73 session, err := auth.Session(w, r) 74 if err != nil { 75 log.Info(log.V{"msg": "login failed", "user_id": user.ID, "status": http.StatusInternalServerError}) 76 } 77 78 // Success, log it and set the cookie with user id 79 session.Set(auth.SessionUserKey, fmt.Sprintf("%d", user.ID)) 80 session.Save(w) 81 82 // Log action 83 log.Info(log.V{"msg": "login", "user_email": user.Email, "user_id": user.ID}) 84 85 // Create a welcome home page 86 pageParams := map[string]string{ 87 "status": "100", 88 "name": "Fragmenta", 89 "url": "/", 90 "text": "<section class=\"padded\"><h1>Welcome to Fragmenta</h1><p><a href=\"/pages/1/update\">Edit this page</a></p></section>", 91 } 92 _, err = pages.New().Create(pageParams) 93 if err != nil { 94 return server.InternalError(err) 95 } 96 97 // Create another couple of simple pages as examples (about and privacy) 98 pageParams = map[string]string{ 99 "status": "100", 100 "name": "About Us", 101 "url": "/about", 102 "text": "<section class=\"narrow\"><h1>About us</h1><p>About us</p></section>", 103 } 104 _, err = pages.New().Create(pageParams) 105 if err != nil { 106 return server.InternalError(err) 107 } 108 pageParams = map[string]string{ 109 "status": "100", 110 "name": "Privacy Policy", 111 "url": "/privacy", 112 "text": "<section class=\"narrow\"><h1>Privacy Policy</h1><p>We respect your privacy.</p></section>", 113 } 114 _, err = pages.New().Create(pageParams) 115 if err != nil { 116 return server.InternalError(err) 117 } 118 119 // Redirect to the home page (newly set up we hope) 120 return server.Redirect(w, r, "/") 121 } 122 123 // nameFromEmail grabs a name from an email address 124 func nameFromEmail(e string) string { 125 // Split email on @, and separate by removing . or _ 126 parts := strings.Split(e, "@") 127 if len(parts) > 0 { 128 n := strings.Replace(parts[0], ".", " ", -1) 129 n = strings.Replace(n, "_", " ", -1) 130 return n 131 } 132 133 return e 134 }