Преглед на файлове

Added better callback from authreq.html

aroz преди 1 година
родител
ревизия
4a796d5f71
променени са 6 файла, в които са добавени 47 реда и са изтрити 67 реда
  1. 6 3
      auth.go
  2. 4 54
      hardware.power.go
  3. 8 1
      mod/disk/raid/handler.go
  4. 5 0
      mod/disk/raid/raidutils.go
  5. 15 6
      web/SystemAO/disk/raid/newdisk.html
  6. 9 3
      web/SystemAO/security/authreq.html

+ 6 - 3
auth.go

@@ -160,20 +160,23 @@ func AuthValidateSecureRequest(w http.ResponseWriter, r *http.Request) bool {
 	}
 
 	if !userinfo.IsAdmin() {
-		utils.SendErrorResponse(w, "Permission Denied")
+		w.WriteHeader(http.StatusForbidden)
+		w.Write([]byte("403 Forbidden"))
 		return false
 	}
 
 	//Double check password for this user
 	password, err := utils.PostPara(r, "password")
 	if err != nil {
-		utils.SendErrorResponse(w, "Password Incorrect")
+		w.WriteHeader(http.StatusUnauthorized)
+		w.Write([]byte("401 Unauthorized"))
 		return false
 	}
 
 	passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)
 	if !passwordCorrect {
-		utils.SendErrorResponse(w, rejectionReason)
+		w.WriteHeader(http.StatusForbidden)
+		w.Write([]byte(rejectionReason))
 		return false
 	}
 

+ 4 - 54
hardware.power.go

@@ -38,33 +38,8 @@ func hardware_power_checkIfHardware(w http.ResponseWriter, r *http.Request) {
 }
 
 func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
-	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
-	if err != nil {
-		w.WriteHeader(http.StatusUnauthorized)
-		w.Write([]byte("401 Unauthorized"))
-		return
-	}
-
-	if !userinfo.IsAdmin() {
-		utils.SendErrorResponse(w, "Permission Denied")
-		return
-	}
-
-	if !sudo_mode {
-		utils.SendErrorResponse(w, "Sudo mode required")
-		return
-	}
-
-	//Double check password for this user
-	password, err := utils.PostPara(r, "password")
-	if err != nil {
-		utils.SendErrorResponse(w, "Password Incorrect")
-		return
-	}
-
-	passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)
-	if !passwordCorrect {
-		utils.SendErrorResponse(w, rejectionReason)
+	//validate password using authreq.html
+	if !AuthValidateSecureRequest(w, r) {
 		return
 	}
 
@@ -105,33 +80,8 @@ func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
 }
 
 func hardware_power_restart(w http.ResponseWriter, r *http.Request) {
-	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
-	if err != nil {
-		w.WriteHeader(http.StatusUnauthorized)
-		w.Write([]byte("401 Unauthorized"))
-		return
-	}
-
-	if !userinfo.IsAdmin() {
-		utils.SendErrorResponse(w, "Permission Denied")
-		return
-	}
-
-	if !sudo_mode {
-		utils.SendErrorResponse(w, "Sudo mode required")
-		return
-	}
-
-	//Double check password for this user
-	password, err := utils.PostPara(r, "password")
-	if err != nil {
-		utils.SendErrorResponse(w, "Password Incorrect")
-		return
-	}
-
-	passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)
-	if !passwordCorrect {
-		utils.SendErrorResponse(w, rejectionReason)
+	//Validate password using authreq.html
+	if !AuthValidateSecureRequest(w, r) {
 		return
 	}
 

+ 8 - 1
mod/disk/raid/handler.go

@@ -116,7 +116,14 @@ func (m *Manager) HandleAddDiskToRAIDVol(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	//OK! Add it to the target RAID array
+	//OK! Clear the disk
+	err = m.ClearSuperblock(sdXDev)
+	if err != nil {
+		utils.SendErrorResponse(w, "unable to clear superblock of device")
+		return
+	}
+
+	//Add it to the target RAID array
 	err = m.AddDisk(mdDev, sdXDev)
 	if err != nil {
 		utils.SendErrorResponse(w, "adding disk to RAID volume failed")

+ 5 - 0
mod/disk/raid/raidutils.go

@@ -85,6 +85,11 @@ func (m *Manager) ClearSuperblock(devicePath string) error {
 	if isMounted {
 		return errors.New("target device is mounted. Make sure it is unmounted before clearing")
 	}
+
+	//Make sure there are /dev/ in front of the device path
+	if !strings.HasPrefix(devicePath, "/dev/") {
+		devicePath = filepath.Join("/dev/", devicePath)
+	}
 	cmd := exec.Command("sudo", "mdadm", "--zero-superblock", devicePath)
 
 	err = cmd.Run()

+ 15 - 6
web/SystemAO/disk/raid/newdisk.html

@@ -161,13 +161,20 @@
             }
 
             window.handleDiskAddCallback = function(data){
-                //Disk added. Handle callback to parent 
-                if (ao_module_hasParentCallback()){
-                    ao_module_parentCallback(data);
-                }
+                if (data.error != undefined){
+                    alert(data.error);
+                }else{
+                    //Disk added. Handle callback to parent 
+                    if (ao_module_hasParentCallback()){
+                        ao_module_parentCallback(data);
+                    }
 
-                //Operation completed. 
-                ao_module_close();
+                    //Operation completed. 
+                    setTimeout(function(){
+                        ao_module_close();
+                    }, 300);
+                }
+               
             }
 
 
@@ -239,6 +246,8 @@
                                 </div>
                             </div>`);
                             
+                           
+
                             if (!notUsable){
                                 $("#usablediskSelectionList").append(diskDOM);
                             }else{

+ 9 - 3
web/SystemAO/security/authreq.html

@@ -126,11 +126,12 @@
                 method: method,
                 success: function(data){
                     if (data.error != undefined){
-                        $("#password").parent().addClass("error");
-                        if ($("#username").parent().hasClass("disabled") == false){
-                            $("#username").parent().addClass("error");
+                        //callback to parent with error code
+                        if (ao_module_hasParentCallback()){
+                            ao_module_parentCallback(data);
                         }
 
+                        ao_module_close();
                     }else{
                         if (ao_module_hasParentCallback()){
                             ao_module_parentCallback(data);
@@ -143,6 +144,11 @@
                         ao_module_close();
                     }
                     
+                }, error: function(){
+                    $("#password").parent().addClass("error");
+                    if ($("#username").parent().hasClass("disabled") == false){
+                        $("#username").parent().addClass("error");
+                    }
                 }
             })
        }