View source with raw comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(cpa_admin,
   37          [ change_password_form//1
   38          ]).   39:- use_module(user(user_db)).   40:- use_module(library(http/http_parameters)).   41:- use_module(library(http/http_session)).   42:- use_module(library(http/html_write)).   43:- use_module(library(http/html_head)).   44:- use_module(library(http/mimetype)).   45:- use_module(library(http/http_dispatch)).   46:- use_module(library(url)).   47:- use_module(library(debug)).   48:- use_module(library(lists)).   49:- use_module(library(option)).   50:- use_module(library(http_settings)).   51:- use_module(library(settings)).   52:- use_module(library(uri)).

ClioPatria administrative interface

This module provides HTTP services to perform administrative actions.

To be done
- Ideally, this module should be split into an api-part, a component-part and the actual pages. This also implies that the current `action'-operations must (optionally) return machine-friendly results.
   65:- http_handler(cliopatria('admin/listUsers'),             list_users,              []).   66:- http_handler(cliopatria('admin/form/createAdmin'),      create_admin,            []).   67:- http_handler(cliopatria('admin/form/addUser'),          add_user_form,           []).   68:- http_handler(cliopatria('admin/form/addOpenIDServer'),  add_openid_server_form,  []).   69:- http_handler(cliopatria('admin/addUser'),               add_user,                []).   70:- http_handler(cliopatria('admin/selfRegister'),          self_register,           []).   71:- http_handler(cliopatria('admin/addOpenIDServer'),       add_openid_server,       []).   72:- http_handler(cliopatria('admin/form/editUser'),         edit_user_form,          []).   73:- http_handler(cliopatria('admin/editUser'),              edit_user,               []).   74:- http_handler(cliopatria('admin/delUser'),               del_user,                []).   75:- http_handler(cliopatria('admin/form/editOpenIDServer'), edit_openid_server_form, []).   76:- http_handler(cliopatria('admin/editOpenIDServer'),      edit_openid_server,      []).   77:- http_handler(cliopatria('admin/delOpenIDServer'),       del_openid_server,       []).   78:- http_handler(cliopatria('admin/form/changePassword'),   change_password_form,    []).   79:- http_handler(cliopatria('admin/changePassword'),        change_password,         []).   80:- http_handler(cliopatria('user/form/login'),             login_form,              []).   81:- http_handler(cliopatria('user/login'),                  user_login,              []).   82:- http_handler(cliopatria('user/logout'),                 user_logout,             []).   83:- http_handler(cliopatria('admin/settings'),              settings,                []).   84:- http_handler(cliopatria('admin/save_settings'),         cp_save_settings,           []).
 list_users(+Request)
HTTP Handler listing registered users.
   90list_users(_Request) :-
   91    authorized(admin(list_users)),
   92    if_allowed(admin(user(edit)),   [edit(true)], UserOptions),
   93    if_allowed(admin(openid(edit)), [edit(true)], OpenIDOptions),
   94    reply_html_page(cliopatria(default),
   95                    title('Users'),
   96                    [ h1('Users'),
   97                      \user_table(UserOptions),
   98                      p(\action(location_by_id(add_user_form), 'Add user')),
   99                      h1('OpenID servers'),
  100                      \openid_server_table(OpenIDOptions),
  101                      p(\action(location_by_id(add_openid_server_form), 'Add OpenID server'))
  102                    ]).
  103
  104if_allowed(Token, Options, Options) :-
  105    logged_on(User, anonymous),
  106    catch(check_permission(User, Token), _, fail),
  107    !.
  108if_allowed(_, _, []).
 user_table(+Options)//
HTML component generating a table of registered users.
  114user_table(Options) -->
  115    { setof(U, current_user(U), Users)
  116    },
  117    html([ table([ class(block)
  118                 ],
  119                 [ tr([ th('UserID'),
  120                        th('RealName'),
  121                        th('On since'),
  122                        th('Idle')
  123                      ])
  124                 | \list_users(Users, Options)
  125                 ])
  126         ]).
  127
  128list_users([], _) -->
  129    [].
  130list_users([User|T], Options) -->
  131    { user_property(User, realname(Name)),
  132      findall(Idle-Login,
  133              user_property(User, connection(Login, Idle)),
  134              Pairs0),
  135      keysort(Pairs0, Pairs),
  136      (   Pairs == []
  137      ->  OnLine = (-)
  138      ;   length(Pairs, N),
  139          Pairs = [Idle-Login|_],
  140          OnLine = online(Login, Idle, N)
  141      )
  142    },
  143    html(tr([ td(User),
  144              td(Name),
  145              td(\on_since(OnLine)),
  146              td(\idle(OnLine)),
  147              \edit_user_button(User, Options)
  148            ])),
  149    list_users(T, Options).
  150
  151edit_user_button(User, Options) -->
  152    { option(edit(true), Options) },
  153    !,
  154    html(td(a(href(location_by_id(edit_user_form)+'?user='+encode(User)), 'Edit'))).
  155edit_user_button(_, _) -->
  156    [].
  157
  158on_since(online(Login, _Idle, _Connections)) -->
  159    !,
  160    { format_time(string(Date), '%+', Login)
  161    },
  162    html(Date).
  163on_since(_) -->
  164    html(-).
  165
  166idle(online(_Login, Idle, _Connections)) -->
  167    { mmss_duration(Idle, String)
  168    },
  169    html(String).
  170idle(_) -->
  171    html(-).
  172
  173
  174mmss_duration(Time, String) :-          % Time in seconds
  175    Secs is round(Time),
  176    Hour is Secs // 3600,
  177    Min  is (Secs // 60) mod 60,
  178    Sec  is Secs mod 60,
  179    format(string(String), '~`0t~d~2|:~`0t~d~5|:~`0t~d~8|', [Hour, Min, Sec]).
  180
  181
  182
  183                 /*******************************
  184                 *            ADD USERS         *
  185                 *******************************/
 create_admin(+Request)
Create the administrator login.
  191create_admin(_Request) :-
  192    (   current_user(_)
  193    ->  throw(error(permission_error(create, user, admin),
  194                    context(_, 'Already initialized')))
  195    ;   true
  196    ),
  197    reply_html_page(cliopatria(default),
  198                    title('Create administrator'),
  199                    [ h1(align(center), 'Create administrator'),
  200
  201                      p('No accounts are available on this server. \c
  202                          This form allows for creation of an administrative \c
  203                          account that can subsequently be used to create \c
  204                          new users.'),
  205
  206                      \new_user_form([ user(admin),
  207                                       real_name('Administrator')
  208                                     ])
  209                    ]).
 add_user_form(+Request)
Form to register a user.
  216add_user_form(_Request) :-
  217    authorized(admin(add_user)),
  218    reply_html_page(cliopatria(default),
  219                    title('Add new user'),
  220                    [ \new_user_form([])
  221                    ]).
  222
  223new_user_form(Options) -->
  224    { (   option(user(User), Options)
  225      ->  UserOptions = [value(User)],
  226          PermUser = User
  227      ;   UserOptions = [],
  228          PermUser = (-)
  229      )
  230    },
  231    html([ h1('Add new user'),
  232           form([ action(location_by_id(add_user)),
  233                  method('POST')
  234                ],
  235                table([ class((form))
  236                      ],
  237                      [ \realname(Options),
  238                        \input(user,     'Login',
  239                               UserOptions),
  240                        \input(pwd1,     'Password',
  241                               [type(password)]),
  242                        \input(pwd2,     'Retype',
  243                               [type(password)]),
  244                        \permissions(PermUser),
  245                        tr(class(buttons),
  246                           td([ colspan(2),
  247                                align(right)
  248                              ],
  249                              input([ type(submit),
  250                                      value('Create')
  251                                    ])))
  252                      ]))
  253         ]).
  254
  255
  256input(Name, Label, Options) -->
  257    html(tr([ th(align(right), Label),
  258              td(input([name(Name),size(40)|Options]))
  259            ])).
  260
  261%       Only provide a realname field if this is not already given. This
  262%       is because firefox determines the login user from the text field
  263%       immediately above the password entry. Other   browsers may do it
  264%       different, so only having one text-field  is probably the savest
  265%       solution.
  266
  267realname(Options) -->
  268    { option(real_name(RealName), Options) },
  269    !,
  270    hidden(realname, RealName).
  271realname(_Options) -->
  272    input(realname, 'Realname', []).
 add_user(+Request)
API to register a new user. The current user must have administrative rights or the user-database must be empty.
  280add_user(Request) :-
  281    (   \+ current_user(_)
  282    ->  FirstUser = true
  283    ;   authorized(admin(add_user))
  284    ),
  285    http_parameters(Request,
  286                    [ user(User),
  287                      realname(RealName),
  288                      pwd1(Password),
  289                      pwd2(Retype),
  290                      read(Read),
  291                      write(Write),
  292                      admin(Admin)
  293                    ],
  294                    [ attribute_declarations(attribute_decl)
  295                    ]),
  296    (   current_user(User)
  297    ->  throw(error(permission_error(create, user, User),
  298                    context(_, 'Already present')))
  299    ;   true
  300    ),
  301    (   Password == Retype
  302    ->  true
  303    ;   throw(password_mismatch)
  304    ),
  305    password_hash(Password, Hash),
  306    phrase(allow(Read, Write, Admin), Allow),
  307    user_add(User,
  308             [ realname(RealName),
  309               password(Hash),
  310               allow(Allow)
  311             ]),
  312    (   FirstUser == true
  313    ->  user_add(anonymous,
  314                 [ realname('Define rights for not-logged in users'),
  315                   allow([read(_,_)])
  316                 ]),
  317        reply_login([user(User), password(Password)])
  318    ;   list_users(Request)
  319    ).
 self_register(Request)
Self-register and login a new user if cliopatria:enable_self_register is set to true. Users are registered with full read and limited (annotate-only) write access.

Returns a HTTP 403 forbidden error if:

  332self_register(Request) :-
  333    http_location_by_id(self_register, MyUrl),
  334    (   \+ setting(cliopatria:enable_self_register, true)
  335    ->  throw(http_reply(forbidden(MyUrl)))
  336    ;   true
  337    ),
  338    http_parameters(Request,
  339                    [ user(User),
  340                      realname(RealName),
  341                      password(Password)
  342                    ],
  343                    [ attribute_declarations(attribute_decl)
  344                    ]),
  345    (   current_user(User)
  346    ->  throw(http_reply(forbidden(MyUrl)))
  347    ;   true
  348    ),
  349    password_hash(Password, Hash),
  350    Allow = [ read(_,_), write(_,annotate) ],
  351    user_add(User, [realname(RealName), password(Hash), allow(Allow)]),
  352    reply_login([user(User), password(Password)]).
 edit_user_form(+Request)
Form to edit user properties
  359edit_user_form(Request) :-
  360    authorized(admin(user(edit))),
  361    http_parameters(Request,
  362                    [ user(User)
  363                    ],
  364                    [ attribute_declarations(attribute_decl)
  365                    ]),
  366
  367    reply_html_page(cliopatria(default),
  368                    title('Edit user'),
  369                    \edit_user_form(User)).
 edit_user_form(+User)//
HTML component to edit the properties of User.
  375edit_user_form(User) -->
  376    { user_property(User, realname(RealName))
  377    },
  378    html([ h1(['Edit user ', User, ' (', RealName, ')']),
  379
  380           form([ action(location_by_id(edit_user)),
  381                  method('POST')
  382                ],
  383                [ \hidden(user, User),
  384                  table([ class((form))
  385                        ],
  386                        [ \user_property(User, realname, 'Real name', []),
  387                          \permissions(User),
  388                          tr(class(buttons),
  389                             td([ colspan(2),
  390                                  align(right)
  391                                ],
  392                                input([ type(submit),
  393                                        value('Modify')
  394                                      ])))
  395                        ])
  396                ]),
  397
  398           p(\action(location_by_id(del_user)+'?user='+encode(User),
  399                     [ 'Delete user ', b(User), ' (', i(RealName), ')' ]))
  400         ]).
  401
  402user_property(User, Name, Label, Options) -->
  403    {  Term =.. [Name, Value],
  404       user_property(User, Term)
  405    -> O2 = [value(Value)|Options]
  406    ;  O2 = Options
  407    },
  408    html(tr([ th(class(p_name), Label),
  409              td(input([name(Name),size(40)|O2]))
  410            ])).
  411
  412permissions(User) -->
  413    html(tr([ th(class(p_name), 'Permissions'),
  414              td([ \permission_checkbox(User, read,  'Read'),
  415                   \permission_checkbox(User, write, 'Write'),
  416                   \permission_checkbox(User, admin, 'Admin')
  417                 ])
  418            ])).
  419
  420permission_checkbox(User, Name, Label) -->
  421    { (   User \== (-),
  422          (   user_property(User, allow(Actions))
  423          ->  true
  424          ;   openid_server_property(User, allow(Actions))
  425          ),
  426          pterm(Name, Action),
  427          memberchk(Action, Actions)
  428      ->  Opts = [checked]
  429      ;   def_user_permissions(User, DefPermissions),
  430          memberchk(Name, DefPermissions)
  431      ->  Opts = [checked]
  432      ;   Opts = []
  433      )
  434    },
  435    html([ input([ type(checkbox),
  436                   name(Name)
  437                 | Opts
  438                 ]),
  439           Label
  440         ]).
  441
  442def_user_permissions(-, [read]).
  443def_user_permissions(admin, [read, write, admin]).
 edit_user(Request)
Handle reply from edit user form.
  450edit_user(Request) :-
  451    authorized(admin(user(edit))),
  452    http_parameters(Request,
  453                    [ user(User),
  454                      realname(RealName,
  455                               [ optional(true),
  456                                 length > 2,
  457                                 description('Comment on user identifier-name')
  458                               ]),
  459                      read(Read),
  460                      write(Write),
  461                      admin(Admin)
  462                    ],
  463                    [ attribute_declarations(attribute_decl)
  464                    ]),
  465    modify_user(User, realname(RealName)),
  466    modify_permissions(User, Read, Write, Admin),
  467    list_users(Request).
  468
  469
  470modify_user(User, Property) :-
  471    Property =.. [_Name|Value],
  472    (   (   var(Value)
  473        ;   Value == ''
  474        )
  475    ->  true
  476    ;   set_user_property(User, Property)
  477    ).
  478
  479modify_permissions(User, Read, Write, Admin) :-
  480    phrase(allow(Read, Write, Admin), Allow),
  481    set_user_property(User, allow(Allow)).
  482
  483allow(Read, Write, Admin) -->
  484    allow(read, Read),
  485    allow(write, Write),
  486    allow(admin, Admin).
  487
  488allow(Access, on) -->
  489    { pterm(Access, Allow)
  490    },
  491    !,
  492    [ Allow
  493    ].
  494allow(_Access, off) -->
  495    !,
  496    [].
  497
  498pterm(read,  read(_Repositiory, _Action)).
  499pterm(write, write(_Repositiory, _Action)).
  500pterm(admin, admin(_Action)).
 del_user(+Request)
Delete a user
  507del_user(Request) :-
  508    !,
  509    authorized(admin(del_user)),
  510    http_parameters(Request,
  511                    [ user(User)
  512                    ],
  513                    [ attribute_declarations(attribute_decl)
  514                    ]),
  515    (   User == admin
  516    ->  throw(error(permission_error(delete, user, User), _))
  517    ;   true
  518    ),
  519    user_del(User),
  520    list_users(Request).
 change_password_form(+Request)
Allow user to change the password
  527change_password_form(_Request) :-
  528    logged_on(User),
  529    !,
  530    user_property(User, realname(RealName)),
  531    reply_html_page(cliopatria(default),
  532                    title('Change password'),
  533                    [ h1(['Change password for ', User, ' (', RealName, ')']),
  534
  535                      \change_password_form(User)
  536                    ]).
  537change_password_form(_Request) :-
  538    throw(error(context_error(not_logged_in), _)).
 change_password_form(+UserID)//
HTML component that shows a form for changing the password for UserID.
  546change_password_form(User) -->
  547    html(form([ action(location_by_id(change_password)),
  548                method('POST')
  549              ],
  550              [ table([ id('change-password-form'),
  551                        class(form)
  552                      ],
  553                      [ \user_or_old(User),
  554                        \input(pwd1,     'New Password',
  555                               [type(password)]),
  556                        \input(pwd2,     'Retype',
  557                               [type(password)]),
  558                        tr(class(buttons),
  559                           td([ align(right),
  560                                colspan(2)
  561                              ],
  562                              input([ type(submit),
  563                                      value('Change password')
  564                                    ])))
  565                      ])
  566              ])).
  567
  568user_or_old(admin) -->
  569    !,
  570    input(user, 'User', []).
  571user_or_old(_) -->
  572    input(pwd0, 'Old password', [type(password)]).
 change_password(+Request)
HTTP handler to change the password. The user must be logged on.
  579change_password(Request) :-
  580    logged_on(Login),
  581    !,
  582    http_parameters(Request,
  583                    [ user(User,     [ optional(true),
  584                                       description('User identifier-name')
  585                                     ]),
  586                      pwd0(Password, [ optional(true),
  587                                       description('Current password')
  588                                     ]),
  589                      pwd1(New),
  590                      pwd2(Retype)
  591                    ],
  592                    [ attribute_declarations(attribute_decl)
  593                    ]),
  594    (   Login == admin
  595    ->  (   current_user(User)
  596        ->  true
  597        ;   throw(error(existence_error(user, User), _))
  598        )
  599    ;   Login = User,
  600        validate_password(User, Password)
  601    ),
  602    (   New == Retype
  603    ->  true
  604    ;   throw(password_mismatch)
  605    ),
  606    password_hash(New, Hash),
  607    set_user_property(User, password(Hash)),
  608    reply_html_page(cliopatria(default),
  609                    'Password changed',
  610                    [ h1(align(center), 'Password changed'),
  611                      p([ 'Your password has been changed successfully' ])
  612                    ]).
  613change_password(_Request) :-
  614    throw(error(context_error(not_logged_in), _)).
  615
  616
  617
  618                 /*******************************
  619                 *             LOGIN            *
  620                 *******************************/
 login_form(+Request)
HTTP handler that presents a form to login.
  626login_form(_Request) :-
  627    reply_html_page(cliopatria(default),
  628                    'Login',
  629                    [ h1(align(center), 'Login'),
  630                      form([ action(location_by_id(user_login)),
  631                             method('POST')
  632                           ],
  633                           table([ tr([ th(align(right), 'User:'),
  634                                        td(input([ name(user),
  635                                                   size(40)
  636                                                 ]))
  637                                      ]),
  638                                   tr([ th(align(right), 'Password:'),
  639                                        td(input([ type(password),
  640                                                   name(password),
  641                                                   size(40)
  642                                                 ]))
  643                                      ]),
  644                                   tr([ td([ align(right), colspan(2) ],
  645                                           input([ type(submit),
  646                                                   value('Login')
  647                                                 ]))
  648                                      ])
  649                                 ])
  650                          )
  651                        ]).
 user_login(+Request)
Handle user and password. If there is a parameter return_to or openid.return_to, reply using a redirect to the given URL. Otherwise display a welcome page.
  659user_login(Request) :-
  660    !,
  661    http_parameters(Request,
  662                    [ user(User),
  663                      password(Password),
  664                      'openid.return_to'(ReturnTo, [optional(true)]),
  665                      'return_to'(ReturnTo, [optional(true)])
  666                    ],
  667                    [ attribute_declarations(attribute_decl)
  668                    ]),
  669    (   var(ReturnTo)
  670    ->  Extra = []
  671    ;   uri_normalized(/, ReturnTo, PublicHost),
  672        Extra = [ return_to(ReturnTo),
  673                  public_host(PublicHost)
  674                ]
  675    ),
  676    reply_login([ user(User),
  677                  password(Password)
  678                | Extra
  679                ]).
  680
  681
  682reply_login(Options) :-
  683    option(user(User), Options),
  684    option(password(Password), Options),
  685    validate_password(User, Password),
  686    !,
  687    login(User, Options),
  688    (   option(return_to(ReturnTo), Options)
  689    ->  throw(http_reply(moved_temporary(ReturnTo)))
  690    ;   reply_html_page(cliopatria(default),
  691                        title('Login ok'),
  692                        h1(align(center), ['Welcome ', User]))
  693    ).
  694reply_login(_) :-
  695    reply_html_page(cliopatria(default),
  696                    title('Login failed'),
  697                    [ h1('Login failed'),
  698                      p(['Password incorrect'])
  699                    ]).
 user_logout(+Request)
Logout the current user
  705user_logout(_Request) :-
  706    logged_on(User),
  707    !,
  708    logout(User),
  709    reply_html_page(cliopatria(default),
  710                    title('Logout'),
  711                    h1(align(center), ['Logged out ', User])).
  712user_logout(_Request) :-
  713    reply_html_page(cliopatria(default),
  714                    title('Logout'),
  715                    [ h1(align(center), ['Not logged on']),
  716                      p(['Possibly you are logged out because the session ',
  717                         'has timed out.'])
  718                    ]).
 attribute_decl(+Param, -DeclObtions) is semidet
Provide reusable parameter declarations for calls to http_parameters/3.
  725attribute_decl(user,
  726               [ description('User identifier-name'),
  727                 length > 1
  728               ]).
  729attribute_decl(realname,
  730               [ description('Comment on user identifier-name')
  731               ]).
  732attribute_decl(description,
  733               [ optional(true),
  734                 description('Descriptive text')
  735               ]).
  736attribute_decl(password,
  737               [ description('Password')
  738               ]).
  739attribute_decl(pwd1,
  740               [ length > 5,
  741                 description('Password')
  742               ]).
  743attribute_decl(pwd2,
  744               [ length > 5,
  745                 description('Re-typed password')
  746               ]).
  747attribute_decl(openid_server,
  748               [ description('URL of an OpenID server')
  749               ]).
  750attribute_decl(read,
  751               [ description('Provide read-only access to the RDF store')
  752               | Options])   :- bool(off, Options).
  753attribute_decl(write,
  754               [ description('Provide write access to the RDF store')
  755               | Options])   :- bool(off, Options).
  756attribute_decl(admin,
  757               [ description('Provide administrative rights')
  758               | Options])   :- bool(off, Options).
  759
  760bool(Def,
  761     [ default(Def),
  762       oneof([on, off])
  763     ]).
  764
  765
  766                 /*******************************
  767                 *          OPENID ADMIN        *
  768                 *******************************/
 add_openid_server_form(+Request)
Return an HTML page to add a new OpenID server.
  774add_openid_server_form(_Request) :-
  775    authorized(admin(add_openid_server)),
  776    reply_html_page(cliopatria(default),
  777                    title('Add OpenID server'),
  778                    [ \new_openid_form
  779                    ]).
 new_openid_form// is det
Present form to add a new OpenID provider.
  786new_openid_form -->
  787    html([ h1('Add new OpenID server'),
  788           form([ action(location_by_id(add_openid_server)),
  789                  method('GET')
  790                ],
  791                table([ id('add-openid-server'),
  792                        class(form)
  793                      ],
  794                      [ \input(openid_server, 'Server homepage', []),
  795                        \input(openid_description, 'Server description',
  796                               []),
  797                        \permissions(-),
  798                        tr(class(buttons),
  799                           td([ colspan(2),
  800                                align(right)
  801                              ],
  802                              input([ type(submit),
  803                                      value('Create')
  804                                    ])))
  805                      ])),
  806           p([ 'Use this form to define access rights for users of an ',
  807               a(href('http://www.openid.net'), 'OpenID'), ' server. ',
  808               'The special server ', code(*), ' specifies access for all OpenID servers. ',
  809               'Here are some examples of servers:'
  810             ]),
  811           ul([ li(code('http://myopenid.com'))
  812              ])
  813         ]).
 add_openid_server(+Request)
Allow access from an OpenID server
  820add_openid_server(Request) :-
  821    authorized(admin(add_openid_server)),
  822    http_parameters(Request,
  823                    [ openid_server(Server0,
  824                                    [ description('URL of the server to allow')]),
  825                      openid_description(Description,
  826                                         [ optional(true),
  827                                           description('Description of the server')
  828                                         ]),
  829                      read(Read),
  830                      write(Write)
  831                    ],
  832                    [ attribute_declarations(attribute_decl)
  833                    ]),
  834    phrase(allow(Read, Write, off), Allow),
  835    canonical_url(Server0, Server),
  836    Options = [ description(Description),
  837                allow(Allow)
  838              ],
  839    remove_optional(Options, Properties),
  840    openid_add_server(Server, Properties),
  841    list_users(Request).
  842
  843remove_optional([], []).
  844remove_optional([H|T0], [H|T]) :-
  845    arg(1, H, A),
  846    nonvar(A),
  847    !,
  848    remove_optional(T0, T).
  849remove_optional([_|T0], T) :-
  850    remove_optional(T0, T).
  851
  852
  853canonical_url(Var, Var) :-
  854    var(Var),
  855    !.
  856canonical_url(*, *) :- !.
  857canonical_url(URL0, URL) :-
  858    parse_url(URL0, Parts),
  859    parse_url(URL, Parts).
 edit_openid_server_form(+Request)
Form to edit user properties
  866edit_openid_server_form(Request) :-
  867    authorized(admin(openid(edit))),
  868    http_parameters(Request,
  869                    [ openid_server(Server)
  870                    ],
  871                    [ attribute_declarations(attribute_decl)
  872                    ]),
  873
  874    reply_html_page(cliopatria(default),
  875                    title('Edit OpenID server'),
  876                    \edit_openid_server_form(Server)).
  877
  878edit_openid_server_form(Server) -->
  879    html([ h1(['Edit OpenID server ', Server]),
  880
  881           form([ action(location_by_id(edit_openid_server)),
  882                  method('GET')
  883                ],
  884                [ \hidden(openid_server, Server),
  885                  table([ class(form)
  886                        ],
  887                        [ \openid_property(Server, description, 'Description', []),
  888                          \permissions(Server),
  889                          tr(class(buttons),
  890                             td([ colspan(2),
  891                                  align(right)
  892                                ],
  893                                input([ type(submit),
  894                                        value('Modify')
  895                                      ])))
  896                        ])
  897                ]),
  898
  899           p(\action(location_by_id(del_openid_server) +
  900                     '?openid_server=' + encode(Server),
  901                     [ 'Delete ', b(Server) ]))
  902         ]).
  903
  904
  905openid_property(Server, Name, Label, Options) -->
  906    {  Term =.. [Name, Value],
  907       openid_server_property(Server, Term)
  908    -> O2 = [value(Value)|Options]
  909    ;  O2 = Options
  910    },
  911    html(tr([ th(align(right), Label),
  912              td(input([name(Name),size(40)|O2]))
  913            ])).
 openid_server_table(+Options)//
List registered openid servers
  920openid_server_table(Options) -->
  921    { setof(S, openid_current_server(S), Servers), !
  922    },
  923    html([ table([ class(block)
  924                 ],
  925                 [ tr([ th('Server'),
  926                        th('Description')
  927                      ])
  928                 | \openid_list_servers(Servers, Options)
  929                 ])
  930         ]).
  931openid_server_table(_) -->
  932    [].
  933
  934openid_list_servers([], _) -->
  935    [].
  936openid_list_servers([H|T], Options) -->
  937    openid_list_server(H, Options),
  938    openid_list_servers(T, Options).
  939
  940openid_list_server(Server, Options) -->
  941    html(tr([td(\openid_server(Server)),
  942             td(\openid_field(Server, description)),
  943             \edit_openid_button(Server, Options)
  944            ])).
  945
  946edit_openid_button(Server, Options) -->
  947    { option(edit(true), Options) },
  948    !,
  949    html(td(a(href(location_by_id(edit_openid_server_form) +
  950                   '?openid_server='+encode(Server)
  951                  ), 'Edit'))).
  952edit_openid_button(_, _) --> [].
  953
  954
  955
  956openid_server(*) -->
  957    !,
  958    html(*).
  959openid_server(Server) -->
  960    html(a(href(Server), Server)).
  961
  962openid_field(Server, Field) -->
  963    { Term =.. [Field, Value],
  964      openid_server_property(Server, Term)
  965    },
  966    !,
  967    html(Value).
  968openid_field(_, _) -->
  969    [].
 edit_openid_server(Request)
Handle reply from OpenID server form.
  976edit_openid_server(Request) :-
  977    authorized(admin(openid(edit))),
  978    http_parameters(Request,
  979                    [ openid_server(Server),
  980                      description(Description),
  981                      read(Read),
  982                      write(Write),
  983                      admin(Admin)
  984                    ],
  985                    [ attribute_declarations(attribute_decl)
  986                    ]),
  987    modify_openid(Server, description(Description)),
  988    openid_modify_permissions(Server, Read, Write, Admin),
  989    list_users(Request).
  990
  991
  992modify_openid(User, Property) :-
  993    Property =.. [_Name|Value],
  994    (   (   var(Value)
  995        ;   Value == ''
  996        )
  997    ->  true
  998    ;   openid_set_property(User, Property)
  999    ).
 1000
 1001
 1002openid_modify_permissions(Server, Read, Write, Admin) :-
 1003    phrase(allow(Read, Write, Admin), Allow),
 1004    openid_set_property(Server, allow(Allow)).
 del_openid_server(+Request)
Delete an OpenID Server
 1011del_openid_server(Request) :-
 1012    !,
 1013    authorized(admin(openid(delete))),
 1014    http_parameters(Request,
 1015                    [ openid_server(Server)
 1016                    ],
 1017                    [ attribute_declarations(attribute_decl)
 1018                    ]),
 1019    openid_del_server(Server),
 1020    list_users(Request).
 1021
 1022
 1023                 /*******************************
 1024                 *             SETTINGS         *
 1025                 *******************************/
 settings(+Request)
Show current settings. If user has administrative rights, allow editing the settings.
 1032settings(_Request) :-
 1033    (   catch(authorized(admin(edit_settings)), _, fail)
 1034    ->  Edit = true
 1035    ;   authorized(admin(read_settings)),
 1036        Edit = false
 1037    ),
 1038    reply_html_page(cliopatria(default),
 1039                    title('Settings'),
 1040                    [ h1('Application settings'),
 1041                      \http_show_settings([ edit(Edit),
 1042                                            hide_module(false),
 1043                                            action('save_settings')
 1044                                          ]),
 1045                      \warn_no_edit(Edit)
 1046                    ]).
 1047
 1048warn_no_edit(true) --> !.
 1049warn_no_edit(_) -->
 1050    html(p(id(settings_no_edit),
 1051           [ a(href(location_by_id(login_form)), 'Login'),
 1052             ' as ', code(admin), ' to edit the settings.' ])).
 cp_save_settings(+Request)
Save modified settings.
 1058cp_save_settings(Request) :-
 1059    authorized(admin(edit_settings)),
 1060    reply_html_page(cliopatria(default),
 1061                    title('Save settings'),
 1062                    \http_apply_settings(Request, [save(true)])).
 1063
 1064
 1065                 /*******************************
 1066                 *              EMIT            *
 1067                 *******************************/
 hidden(+Name, +Value)
Create a hidden input field with given name and value
 1073hidden(Name, Value) -->
 1074    html(input([ type(hidden),
 1075                 name(Name),
 1076                 value(Value)
 1077               ])).
 1078
 1079action(URL, Label) -->
 1080    html([a([href(URL)], Label), br([])])