0 package gmi
1
2 import (
3 "gemigit/db"
4 "gemigit/repo"
5 "github.com/pitr/gig"
6 )
7
8 func ChangeDesc(c gig.Context) error {
9 newdesc, err := c.QueryString()
10 if err != nil {
11 return c.NoContent(gig.StatusBadRequest,
12 "Invalid input received")
13 }
14 if newdesc == "" {
15 return c.NoContent(gig.StatusInput,
16 "New account description")
17 }
18 user, exist := db.GetUser(c.CertHash())
19 if !exist {
20 return c.NoContent(gig.StatusBadRequest,
21 "Invalid username")
22 }
23 if err := user.ChangeDescription(newdesc, c.CertHash());
24 err != nil {
25 return c.NoContent(gig.StatusBadRequest, err.Error())
26 }
27 return c.NoContent(gig.StatusRedirectTemporary, "/account")
28 }
29
30 func AddRepo(c gig.Context) error {
31 name, err := c.QueryString()
32 if err != nil {
33 return c.NoContent(gig.StatusBadRequest, err.Error())
34 }
35 if name == "" {
36 return c.NoContent(gig.StatusInput, "Repository name")
37 }
38 user, b := db.GetUser(c.CertHash())
39 if !b {
40 return c.NoContent(gig.StatusBadRequest,
41 "Cannot find username")
42 }
43 if err := user.CreateRepo(name, c.CertHash());
44 err != nil {
45 return c.NoContent(gig.StatusBadRequest,
46 err.Error())
47 }
48 if err := repo.InitRepo(name, user.Name); err != nil {
49 return c.NoContent(gig.StatusBadRequest,
50 err.Error())
51 }
52 return c.NoContent(gig.StatusRedirectTemporary,
53 "/account/repo/" + name)
54 }
55
56 func AddGroup(c gig.Context) error {
57 name, err := c.QueryString()
58 if err != nil {
59 return c.NoContent(gig.StatusBadRequest, err.Error())
60 }
61 if name == "" {
62 return c.NoContent(gig.StatusInput, "Group name")
63 }
64 user, b := db.GetUser(c.CertHash())
65 if !b {
66 return c.NoContent(gig.StatusBadRequest,
67 "Cannot find username")
68 }
69 if err := user.CreateGroup(name, c.CertHash());
70 err != nil {
71 return c.NoContent(gig.StatusBadRequest,
72 err.Error())
73 }
74 return c.NoContent(gig.StatusRedirectTemporary,
75 "/account/groups/" + name)
76 }
77
78 func ChangePassword(c gig.Context) error {
79 passwd, err := c.QueryString()
80 if err != nil {
81 return c.NoContent(gig.StatusBadRequest,
82 "Invalid input received")
83 }
84 if passwd == "" {
85 return c.NoContent(gig.StatusSensitiveInput, "New password")
86 }
87 user, b := db.GetUser(c.CertHash())
88 if !b {
89 return c.NoContent(gig.StatusBadRequest,
90 "Cannot find username")
91 }
92 err = user.ChangePassword(passwd, c.CertHash())
93 if err != nil {
94 return c.NoContent(gig.StatusBadRequest, err.Error())
95 }
96 return c.NoContent(gig.StatusRedirectTemporary, "/account")
97 }
98
99 func Disconnect(c gig.Context) error {
100 user, exist := db.GetUser(c.CertHash())
101 if !exist {
102 return c.NoContent(gig.StatusBadRequest, "Invalid username")
103 }
104 if err := user.Disconnect(c.CertHash()); err != nil {
105 return c.NoContent(gig.StatusBadRequest, err.Error())
106 }
107 return c.NoContent(gig.StatusRedirectTemporary, "/")
108 }
109
110 func DisconnectAll(c gig.Context) error {
111 user, exist := db.GetUser(c.CertHash())
112 if !exist {
113 return c.NoContent(gig.StatusBadRequest, "Invalid username")
114 }
115 if err := user.DisconnectAll(c.CertHash()); err != nil {
116 return c.NoContent(gig.StatusBadRequest, err.Error())
117 }
118 return c.NoContent(gig.StatusRedirectTemporary, "/account")
119 }
120