github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/panel/plugins.go (about)

     1  package panel
     2  
     3  import (
     4  	"errors"
     5  	"log"
     6  	"net/http"
     7  
     8  	c "github.com/Azareal/Gosora/common"
     9  )
    10  
    11  func Plugins(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
    12  	bp, ferr := buildBasePage(w, r, u, "plugins", "plugins")
    13  	if ferr != nil {
    14  		return ferr
    15  	}
    16  	if !u.Perms.ManagePlugins {
    17  		return c.NoPermissions(w, r, u)
    18  	}
    19  
    20  	plList, i := make([]interface{}, len(c.Plugins)), 0
    21  	for _, pl := range c.Plugins {
    22  		plList[i] = pl
    23  		i++
    24  	}
    25  
    26  	return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "", "", "panel_plugins", c.PanelPage{bp, plList, nil}})
    27  }
    28  
    29  // TODO: Abstract more of the plugin activation / installation / deactivation logic, so we can test all that more reliably and easily
    30  func PluginsActivate(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
    31  	_, ferr := c.SimplePanelUserCheck(w, r, u)
    32  	if ferr != nil {
    33  		return ferr
    34  	}
    35  	if !u.Perms.ManagePlugins {
    36  		return c.NoPermissions(w, r, u)
    37  	}
    38  
    39  	pl, ok := c.Plugins[uname]
    40  	if !ok {
    41  		return c.LocalError("The plugin isn't registered in the system", w, r, u)
    42  	}
    43  	if pl.Installable && !pl.Installed {
    44  		return c.LocalError("You can't activate this plugin without installing it first", w, r, u)
    45  	}
    46  
    47  	active, err := pl.BypassActive()
    48  	hasPlugin, err2 := pl.InDatabase()
    49  	if err != nil || err2 != nil {
    50  		return c.InternalError(err, w, r)
    51  	}
    52  
    53  	if pl.Activate != nil {
    54  		err = pl.Activate(pl)
    55  		if err != nil {
    56  			return c.LocalError(err.Error(), w, r, u)
    57  		}
    58  	}
    59  
    60  	if hasPlugin {
    61  		if active {
    62  			return c.LocalError("The plugin is already active", w, r, u)
    63  		}
    64  		err = pl.SetActive(true)
    65  	} else {
    66  		err = pl.AddToDatabase(true, false)
    67  	}
    68  	if err != nil {
    69  		return c.InternalError(err, w, r)
    70  	}
    71  
    72  	log.Printf("Activating plugin '%s'", pl.Name)
    73  	err = pl.Init(pl)
    74  	if err != nil {
    75  		return c.LocalError(err.Error(), w, r, u)
    76  	}
    77  	err = c.AdminLogs.CreateExtra("activate", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
    78  	if err != nil {
    79  		return c.InternalError(err, w, r)
    80  	}
    81  
    82  	http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
    83  	return nil
    84  }
    85  
    86  func PluginsDeactivate(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
    87  	_, ferr := c.SimplePanelUserCheck(w, r, u)
    88  	if ferr != nil {
    89  		return ferr
    90  	}
    91  	if !u.Perms.ManagePlugins {
    92  		return c.NoPermissions(w, r, u)
    93  	}
    94  
    95  	pl, ok := c.Plugins[uname]
    96  	if !ok {
    97  		return c.LocalError("The plugin isn't registered in the system", w, r, u)
    98  	}
    99  	log.Printf("plugin: %+v\n", pl)
   100  
   101  	active, err := pl.BypassActive()
   102  	if err != nil {
   103  		return c.InternalError(err, w, r)
   104  	} else if !active {
   105  		return c.LocalError("The plugin you're trying to deactivate isn't active", w, r, u)
   106  	}
   107  
   108  	err = pl.SetActive(false)
   109  	if err != nil {
   110  		return c.InternalError(err, w, r)
   111  	}
   112  	if pl.Deactivate != nil {
   113  		pl.Deactivate(pl)
   114  	}
   115  	err = c.AdminLogs.CreateExtra("deactivate", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
   116  	if err != nil {
   117  		return c.InternalError(err, w, r)
   118  	}
   119  
   120  	http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
   121  	return nil
   122  }
   123  
   124  func PluginsInstall(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
   125  	_, ferr := c.SimplePanelUserCheck(w, r, u)
   126  	if ferr != nil {
   127  		return ferr
   128  	}
   129  	if !u.Perms.ManagePlugins {
   130  		return c.NoPermissions(w, r, u)
   131  	}
   132  
   133  	pl, ok := c.Plugins[uname]
   134  	if !ok {
   135  		return c.LocalError("The plugin isn't registered in the system", w, r, u)
   136  	}
   137  	if !pl.Installable {
   138  		return c.LocalError("This plugin is not installable", w, r, u)
   139  	}
   140  	if pl.Installed {
   141  		return c.LocalError("This plugin has already been installed", w, r, u)
   142  	}
   143  
   144  	active, err := pl.BypassActive()
   145  	hasPlugin, err2 := pl.InDatabase()
   146  	if err != nil || err2 != nil {
   147  		return c.InternalError(err, w, r)
   148  	}
   149  	if active {
   150  		return c.InternalError(errors.New("An uninstalled plugin is still active"), w, r)
   151  	}
   152  
   153  	if pl.Install != nil {
   154  		err = pl.Install(pl)
   155  		if err != nil {
   156  			return c.LocalError(err.Error(), w, r, u)
   157  		}
   158  	}
   159  
   160  	if pl.Activate != nil {
   161  		err = pl.Activate(pl)
   162  		if err != nil {
   163  			return c.LocalError(err.Error(), w, r, u)
   164  		}
   165  	}
   166  
   167  	if hasPlugin {
   168  		err = pl.SetInstalled(true)
   169  		if err != nil {
   170  			return c.InternalError(err, w, r)
   171  		}
   172  		err = pl.SetActive(true)
   173  	} else {
   174  		err = pl.AddToDatabase(true, true)
   175  	}
   176  	if err != nil {
   177  		return c.InternalError(err, w, r)
   178  	}
   179  
   180  	log.Printf("Installing plugin '%s'", pl.Name)
   181  	err = pl.Init(pl)
   182  	if err != nil {
   183  		return c.LocalError(err.Error(), w, r, u)
   184  	}
   185  	err = c.AdminLogs.CreateExtra("install", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
   186  	if err != nil {
   187  		return c.InternalError(err, w, r)
   188  	}
   189  
   190  	http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
   191  	return nil
   192  }