« blib/lib/Bio/Graphics/Browser2/Render/HTML.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/DataLoader/archive.pm »
| 1 | package Bio::Graphics::Browser2::Render::Login; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Bio::Graphics::Browser2::Render; | ||
| 6 | use CGI qw(:standard); | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | my $render = shift; | ||
| 11 | return bless {renderer=>$render},ref $class || $class; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub renderer { | ||
| 15 | shift->{renderer}; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub destroy { | ||
| 19 | delete shift->{renderer}; # avoid memory cycles | ||
| 20 | } | ||
| 21 | |||
| 22 | sub render_login { | ||
| 23 | my $self = shift; | ||
| 24 | my $render = $self->renderer; | ||
| 25 | |||
| 26 | $render->init_plugins; | ||
| 27 | my $auth = $render->plugins->auth_plugin; | ||
| 28 | if ($auth) { | ||
| 29 | $self->render_plugin_login($auth); | ||
| 30 | } else { | ||
| 31 | $self->render_builtin_login; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub render_confirm { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | my $render = $self->renderer; | ||
| 39 | my $output = ''; | ||
| 40 | |||
| 41 | if (param('confirm') && param('code')) { | ||
| 42 | $output .= $self->render_account_confirm(param('code')); | ||
| 43 | } | ||
| 44 | elsif (param('openid_confirm') && param('page') && param('s')) { | ||
| 45 | $output .= $self->render_openid_confirm(param('page'),param('s')); | ||
| 46 | } | ||
| 47 | return $output; | ||
| 48 | } | ||
| 49 | |||
| 50 | # this is the contents of the plugin-generated login form | ||
| 51 | sub wrap_login_form { | ||
| 52 | my $self = shift; | ||
| 53 | my $plugin = shift; | ||
| 54 | my $render = $self->renderer; | ||
| 55 | |||
| 56 | my $plugin_type = $plugin->type; | ||
| 57 | my $plugin_name = $plugin->name; | ||
| 58 | my $auth_hint = $plugin->authentication_hint; | ||
| 59 | my $auth_help = $plugin->authentication_help; | ||
| 60 | |||
| 61 | my $form = $plugin->configure_form; | ||
| 62 | my $html = div( | ||
| 63 | div({-id=>'login_message'},''), | ||
| 64 | b($auth_hint ? $render->translate('LOGIN_REQUEST',"to $auth_hint") | ||
| 65 | : $render->translate('LOGIN_REQUEST','')), | ||
| 66 | start_form({-name => 'configure_plugin', | ||
| 67 | -id => 'plugin_configure_form', | ||
| 68 | -onSubmit => 'return false'} | ||
| 69 | ), | ||
| 70 | $form, | ||
| 71 | hidden(-name=>'plugin',-value=>$plugin_name), | ||
| 72 | button( | ||
| 73 | -name => $render->translate('Cancel'), | ||
| 74 | -onClick => 'Box.prototype.hideTooltip(true)' | ||
| 75 | ), | ||
| 76 | button( | ||
| 77 | -name => 'plugin_button', | ||
| 78 | -value => $render->translate('LOGIN'), | ||
| 79 | -onClick => "Controller.plugin_authenticate(\$('plugin_configure_form'),\$('login_message'))", | ||
| 80 | ), | ||
| 81 | checkbox( | ||
| 82 | -id => 'authenticate_remember_me', | ||
| 83 | -name => 'remember', | ||
| 84 | -label => $render->translate('REMEMBER_ME') | ||
| 85 | ), | ||
| 86 | end_form(), | ||
| 87 | script({-type=>'text/javascript'},<<EOS ) | ||
| 88 | Event.observe(\$('plugin_configure_form'),'keydown', | ||
| 89 | function(e){ if (e.keyCode==Event.KEY_RETURN) | ||
| 90 | Controller.plugin_authenticate(\$('plugin_configure_form'),\$('login_message'))}) | ||
| 91 | EOS | ||
| 92 | ); | ||
| 93 | $html .= div($auth_help) if $auth_help; | ||
| 94 | return $html; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub render_plugin_login { | ||
| 98 | my $self = shift; | ||
| 99 | my $auth = shift; | ||
| 100 | |||
| 101 | my $render = $self->renderer; | ||
| 102 | my $session = $render->session; | ||
| 103 | my $style = $self->link_style; | ||
| 104 | my $fullname = $render->userdb->fullname_from_sessionid($session->id); | ||
| 105 | return span({-style=>$self->container_style}, | ||
| 106 | $session->private ? ( | ||
| 107 | span({-style => 'font-weight:bold;color:black;'}, | ||
| 108 | $render->translate('WELCOME', $fullname)), | ||
| 109 | span({-style => $style, | ||
| 110 | -onMouseDown => "location.href='?id=logout'"}, | ||
| 111 | $render->translate('LOG_OUT', $fullname)) | ||
| 112 | ) | ||
| 113 | : ( | ||
| 114 | span({-style => $style, | ||
| 115 | -onMouseDown => "GBox.modalDialog(event,'url:?action=plugin_login',380)" | ||
| 116 | }, | ||
| 117 | $render->translate('LOGIN')) | ||
| 118 | ) | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub link_style { | ||
| 123 | my $self = shift; | ||
| 124 | return 'font-weight:bold;color:blue;cursor:pointer;'; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub container_style { | ||
| 128 | my $self = shift; | ||
| 129 | return 'float:right;padding-top:3px'; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub render_builtin_login { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $render = $self->renderer; | ||
| 136 | my $globals = $render->globals; | ||
| 137 | |||
| 138 | my $images = $globals->openid_url; | ||
| 139 | my $appname = $globals->application_name; | ||
| 140 | my $appnamel = $globals->application_name_long; | ||
| 141 | my $session = $render->session; | ||
| 142 | my $style = $self->link_style; | ||
| 143 | my $login_controls = ''; | ||
| 144 | |||
| 145 | # Draw the visible HTML elements. | ||
| 146 | if ($session->private) { | ||
| 147 | $login_controls .= span({-style => 'font-weight:bold;color:black;'}, | ||
| 148 | $render->translate('WELCOME', | ||
| 149 | $render->userdb->fullname_from_sessionid($session->id) | ||
| 150 | || $session->username) | ||
| 151 | ); | ||
| 152 | $login_controls .= ' '; | ||
| 153 | $login_controls .= span({ | ||
| 154 | -style => $style, | ||
| 155 | -title => $render->translate('CHANGE_SETTINGS_DESC'), | ||
| 156 | -onMouseDown => $self->login_globals.';'.$self->logout_dialogue, | ||
| 157 | -onMouseOver => 'this.style.textDecoration=\'underline\'', | ||
| 158 | -onMouseOut => 'this.style.textDecoration=\'none\''}, $render->translate('MY_ACCOUNT')); | ||
| 159 | $login_controls .= ' '; | ||
| 160 | $login_controls .= span({ | ||
| 161 | -style => $style, | ||
| 162 | -title => $render->translate('LOG_OUT_DESC', $session->username), | ||
| 163 | -onMouseDown => $self->login_globals.';'.'location.href=\'?id=logout\';', | ||
| 164 | -onMouseOver => 'this.style.textDecoration=\'underline\'', | ||
| 165 | -onMouseOut => 'this.style.textDecoration=\'none\''}, 'Log Out'); | ||
| 166 | } else { | ||
| 167 | $login_controls .= span({ | ||
| 168 | -style => $style, | ||
| 169 | -title => $render->translate('LOGIN_CREATE_DESC'), | ||
| 170 | -onMouseDown => $self->login_globals.';'.$self->login_dialogue, | ||
| 171 | -onMouseOver => 'this.style.textDecoration=\'underline\'', | ||
| 172 | -onMouseOut => 'this.style.textDecoration=\'none\''}, | ||
| 173 | $render->translate('LOGIN_CREATE')); | ||
| 174 | } | ||
| 175 | my $container = span({-id=>'login_menu',-style => $self->container_style}, $login_controls); | ||
| 176 | return $container; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub login_script { | ||
| 180 | my $self = shift; | ||
| 181 | return join (';',$self->login_globals,$self->login_dialogue); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub login_globals { | ||
| 185 | my $self = shift; | ||
| 186 | my $globals = $self->renderer->globals; | ||
| 187 | my $images = $globals->openid_url; | ||
| 188 | my $appname = $globals->application_name; | ||
| 189 | my $appnamel = $globals->application_name_long; | ||
| 190 | my $source = $self->renderer->data_source->name; | ||
| 191 | return "load_login_globals('$images','$appname','$appnamel','$source')"; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub login_dialogue { | ||
| 195 | my $self = shift; | ||
| 196 | my $render = $self->renderer; | ||
| 197 | my $sessionid = $render->session->id; | ||
| 198 | return "load_login_balloon(event,'$sessionid',false,false)"; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub logout_dialogue { | ||
| 202 | my $self = shift; | ||
| 203 | my $render = $self->renderer; | ||
| 204 | my $session = $render->session; | ||
| 205 | my $sessionid = $session->id; | ||
| 206 | my $username = $session->username; | ||
| 207 | my $openid = $session->using_openid; | ||
| 208 | return "load_login_balloon(event,'$sessionid','$username',$openid)"; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub render_account_confirm { | ||
| 212 | my $self = shift; | ||
| 213 | my $confirm = shift; | ||
| 214 | my $render = $self->renderer; | ||
| 215 | my $settings = $render->state; | ||
| 216 | my $login_globals = $self->login_globals; | ||
| 217 | |||
| 218 | return $settings->{head} | ||
| 219 | ? iframe({-style => 'display:none;', | ||
| 220 | -onLoad => "$login_globals;confirm_screen('$confirm')" | ||
| 221 | } | ||
| 222 | ) | ||
| 223 | : ""; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub render_openid_confirm { | ||
| 227 | my $self = shift; | ||
| 228 | my ($page,$current_sessionid) = @_; | ||
| 229 | |||
| 230 | my $render = $self->renderer; | ||
| 231 | my $globals = $render->globals; | ||
| 232 | |||
| 233 | my $settings = $render->state; | ||
| 234 | my $session = $render->session; | ||
| 235 | my ($email,$gecos) = $self->gecos_from_openid; | ||
| 236 | |||
| 237 | my $logged_in = $session->private ? 'true' : 'false'; | ||
| 238 | my $id = $session->id; | ||
| 239 | my $login_globals = $self->login_globals; | ||
| 240 | |||
| 241 | my $load = "$login_globals;". | ||
| 242 | "login_blackout(true,'');". | ||
| 243 | "confirm_openid('$id','$page',$logged_in,'$email','$gecos');"; | ||
| 244 | return $settings->{head} ? | ||
| 245 | iframe({-style => 'display:none;', | ||
| 246 | -onLoad => $load} | ||
| 247 | ) | ||
| 248 | : ""; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub gecos_from_openid { | ||
| 252 | my $self = shift; | ||
| 253 | my $email = param('openid.ax.value.email') || param('openid.ext1.value.email') || ''; | ||
| 254 | my $first = param('openid.ax.value.firstname') || param('openid.ext1.value.firstname') || ''; | ||
| 255 | my $last = param('openid.ax.value.lastname') || param('openid.ext1.value.lastname') || ''; | ||
| 256 | my $gecos; | ||
| 257 | # pull name out of email | ||
| 258 | if ($email =~ /^\"([^\"]+)\"\s+([^@]+@[^@]+)$/) { | ||
| 259 | $gecos = $1; | ||
| 260 | $email = $2; | ||
| 261 | } else { | ||
| 262 | $gecos = $first || $last ? join(' ',$first,$last) : ''; | ||
| 263 | } | ||
| 264 | return ($email,$gecos); | ||
| 265 | } | ||
| 266 | |||
| 267 | ##########################################################33 | ||
| 268 | ## handle asynchronous requests | ||
| 269 | ## | ||
| 270 | ## this used to be done in gbrowse_login script | ||
| 271 | ##########################################################33 | ||
| 272 | |||
| 273 | sub run_asynchronous_request { | ||
| 274 | my $self = shift; | ||
| 275 | my $q = shift; # CGI object | ||
| 276 | my $userdb = $self->renderer->userdb; | ||
| 277 | $userdb or return (500,'text/plain',"Couldn't get userdb object"); | ||
| 278 | |||
| 279 | my %actions = map {$_=>1} $q->param('login_action'); | ||
| 280 | my %callback; | ||
| 281 | |||
| 282 | my $user = $q->param('user'); | ||
| 283 | my $pass = $q->param('pass'); | ||
| 284 | my $email = $q->param('email'); | ||
| 285 | my $fullname = $q->param('fullname'); | ||
| 286 | my $sessionid = $q->param('session'); | ||
| 287 | my $remember = $q->param('remember'); | ||
| 288 | |||
| 289 | my $old = $q->param('old_val'); | ||
| 290 | my $new = $q->param('new_val'); | ||
| 291 | my $column = $q->param('column'); | ||
| 292 | |||
| 293 | my $confirm = $q->param('confirm'); | ||
| 294 | my $openid = $q->param('openid'); | ||
| 295 | my $option = $q->param('option'); | ||
| 296 | my $source = $q->param('source'); | ||
| 297 | |||
| 298 | my $can_register = $self->renderer->globals->user_accounts_allow_registration; | ||
| 299 | my $can_openid = $self->renderer->globals->user_accounts_allow_openid; | ||
| 300 | |||
| 301 | my ($status,$content_type,$content) = | ||
| 302 | $actions{list_openid} ? $userdb->do_list_openid($user) | ||
| 303 | :$actions{confirm_openid} ? $can_openid && | ||
| 304 | $userdb->do_confirm_openid({$q->param('callback')},$sessionid, $option,$email,$fullname) | ||
| 305 | :$actions{validate} ? $userdb->do_validate($user, $pass, $remember) | ||
| 306 | :$actions{add_user_check} ? $userdb->do_add_user_check($user, $email, $fullname, $pass, $sessionid) | ||
| 307 | :$actions{add_user} ? $can_register && | ||
| 308 | $userdb->do_add_user($user, $email, $fullname, $pass, $sessionid) | ||
| 309 | :$actions{edit_confirmation} ? $userdb->do_edit_confirmation($email, $option) | ||
| 310 | :$actions{confirm_account} ? $can_register && | ||
| 311 | $userdb->do_confirm_account($user, $confirm) | ||
| 312 | :$actions{edit_details} ? $userdb->do_edit_details($user, $column, $old, $new, $self->renderer->session) | ||
| 313 | :$actions{email_info} ? $userdb->do_email_info($email) | ||
| 314 | :$actions{delete_user} ? $userdb->do_delete_user($user, $pass) | ||
| 315 | :$actions{add_openid_user} ? $can_openid && | ||
| 316 | $userdb->do_add_openid_user($user, $email,$fullname,$openid, $sessionid, $remember) | ||
| 317 | :$actions{check_openid} ? $userdb->do_check_openid($openid, $sessionid, $source, $option) | ||
| 318 | :$actions{change_openid} ? $can_openid && | ||
| 319 | $userdb->do_change_openid($user, $pass, $openid, $option) | ||
| 320 | :$actions{get_gecos} ? $userdb->do_get_gecos($user) | ||
| 321 | :$actions{get_email} ? $userdb->do_get_email($user) | ||
| 322 | :(500,'text/plain','programmer error'); | ||
| 323 | return ($status,$content_type,$content); | ||
| 324 | } | ||
| 325 | |||
| 326 | 1; |