github.com/cs3org/reva/v2@v2.27.7/pkg/siteacc/admin/panel.go (about) 1 // Copyright 2018-2020 CERN 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 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package admin 20 21 import ( 22 "net/http" 23 24 "github.com/cs3org/reva/v2/pkg/siteacc/config" 25 "github.com/cs3org/reva/v2/pkg/siteacc/data" 26 "github.com/cs3org/reva/v2/pkg/siteacc/html" 27 "github.com/pkg/errors" 28 "github.com/rs/zerolog" 29 ) 30 31 // Panel represents the web interface panel of the accounts service administration. 32 type Panel struct { 33 html.PanelProvider 34 html.ContentProvider 35 36 htmlPanel *html.Panel 37 } 38 39 const ( 40 templateMain = "main" 41 ) 42 43 func (panel *Panel) initialize(conf *config.Configuration, log *zerolog.Logger) error { 44 // Create the internal HTML panel 45 htmlPanel, err := html.NewPanel("admin-panel", panel, conf, log) 46 if err != nil { 47 return errors.Wrap(err, "unable to create the administration panel") 48 } 49 panel.htmlPanel = htmlPanel 50 51 // Add all templates 52 if err := panel.htmlPanel.AddTemplate(templateMain, panel); err != nil { 53 return errors.Wrap(err, "unable to create the main template") 54 } 55 56 return nil 57 } 58 59 // GetActiveTemplate returns the name of the active template. 60 func (panel *Panel) GetActiveTemplate(*html.Session, string) string { 61 return templateMain 62 } 63 64 // GetTitle returns the title of the htmlPanel. 65 func (panel *Panel) GetTitle() string { 66 return "ScienceMesh Site Administrator Accounts Panel" 67 } 68 69 // GetCaption returns the caption which is displayed on the htmlPanel. 70 func (panel *Panel) GetCaption() string { 71 return "ScienceMesh Site Administrator Accounts ({{.Accounts | len}})" 72 } 73 74 // GetContentJavaScript delivers additional JavaScript code. 75 func (panel *Panel) GetContentJavaScript() string { 76 return tplJavaScript 77 } 78 79 // GetContentStyleSheet delivers additional stylesheet code. 80 func (panel *Panel) GetContentStyleSheet() string { 81 return tplStyleSheet 82 } 83 84 // GetContentBody delivers the actual body content. 85 func (panel *Panel) GetContentBody() string { 86 return tplBody 87 } 88 89 // PreExecute is called before the actual template is being executed. 90 func (panel *Panel) PreExecute(*html.Session, string, http.ResponseWriter, *http.Request) (html.ExecutionResult, error) { 91 return html.ContinueExecution, nil 92 } 93 94 // Execute generates the HTTP output of the htmlPanel and writes it to the response writer. 95 func (panel *Panel) Execute(w http.ResponseWriter, r *http.Request, session *html.Session, accounts *data.Accounts) error { 96 dataProvider := func(*html.Session) interface{} { 97 type TemplateData struct { 98 Accounts *data.Accounts 99 } 100 101 return TemplateData{ 102 Accounts: accounts, 103 } 104 } 105 return panel.htmlPanel.Execute(w, r, session, dataProvider) 106 } 107 108 // NewPanel creates a new administration panel. 109 func NewPanel(conf *config.Configuration, log *zerolog.Logger) (*Panel, error) { 110 panel := &Panel{} 111 if err := panel.initialize(conf, log); err != nil { 112 return nil, errors.Wrap(err, "unable to initialize the administration panel") 113 } 114 return panel, nil 115 }