github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/ui/params.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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 ui
    16  
    17  import (
    18  	"fmt"
    19  )
    20  
    21  // NavigationItem represents side navigation menu item.
    22  type NavigationItem struct {
    23  	Name     string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"`
    24  	Path     string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"`
    25  	Active   bool   `json:"active,omitempty" xml:"active,omitempty" yaml:"active,omitempty"`
    26  	IconName string `json:"icon_name,omitempty" xml:"icon_name,omitempty" yaml:"icon_name,omitempty"`
    27  }
    28  
    29  // Parameters represent a common set of configuration settings
    30  // for HTML UI.
    31  type Parameters struct {
    32  	Theme                   string            `json:"theme,omitempty" xml:"theme,omitempty" yaml:"theme,omitempty"`
    33  	Templates               map[string]string `json:"templates,omitempty" xml:"templates,omitempty" yaml:"templates,omitempty"`
    34  	AllowRoleSelection      bool              `json:"allow_role_selection,omitempty" xml:"allow_role_selection,omitempty" yaml:"allow_role_selection,omitempty"`
    35  	Title                   string            `json:"title,omitempty" xml:"title,omitempty" yaml:"title,omitempty"`
    36  	LogoURL                 string            `json:"logo_url,omitempty" xml:"logo_url,omitempty" yaml:"logo_url,omitempty"`
    37  	LogoDescription         string            `json:"logo_description,omitempty" xml:"logo_description,omitempty" yaml:"logo_description,omitempty"`
    38  	MetaTitle               string            `json:"meta_title,omitempty" xml:"meta_title,omitempty" yaml:"meta_title,omitempty"`
    39  	MetaDescription         string            `json:"meta_description,omitempty" xml:"meta_description,omitempty" yaml:"meta_description,omitempty"`
    40  	MetaAuthor              string            `json:"meta_author,omitempty" xml:"meta_author,omitempty" yaml:"meta_author,omitempty"`
    41  	PrivateLinks            []Link            `json:"private_links,omitempty" xml:"private_links,omitempty" yaml:"private_links,omitempty"`
    42  	AutoRedirectURL         string            `json:"auto_redirect_url,omitempty" xml:"auto_redirect_url,omitempty" yaml:"auto_redirect_url,omitempty"`
    43  	Realms                  []UserRealm       `json:"realms,omitempty" xml:"realms,omitempty" yaml:"realms,omitempty"`
    44  	PasswordRecoveryEnabled bool              `json:"password_recovery_enabled,omitempty" xml:"password_recovery_enabled,omitempty" yaml:"password_recovery_enabled,omitempty"`
    45  	CustomCSSPath           string            `json:"custom_css_path,omitempty" xml:"custom_css_path,omitempty" yaml:"custom_css_path,omitempty"`
    46  	CustomJsPath            string            `json:"custom_js_path,omitempty" xml:"custom_js_path,omitempty" yaml:"custom_js_path,omitempty"`
    47  	CustomHTMLHeaderPath    string            `json:"custom_html_header_path,omitempty" xml:"custom_html_header_path,omitempty" yaml:"custom_html_header_path,omitempty"`
    48  	StaticAssets            []StaticAsset     `json:"static_assets,omitempty" xml:"static_assets,omitempty" yaml:"static_assets,omitempty"`
    49  	Language                string            `json:"language,omitempty" xml:"language,omitempty" yaml:"language,omitempty"`
    50  	DisabledPages           map[string]bool   `json:"disabled_pages,omitempty" xml:"disabled_pages,omitempty" yaml:"disabled_pages,omitempty"`
    51  }
    52  
    53  // DisablePage disables a specific page.
    54  func (p *Parameters) DisablePage(args []string) error {
    55  	pages := map[string]bool{
    56  		"settings": true,
    57  	}
    58  
    59  	if len(args) < 2 {
    60  		return fmt.Errorf("invalid syntax: too few arguments")
    61  	}
    62  
    63  	if _, exists := pages[args[0]]; !exists {
    64  		return fmt.Errorf("invalid syntax: %s is not supported", args[0])
    65  	}
    66  
    67  	if p.DisabledPages == nil {
    68  		p.DisabledPages = make(map[string]bool)
    69  	}
    70  
    71  	for _, arg := range args[1:] {
    72  		p.DisabledPages[args[0]+"/"+arg] = true
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // IsDisabledPage checks whether a specific page is disabled.
    79  func (p *Parameters) IsDisabledPage(s string) bool {
    80  	if p.DisabledPages == nil {
    81  		return false
    82  	}
    83  	if _, exists := p.DisabledPages[s]; !exists {
    84  		return false
    85  	}
    86  	return true
    87  }
    88  
    89  // GetNavigationItems return items for nav menu.
    90  func (p *Parameters) GetNavigationItems(s string) []*NavigationItem {
    91  	var navItems []*NavigationItem
    92  	for _, entry := range []string{
    93  		"settings/",
    94  		"settings/sshkeys",
    95  		"settings/gpgkeys",
    96  		"settings/apikeys",
    97  		"settings/mfa",
    98  		"settings/password",
    99  		"settings/connected",
   100  	} {
   101  		if p.IsDisabledPage(entry) {
   102  			continue
   103  		}
   104  		navItem := &NavigationItem{
   105  			Path: "/" + entry,
   106  		}
   107  		if s == entry {
   108  			navItem.Active = true
   109  		}
   110  		switch entry {
   111  		case "settings/":
   112  			navItem.Name = "Profile"
   113  			navItem.IconName = "las la-user-circle"
   114  		case "settings/sshkeys":
   115  			navItem.Name = "SSH Keys"
   116  			navItem.IconName = "las la-server"
   117  		case "settings/gpgkeys":
   118  			navItem.Name = "GPG Keys"
   119  			navItem.IconName = "las la-key"
   120  		case "settings/apikeys":
   121  			navItem.Name = "API Keys"
   122  			navItem.IconName = "las la-stream"
   123  		case "settings/mfa":
   124  			navItem.Name = "MFA"
   125  			navItem.IconName = "las la-microchip"
   126  		case "settings/password":
   127  			navItem.Name = "Password"
   128  			navItem.IconName = "las la-fingerprint"
   129  		case "settings/connected":
   130  			navItem.Name = "Connected Accounts"
   131  			navItem.IconName = "las la-share-alt"
   132  		}
   133  		navItems = append(navItems, navItem)
   134  	}
   135  	return navItems
   136  }