-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclients.go
247 lines (232 loc) · 6.72 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"net/http"
"time"
)
// Funcion para dar de alta clientes
func nuevoCliente(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// --- we must identify the session user 1st ------------------------
cookie, err := r.Cookie(CookieName)
if err != nil {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
key := cookie.Value
mu_user.RLock()
id, ok := id_[key] // De aquí podemos recoger el id del usuario logeado
mu_user.RUnlock()
if !ok {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
//mu_user.RLock()
//tipo := type_[key]
//user := user_[key]
//mu_user.RUnlock()
// actualizamos la cookie actual
expiration := time.Now().Add(time.Duration(session_timeout) * time.Second)
newcookie := http.Cookie{Name: CookieName, Value: key, Expires: expiration}
http.SetCookie(w, &newcookie)
mu_user.Lock()
time_[key] = expiration
mu_user.Unlock()
// ---- end of session identification -------------------------------
resp := "BAD"
dbgeneral, err := sql.Open("sqlite3", DirDB+"general.db")
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
defer dbgeneral.Close()
var count int
dbgen_mu.RLock()
err = dbgeneral.QueryRow("SELECT count(id) FROM users WHERE username = ", r.FormValue("nom_cli")).Scan(&count)
dbgen_mu.RUnlock()
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
if count > 0 { // username already exists
resp = "DUP"
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
dbgen_mu.Lock()
_, err1 := dblive.Exec("INSERT INTO users (`username`, `password`, `pubpass`, `type`, `status`, `id_recruiter`) VALUES (?, ?, ?, ?, ?)",
r.FormValue("nom_cli"), r.FormValue("passw"), r.FormValue("passw"), r.FormValue("type"), r.FormValue("status"), id)
dbgen_mu.Unlock()
if err1 != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
resp = "OK"
fmt.Fprintf(w, "%s", resp)
}
// Función para crear las opciones de tipos
func types(w http.ResponseWriter, r *http.Request) {
// --- we must identify the session user 1st ------------------------
cookie, err := r.Cookie(CookieName)
if err != nil {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
key := cookie.Value
mu_user.RLock()
tipo, ok := type_[key] // De aquí podemos recoger el id del usuario logeado
mu_user.RUnlock()
if !ok {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
// ---- end of session identification -------------------------------
options := ""
switch tipo {
case 0: // superadmin
options = "<option value=\"1\">Admin</option><option value=\"2\">Distributor</option><option value=\"3\">Publisher</option>"
case 1: // admin
options = "<option value=\"2\">Distributor</option><option value=\"3\">Publisher</option>"
case 2: // distro
options = "<option value=\"3\">Publisher</option>"
}
fmt.Fprintf(w, "%s", options)
}
// Función que selecciona los clientes de la tabla admin
func buscarClientes(w http.ResponseWriter, r *http.Request) {
// --- we must identify the session user 1st ------------------------
cookie, err := r.Cookie(CookieName)
if err != nil {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
key := cookie.Value
mu_user.RLock()
tipo, ok := type_[key]
mu_user.RUnlock()
if !ok {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
mu_user.RLock()
id := type_[key]
//user := user_[key]
mu_user.RUnlock()
// actualizamos la cookie actual
expiration := time.Now().Add(time.Duration(session_timeout) * time.Second)
newcookie := http.Cookie{Name: CookieName, Value: key, Expires: expiration}
http.SetCookie(w, &newcookie)
mu_user.Lock()
time_[key] = expiration
mu_user.Unlock()
// ---- end of session identification -------------------------------
var idn int
var nombre, selector string
dbgeneral, err := sql.Open("sqlite3", DirDB+"general.db")
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", selector)
return
}
defer dbgeneral.Close()
dbgen_mu.RLock()
query, err := dblive.Query("SELECT id, username FROM users WHERE type = ? AND id_recruiter = ?", tipo+1, id)
dbgen_mu.RUnlock()
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", selector)
return
}
for query.Next() {
err = query.Scan(&idn, &nombre)
if err != nil {
Warning.Println(err)
fmt.Fprintf(w, "%s", selector)
return
}
selector = selector + fmt.Sprintf("<option value='%d'>%s</option>", idn, nombre)
}
query.Close()
fmt.Fprintf(w, "%s", selector)
}
// Función que borra un cliente de la tabla admin
func borrarCliente(w http.ResponseWriter, r *http.Request) {
// --- we must identify the session user 1st ------------------------
cookie, err := r.Cookie(CookieName)
if err != nil {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
key := cookie.Value
mu_user.RLock()
tipo, ok := type_[key] // De aquí podemos recoger el id del usuario logeado
mu_user.RUnlock()
if !ok {
http.Redirect(w, r, "/"+first_page+".html", http.StatusFound)
return
}
//mu_user.RLock()
//tipo := type_[key]
//user := user_[key]
//mu_user.RUnlock()
// actualizamos la cookie actual
expiration := time.Now().Add(time.Duration(session_timeout) * time.Second)
newcookie := http.Cookie{Name: CookieName, Value: key, Expires: expiration}
http.SetCookie(w, &newcookie)
mu_user.Lock()
time_[key] = expiration
mu_user.Unlock()
// ---- end of session identification -------------------------------
resp := "BAD"
r.ParseForm()
dbgeneral, err := sql.Open("sqlite3", DirDB+"general.db")
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
defer dbgeneral.Close()
// debemos revisar si quien queremos borrar tiene usuarios a su vez (excepto si es un distro que borra directamente al publisher)
if tipo == 2 { // distro
dbgen_mu.Lock()
_, err = dbgeneral.Exec("DELETE FROM users WHERE id = ?", r.FormValue("clients"))
dbgen_mu.Unlock()
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
resp = "OK"
} else { // admin o superadmin
var count int
dbgen_mu.RLock()
err = dbgeneral.QueryRow("SELECT count(id) FROM users WHERE id_recruiter = ?", r.FormValue("clients")).Scan(&count)
dbgen_mu.RUnlock()
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
if count > 0 { // tiene clientes dependientes (no borrar)
// do nothing
resp = "DUP"
} else { // no tiene clientes, se le borra
dbgen_mu.Lock()
_, err = dbgeneral.Exec("DELETE FROM users WHERE id = ?", r.FormValue("clients"))
dbgen_mu.Unlock()
if err != nil {
Error.Println(err)
fmt.Fprintf(w, "%s", resp)
return
}
resp = "OK"
}
}
fmt.Fprintf(w, "%s", resp)
}