github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/user/user.go (about) 1 // Copyright 2023 IAC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package user 16 17 import ( 18 "fmt" 19 "strconv" 20 "time" 21 22 //"log" 23 "net/http" 24 25 "github.com/gin-gonic/gin" 26 "github.com/mdaxf/iac/controllers/common" 27 "github.com/mdaxf/iac/logger" 28 ) 29 30 type UserController struct{} 31 32 func (c *UserController) Login(ctx *gin.Context) { 33 // Retrieve a list of users from the database 34 log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "UserController"} 35 startTime := time.Now() 36 defer func() { 37 elapsed := time.Since(startTime) 38 log.PerformanceWithDuration("controllers.user.login", elapsed) 39 }() 40 /* 41 defer func() { 42 if err := recover(); err != nil { 43 log.Error(fmt.Sprintf("login defer error: %s", err)) 44 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 45 } 46 }() 47 */ 48 log.Debug("Login handle function is called.") 49 50 var user LoginUserData 51 if err := ctx.BindJSON(&user); err != nil { 52 log.Error(fmt.Sprintf("Login error:%s", err.Error())) 53 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 54 return 55 } 56 57 username := user.Username 58 password := user.Password 59 token := user.Token 60 ClientID := user.ClientID 61 Renew := user.Renew 62 63 log.Debug(fmt.Sprintf("Login:%s %s token: %s renew:%s", username, password, token, Renew)) 64 65 execLogin(ctx, username, password, token, ClientID, Renew) 66 67 /* 68 //log.Println(fmt.Sprintf("Database open connection:%d", &dbconn.DB.Stats().OpenConnections)) 69 querystr := fmt.Sprintf("SELECT ID,Name,FamilyName FROM EMPLOYEE WHERE LoginName='%s'", username) 70 log.Println(fmt.Sprintf("query:%s", querystr)) 71 rows, err := dbconn.DB.Query(querystr) 72 if err != nil { 73 panic(err.Error()) 74 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 75 return 76 //panic(err.Error()) 77 } 78 defer rows.Close() 79 80 log.Println(fmt.Printf("rows: %v\n", rows)) 81 82 for rows.Next() { 83 var ID int 84 var Name string 85 var FamilyName string 86 87 err = rows.Scan(&ID, &Name, &FamilyName) 88 if err != nil { 89 panic(err.Error()) 90 } 91 log.Println(fmt.Sprintf("ID:%d Name:%s FamilyName:%s", ID, Name, FamilyName)) 92 93 user := User{ID: ID, Username: Name + " " + FamilyName, Email: "", Password: password, SessionID: uuid.New()} 94 95 exist, err := config.SessionCache.IsExist(ctx, "USER_"+string(rune(ID))) 96 97 if err != nil && exist { 98 config.SessionCache.Delete(ctx, "USER_"+string(rune(ID))) 99 100 } 101 config.SessionCache.Put(ctx, "USER_"+string(rune(ID)), user, 10*time.Minute) 102 103 ctx.JSON(http.StatusOK, user) 104 return 105 } 106 107 ctx.JSON(http.StatusNotFound, "Login failed") 108 */ 109 110 } 111 112 func (c *UserController) Image(ctx *gin.Context) { 113 log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "UserController"} 114 115 startTime := time.Now() 116 defer func() { 117 elapsed := time.Since(startTime) 118 log.PerformanceWithDuration("controllers.user.Image", elapsed) 119 }() 120 121 defer func() { 122 if err := recover(); err != nil { 123 log.Error(fmt.Sprintf("Image defer error: %s", err)) 124 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 125 } 126 }() 127 128 _, userno, clientid, err := common.GetRequestUser(ctx) 129 if err != nil { 130 log.Error(fmt.Sprintf("GetRequestUser error: %s", err)) 131 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 132 return 133 } 134 135 log.User = userno 136 log.ClientID = clientid 137 138 log.Debug("Get User Image handle function is called.") 139 140 log.Debug(fmt.Sprintf("Get User Image:%s", ctx.Param("username"))) 141 142 username := ctx.Query("username") 143 /*var user LoginUserData 144 if err := ctx.BindJSON(&user); err != nil { 145 log.Error(fmt.Sprintf("Get User Image error:%s", err.Error())) 146 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 147 return 148 } 149 150 username := user.Username */ 151 152 log.Debug(fmt.Sprintf("Get User Image:%s", username)) 153 154 PictureUrl, err := getUserImage(username, clientid) 155 156 if err != nil { 157 log.Error(fmt.Sprintf("Get User Image error:%s", err.Error())) 158 PictureUrl = "images/avatardefault.png" 159 } 160 161 if PictureUrl == "" { 162 PictureUrl = "images/avatardefault.png" 163 } 164 165 log.Debug(fmt.Sprintf("Get User Image:%s", PictureUrl)) 166 ctx.JSON(http.StatusOK, PictureUrl) 167 } 168 169 func (c *UserController) Logout(ctx *gin.Context) { 170 // Retrieve a list of users from the database 171 log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "UserController"} 172 startTime := time.Now() 173 defer func() { 174 elapsed := time.Since(startTime) 175 log.PerformanceWithDuration("controllers.user.Logout", elapsed) 176 }() 177 178 defer func() { 179 if err := recover(); err != nil { 180 log.Error(fmt.Sprintf("Logout defer error: %s", err)) 181 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 182 } 183 }() 184 185 // Send the list of users in the response 186 var user LoginUserData 187 if err := ctx.BindJSON(&user); err != nil { 188 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 189 return 190 } 191 192 //userID := user.ID 193 token := user.Token 194 execLogout(ctx, token) 195 ctx.JSON(http.StatusOK, "Logoutsessionid") 196 } 197 198 func (c *UserController) ChangePassword(ctx *gin.Context) { 199 log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "UserController"} 200 startTime := time.Now() 201 defer func() { 202 elapsed := time.Since(startTime) 203 log.PerformanceWithDuration("controllers.user.ChangePassword", elapsed) 204 }() 205 206 defer func() { 207 if err := recover(); err != nil { 208 log.Error(fmt.Sprintf("ChangePassword defer error: %s", err)) 209 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 210 } 211 }() 212 _, userno, clientid, err := common.GetRequestUser(ctx) 213 if err != nil { 214 log.Error(fmt.Sprintf("GetRequestUser error: %s", err)) 215 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 216 return 217 } 218 219 log.User = userno 220 log.ClientID = clientid 221 222 log.Debug("Change Password handle function is called.") 223 224 var user ChangePwdData 225 if err := ctx.BindJSON(&user); err != nil { 226 log.Error(fmt.Sprintf("Change Password handle function error:%s", err.Error())) 227 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 228 return 229 } 230 231 username := user.Username 232 oldpassword := user.OldPassword 233 newpassword := user.NewPassword 234 235 log.Debug(fmt.Sprintf("Change Password:%s %s %s", username, oldpassword, newpassword)) 236 237 execChangePassword(ctx, username, oldpassword, newpassword, clientid) 238 } 239 240 func (c *UserController) UserMenus(ctx *gin.Context) { 241 log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "UserController"} 242 startTime := time.Now() 243 defer func() { 244 elapsed := time.Since(startTime) 245 log.PerformanceWithDuration("controllers.user.UserMenus", elapsed) 246 }() 247 248 defer func() { 249 if err := recover(); err != nil { 250 log.Error(fmt.Sprintf("UserMenus defer error: %s", err)) 251 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 252 } 253 }() 254 _, userno, clientid, err := common.GetRequestUser(ctx) 255 if err != nil { 256 log.Error(fmt.Sprintf("GetRequestUser error: %s", err)) 257 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 258 return 259 } 260 261 log.User = userno 262 log.ClientID = clientid 263 log.Debug("Get User menus handle function is called.") 264 265 log.Debug(fmt.Sprintf("Get User menus:%s", ctx.Param("username"))) 266 267 userID := ctx.Query("userid") 268 Mobile := ctx.Query("mobile") 269 parentID := ctx.Query("parentid") 270 271 log.Debug(fmt.Sprintf("Get User menus:%s, %s", userID, Mobile)) 272 273 isMobile := false 274 if Mobile == "1" { 275 isMobile = true 276 } 277 num, err := strconv.Atoi(userID) 278 if err != nil { 279 log.Error(fmt.Sprintf("Get User menus error:%s", err.Error())) 280 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 281 return 282 } 283 284 num1, err := strconv.Atoi(parentID) 285 if err != nil { 286 log.Error(fmt.Sprintf("Get User menus error:%s", err.Error())) 287 num1 = -1 288 } 289 290 jdata, err := getUserMenus(num, isMobile, num1, userno, clientid) 291 292 if err != nil { 293 log.Error(fmt.Sprintf("Get User menus error: %s", err.Error())) 294 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 295 return 296 } 297 298 log.Debug(fmt.Sprintf("Get User menus:%s", jdata)) 299 ctx.JSON(http.StatusOK, jdata) 300 } 301 302 func (c *UserController) List(ctx *gin.Context) { 303 // Retrieve a list of users from the database 304 users := []User{ /* ... */ } 305 306 // Send the list of users in the response 307 ctx.JSON(http.StatusOK, users) 308 } 309 310 func (c *UserController) Create(ctx *gin.Context) { 311 // Retrieve user data from the request body 312 var user User 313 if err := ctx.BindJSON(&user); err != nil { 314 ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid user data"}) 315 return 316 } 317 318 // Save the user data to the database 319 if err := SaveUser(&user); err != nil { 320 ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to save user data"}) 321 return 322 } 323 324 // Send the saved user data in the response 325 ctx.JSON(http.StatusOK, user) 326 } 327 328 func SaveUser(user *User) error { 329 // Save the user data to the database 330 return nil 331 }