|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# --- BEGIN_HEADER --- |
| 5 | +# |
| 6 | +# account - account page with info and account management options |
| 7 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 8 | +# |
| 9 | +# This file is part of MiG. |
| 10 | +# |
| 11 | +# MiG is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation; either version 2 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# MiG is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | +# |
| 25 | +# -- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | + |
| 29 | +"""Account page with user details and account management options""" |
| 30 | + |
| 31 | +from __future__ import absolute_import |
| 32 | + |
| 33 | +import datetime |
| 34 | +import os |
| 35 | + |
| 36 | +from mig.shared import returnvalues |
| 37 | +from mig.shared.functional import validate_input_and_cert |
| 38 | +from mig.shared.init import initialize_main_variables, find_entry |
| 39 | +from mig.shared.htmlgen import html_user_messages, man_base_html, man_base_js |
| 40 | +from mig.shared.useradm import get_full_user_map |
| 41 | + |
| 42 | +_account_field_order = [('full_name', 'Full Name'), |
| 43 | + ('organization', 'Organization'), |
| 44 | + ('email', 'Email Address'), |
| 45 | + ('country', 'Country'), |
| 46 | + ('role', 'Role'), |
| 47 | + ('status', 'Account Status'), |
| 48 | + ('expire', 'Expire'), |
| 49 | + ('peers_full_name', 'Peer Full Name(s)'), |
| 50 | + ('peers_email', 'Peer Email Address(es)'), |
| 51 | + ] |
| 52 | + |
| 53 | + |
| 54 | +def html_tmpl(configuration, client_id, environ, title_entry): |
| 55 | + """HTML page base: some account and manage actions depend on configuration |
| 56 | + and environ. |
| 57 | + """ |
| 58 | + |
| 59 | + user_msg, show_user_msg = '', 'hidden' |
| 60 | + if configuration.site_enable_user_messages: |
| 61 | + user_msg = html_user_messages(configuration, client_id) |
| 62 | + show_user_msg = '' |
| 63 | + user_map = get_full_user_map(configuration) |
| 64 | + user_dict = user_map.get(client_id, None) |
| 65 | + user_account = '' |
| 66 | + if user_dict: |
| 67 | + user_account += ''' |
| 68 | + <h3>Account Details</h3> |
| 69 | + <p class="sub-title">Your account has the following information |
| 70 | + registered: |
| 71 | + </p> |
| 72 | + ''' |
| 73 | + for (field, label) in _account_field_order: |
| 74 | + if not user_dict.get(field, False): |
| 75 | + continue |
| 76 | + if field == 'expire': |
| 77 | + # NOTE: translate epoch to proper datetime string |
| 78 | + expire_dt = datetime.datetime.fromtimestamp(user_dict[field]) |
| 79 | + user_dict[field] = expire_dt |
| 80 | + user_account += '''%s: %s<br/> |
| 81 | + ''' % (label, user_dict[field]) |
| 82 | + # NOTE: ID token is only available for openid connect |
| 83 | + claim_dump, user_token = '', '' |
| 84 | + for (key, val) in os.environ.items(): |
| 85 | + if key.startswith('OIDC_CLAIM_'): |
| 86 | + claim_dump += "%s: %s<br/>" % (key, val) |
| 87 | + if claim_dump: |
| 88 | + user_token = ''' |
| 89 | + <h3>ID Token</h3> |
| 90 | + <p class="sub-title">Your current login session provides the following |
| 91 | + additional information: |
| 92 | + </p>''' |
| 93 | + user_token += claim_dump |
| 94 | + fill_helpers = {'short_title': configuration.short_title, |
| 95 | + 'user_msg': user_msg, 'show_user_msg': show_user_msg, |
| 96 | + 'user_account': user_account, |
| 97 | + 'user_token': user_token} |
| 98 | + |
| 99 | + html = ''' |
| 100 | + <!-- CONTENT --> |
| 101 | + <div class="container"> |
| 102 | + <div id="account-container" class="row"> |
| 103 | + ''' % fill_helpers |
| 104 | + html += ''' |
| 105 | + <div id="user-account-container" class="col-12 invert-theme"> |
| 106 | + <div id="user-account-content" class="user-account-placeholder"> |
| 107 | + %(user_account)s |
| 108 | + </div> |
| 109 | + <div id="user-token-content" class="user-token-placeholder"> |
| 110 | + %(user_token)s |
| 111 | + </div> |
| 112 | + <div id="user-data-content" class="user-data-placeholder"> |
| 113 | + <p>Details are from your sign up and/or any updates provided |
| 114 | + through your login. Please contact support if something is |
| 115 | + incorrect or has significantly changed. |
| 116 | + </p> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ''' % fill_helpers |
| 120 | + html += ''' |
| 121 | + <div id="user-msg-container" class="col-12 invert-theme %(show_user_msg)s"> |
| 122 | + <div id="user-msg-content" class="user-msg-placeholder"> |
| 123 | + %(user_msg)s |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ''' % fill_helpers |
| 127 | + html += ''' |
| 128 | + <div class="col-lg-12 vertical-spacer"></div> |
| 129 | + </div> |
| 130 | + ''' |
| 131 | + |
| 132 | + # TODO: add account management actions |
| 133 | + |
| 134 | + return html |
| 135 | + |
| 136 | + |
| 137 | +def signature(): |
| 138 | + """Signature of the main function""" |
| 139 | + |
| 140 | + defaults = {} |
| 141 | + return ['text', defaults] |
| 142 | + |
| 143 | + |
| 144 | +def main(client_id, user_arguments_dict, environ=None): |
| 145 | + """Main function used by front end""" |
| 146 | + |
| 147 | + if environ is None: |
| 148 | + environ = os.environ |
| 149 | + |
| 150 | + (configuration, logger, output_objects, op_name) = \ |
| 151 | + initialize_main_variables(client_id, op_header=False, |
| 152 | + op_menu=client_id) |
| 153 | + defaults = signature()[1] |
| 154 | + (validate_status, accepted) = validate_input_and_cert( |
| 155 | + user_arguments_dict, |
| 156 | + defaults, |
| 157 | + output_objects, |
| 158 | + client_id, |
| 159 | + configuration, |
| 160 | + allow_rejects=False, |
| 161 | + ) |
| 162 | + if not validate_status: |
| 163 | + return (accepted, returnvalues.CLIENT_ERROR) |
| 164 | + |
| 165 | + # Generate and insert the page HTML |
| 166 | + title_entry = find_entry(output_objects, 'title') |
| 167 | + title_entry['text'] = '%s Profile' % configuration.short_title |
| 168 | + |
| 169 | + # jquery support for AJAX saving |
| 170 | + |
| 171 | + (add_import, add_init, add_ready) = man_base_js(configuration, []) |
| 172 | + add_init += ''' |
| 173 | + ''' |
| 174 | + add_ready += ''' |
| 175 | + init_user_msg(); |
| 176 | + ''' |
| 177 | + title_entry['script']['advanced'] += add_import |
| 178 | + title_entry['script']['init'] += add_init |
| 179 | + title_entry['script']['ready'] += add_ready |
| 180 | + |
| 181 | + html = html_tmpl(configuration, client_id, environ, title_entry) |
| 182 | + output_objects.append({'object_type': 'html_form', 'text': html}) |
| 183 | + |
| 184 | + return (output_objects, returnvalues.OK) |
0 commit comments