@@ -150,3 +150,64 @@ func (controller *Controller) DeletePermanentlyByID(c echo.Context) error {
150150 log .Info ("END request method GET for admin delete permanently by id: [+]success" )
151151 return c .Redirect (http .StatusMovedPermanently , "/admin/delete-permanently" )
152152}
153+
154+ /*
155+ * Restore User
156+ *
157+ * @target: [Admin] Restore User
158+ * @method: GET
159+ * @route: /admin/restore/:id
160+ */
161+ func (controller * Controller ) RestoreUser (c echo.Context ) error {
162+ session , _ := middleware .GetAuth (c )
163+ log := log .WithFields (log.Fields {
164+ "username" : session .Values ["username" ],
165+ "route" : c .Path (),
166+ })
167+ log .Info ("START request method GET for admin restore" )
168+
169+ is_auth_type := session .Values ["is_auth_type" ]
170+ if is_auth_type == - 1 {
171+ log .Warn ("for GET to admin restore without no-session [@route: /login]" )
172+ middleware .SetFlashError (c , "login process failed!" )
173+ log .Warn ("END request method GET for admin restore: [-]failure" )
174+ return c .Redirect (http .StatusFound , "/login" )
175+ }
176+
177+ id , _ := strconv .Atoi (c .Param ("id" ))
178+
179+ // why?
180+ // delete permanently not for admin
181+ if id == 1 {
182+ log .Warn ("END request method GET for admin restore [admin]: [-]failure" )
183+ // HTTP response status: 403 Forbidden
184+ return c .HTML (http .StatusForbidden , "403 Forbidden" )
185+ }
186+
187+ if ! middleware .IsAdmin (is_auth_type ) {
188+ log .Warn ("END request method GET for admin restore: [-]failure" )
189+ // HTTP response status: 404 Not Found
190+ return c .HTML (http .StatusNotFound , "404 Not Found" )
191+ }
192+
193+ user , err := (models.User {}).UnscopedFirstUserByID (controller .DB , id )
194+ if err != nil {
195+ log .Warnf ("for GET to admin restore without models.User{}.FirstByID() errors: `%v`" , err )
196+ log .Warn ("END request method GET for admin restore: [-]failure" )
197+ // HTTP response status: 404 Not Found
198+ return c .HTML (http .StatusNotFound , err .Error ())
199+ }
200+
201+ if err := user .Restore (controller .DB , id ); err != nil {
202+ log .Warnf ("for GET to admin restore without models.User{}.Restore() errors: `%v`" , err )
203+ log .Warn ("END request method GET for admin restore: [-]failure" )
204+ // HTTP response status: 403 Forbidden
205+ return c .HTML (http .StatusForbidden , err .Error ())
206+ }
207+
208+ middleware .SetFlashSuccess (c , fmt .Sprintf ("success restore user: %s!" , user .Username ))
209+
210+ // restore admin
211+ log .Info ("END request method GET for admin restore: [+]success" )
212+ return c .Redirect (http .StatusMovedPermanently , "/admin/delete-permanently" )
213+ }
0 commit comments