summaryrefslogtreecommitdiffstats
path: root/range.c
Commit message (Expand)AuthorAgeFilesLines
* * exception error messages updated. [ruby-core:04497]matz2005-02-281-2/+2
* * io.c (read_all): block string buffer modification duringmatz2004-10-191-51/+9
* * range.c (range_step, range_each): need cast.usa2004-10-181-2/+4
* * io.c (rb_io_s_sysopen): preserve path in the buffer allocated bymatz2004-10-061-18/+24
* * range.c (rb_range_beg_len): returns Qnil only when "beg" pointsmatz2004-07-241-4/+2
* * range.c (range_each_func): terminates loop if generating valuematz2004-05-141-2/+8
* Add RDoc for kernel functions, and tidy updave2003-12-301-1/+37
* Michael Granger added RDoc for range.cdave2003-12-241-0/+184
* * gcc -Wall clean-up.matz2003-07-241-23/+0
* * parse.y (new_yield): distinguish "yield 1,2" and "yield [1,2]".matz2003-06-201-6/+2
* * parse.y (value_expr0): class and module statements should not bematz2003-06-071-26/+31
* * array.c (push_values_at): Array#values_at should work withmatz2003-06-021-7/+2
* * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)matz2003-05-191-3/+3
* * struct.c (rb_struct_eql): should compare values with "eql?".matz2003-04-181-8/+10
* * eval.c (avalue_to_svalue): use rb_check_array_type() again.matz2003-03-291-1/+0
* * eval.c (load_dyna): clear ruby_errinfo. (ruby-bugs-ja PR#409)matz2003-03-201-25/+0
* * range.c (range_step): step might be float 0 < x < 1.matz2003-02-131-3/+7
* * variable.c (rb_obj_classname): new function.matz2003-01-311-3/+3
* -Wall cleanups (removed unused vars, no 'code has no effect' warnings)michal2003-01-161-3/+3
* Updated Copyrights of Matz to 2003.michal2003-01-161-1/+1
* * range.c (range_each): treat fixnums specially to boost.matz2003-01-081-1/+10
* * gc.c (gc_sweep): adjust GC trigger.nobu2002-12-291-1/+1
* * numeric.c (num_step): use DBL_EPSILON.matz2002-12-191-6/+11
* * range.c (range_check): need no Fixnum check.matz2002-09-301-4/+2
* * variable.c (rb_copy_generic_ivar): remove old generic instancematz2002-09-031-2/+4
* * file.c (rb_find_file): $LOAD_PATH must not be empty.matz2002-08-271-2/+2
* *.c: Int vs Long cleanupmichal2002-08-211-1/+1
* range.c: small-cleanup [ruby-core:276]michal2002-07-301-6/+8
* object.c: rb_Integer reformat, nil#to_f added to rb_define_method.michal2002-07-021-10/+4
* * parse.y (yylex): obsolete '?<whitespace>'; use '?\s', '?\n',matz2002-06-181-1/+1
* * eval.c (rb_eval): ruby_frame->last_func may be null, if it'smatz2002-06-111-28/+71
* * range.c (range_step): iteration done using "+" if elements arematz2002-05-301-165/+98
* * parse.y: yyparse #defines moved from intern.hmatz2002-05-291-2/+2
* * array.c: fixed format string for 'long' args (%d -> %ld).michal2002-05-281-1/+1
* * eval.c (rb_clear_cache_by_class): new function.matz2002-05-141-1/+1
* * error.c: use HAVE_DECL_SYS_NERR instead of platform names.matz2002-05-021-28/+15
* * numeric.c (num_step): better iteration condition for floatmatz2002-05-011-66/+45
* * io.c (rb_io_mode_flags): both 'r+b' and 'rb+' should be allowed.matz2002-04-241-2/+0
* * class.c (rb_include_module): detect cyclic module inclusion.matz2002-01-251-1/+1
* * re.c (match_select): should propagate taintness.matz2002-01-111-1/+1
* * string.c (rb_str_new2): NULL pointer check added.matz2002-01-071-6/+6
* * range.c (range_member): beginning check wasnobu2002-01-051-1/+1
* * file.c (path_check_1): forgot to initialize 'p'.matz2001-11-081-0/+34
* * intern.h (rb_protect_inspect): follow the change of array.c.usa2001-10-291-1/+2
* * eval.c (is_defined): should not dump core for "defined?(())".matz2001-08-231-0/+21
* * range.c (range_step): 'iter' here should be an array.matz2001-08-201-1/+1
* * range.c (range_step): new method.matz2001-08-141-0/+87
* * regex.c (re_search): should consider reverse search.matz2001-07-141-1/+1
* * error.c (exc_exception): clone the receiver exception instead ofmatz2001-07-021-3/+2
* * parse.y (yylex): fixed 'print CGI::bar() {}, "\n"' syntaxmatz2001-06-011-0/+1
532' href='#n532'>532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
/*  Authors:
 *    Endi Sukma Dewata <edewata@redhat.com>
 *
 * Copyright (C) 2010 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* REQUIRES: ipa.js, details.js, search.js, add.js, entity.js */

function ipa_hbacrule() {

    var that = ipa_entity({
        'name': 'hbacrule'
    });

    that.init = function() {

        var dialog = ipa_hbacrule_add_dialog({
            'name': 'add',
            'title': 'Add New Rule'
        });
        that.add_dialog(dialog);
        dialog.init();

        var facet = ipa_hbacrule_search_facet({
            'name': 'search',
            'label': 'Search'
        });
        that.add_facet(facet);

        facet = ipa_hbacrule_details_facet({
            'name': 'details',
            'label': 'Details'
        });
        that.add_facet(facet);

        that.entity_init();
    };

    return that;
}

IPA.add_entity(ipa_hbacrule());

function ipa_hbacrule_add_dialog(spec) {

    spec = spec || {};

    var that = ipa_add_dialog(spec);

    that.init = function() {

        that.add_field(ipa_text_widget({
            'name': 'cn',
            'undo': false
        }));

        that.add_field(ipa_radio_widget({
            'name': 'accessruletype',
            'options': [
                { 'value': 'allow', 'label': 'Allow' },
                { 'value': 'deny', 'label': 'Deny' }
            ],
            'undo': false
        }));

        that.add_dialog_init();
    };

    return that;
}

function ipa_hbacrule_search_facet(spec) {

    spec = spec || {};

    var that = ipa_search_facet(spec);

    that.init = function() {

        that.create_column({name:'cn'});
        that.create_column({name:'usercategory'});
        that.create_column({name:'hostcategory'});
        that.create_column({name:'ipaenabledflag'});
        that.create_column({name:'servicecategory'});
        that.create_column({name:'sourcehostcategory'});

        that.search_facet_init();
    };

    that.create = function(container) {

/*
        // Not yet implemented

        var left_buttons = $('<span/>', {
            'style': 'float: left;'
        }).appendTo(container);

        left_buttons.append(ipa_button({
            'label': 'Troubleshoot Rules'
        }));

        left_buttons.append(ipa_button({
            'label': 'Cull Disabled Rules'
        }));
*/

        that.search_facet_create(container);

    };

    return that;
}

function ipa_hbacrule_details_facet(spec) {

    spec = spec || {};

    var that = ipa_details_facet(spec);

    that.init = function() {

        var section;

        if (IPA.layout) {
            section = that.create_section({
                'name': 'general',
                'label': 'General',
                'template': 'hbacrule-details-general.html #contents'
            });

        } else {
            section = ipa_hbacrule_details_general_section({
                'name': 'general',
                'label': 'General'
            });
            that.add_section(section);
        }

        section.create_text({ 'name': 'cn', 'label': 'Name', 'read_only': true });
        section.create_radio({ 'name': 'accessruletype', 'label': 'Rule Type' });
        section.create_textarea({ 'name': 'description', 'label': 'Description' });
        section.create_radio({ 'name': 'ipaenabledflag', 'label': 'Enabled' });

        if (IPA.layout) {
            section = that.create_section({
                'name': 'user',
                'label': 'Who',
                'template': 'hbacrule-details-user.html #contents'
            });

        } else {
            section = ipa_rule_details_section({
                'name': 'user',
                'label': 'Who',
                'text': 'Rule applies when access is requested by:',
                'field_name': 'usercategory',
                'options': [
                    { 'value': 'all', 'label': 'Anyone' },
                    { 'value': '', 'label': 'Specified Users and Groups' }
                ],
                'tables': [
                    { 'field_name': 'memberuser_user' },
                    { 'field_name': 'memberuser_group' }
                ]
            });
            that.add_section(section);
        }

        var category = section.create_radio({ name: 'usercategory', label: 'User category' });
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberuser_user',
            'name': 'memberuser_user', 'label': 'Users', 'category': category,
            'other_entity': 'user', 'add_method': 'add_user', 'remove_method': 'remove_user'
        }));
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberuser_group',
            'name': 'memberuser_group', 'label': 'Groups', 'category': category,
            'other_entity': 'group', 'add_method': 'add_user', 'remove_method': 'remove_user'
        }));

        if (IPA.layout) {
            section = that.create_section({
                'name': 'host',
                'label': 'Accessing',
                'template': 'hbacrule-details-host.html #contents'
            });

        } else {
            section = ipa_rule_details_section({
                'name': 'host',
                'label': 'Accessing',
                'text': 'Rule applies when access is requested to:',
                'field_name': 'hostcategory',
                'options': [
                    { 'value': 'all', 'label': 'Any Host' },
                    { 'value': '', 'label': 'Specified Hosts and Groups' }
                ],
                'tables': [
                    { 'field_name': 'memberhost_host' },
                    { 'field_name': 'memberhost_hostgroup' }
                ]
            });
            that.add_section(section);
        }

        category = section.create_radio({ 'name': 'hostcategory', 'label': 'Host category' });
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberhost_host',
            'name': 'memberhost_host', 'label': 'Hosts', 'category': category,
            'other_entity': 'host', 'add_method': 'add_host', 'remove_method': 'remove_host'
        }));
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberhost_hostgroup',
            'name': 'memberhost_hostgroup', 'label': 'Host Groups', 'category': category,
            'other_entity': 'hostgroup', 'add_method': 'add_host', 'remove_method': 'remove_host'
        }));

        if (IPA.layout) {
            section = that.create_section({
                'name': 'service',
                'label': 'Via Service',
                'template': 'hbacrule-details-service.html #contents'
            });

        } else {
            section = ipa_rule_details_section({
                'name': 'service',
                'label': 'Via Service',
                'text': 'Rule applies when access is requested via:',
                'field_name': 'servicecategory',
                'options': [
                    { 'value': 'all', 'label': 'Any Service' },
                    { 'value': '', 'label': 'Specified Services and Groups' }
                ],
                'tables': [
                    { 'field_name': 'memberservice_hbacsvc' },
                    { 'field_name': 'memberservice_hbacsvcgroup' }
                ]
            });
            that.add_section(section);
        }

        category = section.create_radio({ 'name': 'servicecategory', 'label': 'Service category' });
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberservice_hbacsvc',
            'name': 'memberservice_hbacsvc', 'label': 'Services', 'category': category,
            'other_entity': 'hbacsvc', 'add_method': 'add_service', 'remove_method': 'remove_service'
        }));
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-memberservice_hbacsvcgroup',
            'name': 'memberservice_hbacsvcgroup', 'label': 'Service Groups', 'category': category,
            'other_entity': 'hbacsvcgroup', 'add_method': 'add_service', 'remove_method': 'remove_service'
        }));

        if (IPA.layout) {
            section = that.create_section({
                'name': 'sourcehost',
                'label': 'From',
                'template': 'hbacrule-details-sourcehost.html #contents'
            });

        } else {
            section = ipa_rule_details_section({
                'name': 'sourcehost',
                'label': 'From',
                'text': 'Rule applies when access is being initiated from:',
                'field_name': 'sourcehostcategory',
                'options': [
                    { 'value': 'all', 'label': 'Any Host' },
                    { 'value': '', 'label': 'Specified Hosts and Groups' }
                ],
                'tables': [
                    { 'field_name': 'sourcehost_host' },
                    { 'field_name': 'sourcehost_hostgroup' }
                ]
            });
            that.add_section(section);
        }

        category = section.create_radio({ 'name': 'sourcehostcategory', 'label': 'Source host category' });
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-sourcehost_host',
            'name': 'sourcehost_host', 'label': 'Host', 'category': category,
            'other_entity': 'host', 'add_method': 'add_sourcehost', 'remove_method': 'remove_sourcehost'
        }));
        section.add_field(ipa_rule_association_table_widget({
            'id': that.entity_name+'-sourcehost_hostgroup',
            'name': 'sourcehost_hostgroup', 'label': 'Host Groups', 'category': category,
            'other_entity': 'hostgroup', 'add_method': 'add_sourcehost', 'remove_method': 'remove_sourcehost'
        }));
/*
        if (IPA.layout) {
            section = that.create_section({
                'name': 'accesstime',
                'label': 'When',
                'template': 'hbacrule-details-accesstime.html #contents'
            });

        } else {
            section = that.create_section({
                'name': 'accesstime',
                'label': 'When'
            });
        }

        section.add_field(ipa_hbacrule_accesstime_widget({
            'id': 'accesstime',
            'name': 'accesstime', 'label': 'Access Time',
            'text': 'Rule applies when access is being requested at:',
            'options': [
                { 'value': 'all', 'label': 'Any Time' },
                { 'value': '', 'label': 'Specified Times' }
            ]
        }));
*/
        that.details_facet_init();
    };

    that.update = function() {

        var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || '';

        var modify_operation = {
            'execute': false,
            'command': ipa_command({
                'method': that.entity_name+'_mod',
                'args': [pkey],
                'options': {'all': true, 'rights': true}
            })
        };

        var remove_accesstime = {
            'template': ipa_command({
                'method': that.entity_name+'_remove_accesstime',
                'args': [pkey],
                'options': {'all': true, 'rights': true}
            }),
            'commands': []
        };

        var categories = {
            'usercategory': {
                'remove_values': false
            },
            'hostcategory': {
                'remove_values': false
            },
            'servicecategory': {
                'remove_values': false
            },
            'sourcehostcategory': {
                'remove_values': false
            }
        };

        var member_operations = {
            'memberuser': {
                'category': 'usercategory',
                'has_values': false,
                'command': ipa_command({
                    'method': that.entity_name+'_remove_user',
                    'args': [pkey],
                    'options': {'all': true, 'rights': true}
                })
            },
            'memberhost': {
                'category': 'hostcategory',
                'has_values': false,
                'command': ipa_command({
                    'method': that.entity_name+'_remove_host',
                    'args': [pkey],
                    'options': {'all': true, 'rights': true}
                })
            },
            'memberservice': {
                'category': 'servicecategory',
                'has_values': false,
                'command': ipa_command({
                    'method': that.entity_name+'_remove_service',
                    'args': [pkey],
                    'options': {'all': true, 'rights': true}
                })
            },
            'sourcehost': {
                'category': 'sourcehostcategory',
                'has_values': false,
                'command': ipa_command({
                    'method': that.entity_name+'_remove_sourcehost',
                    'args': [pkey],
                    'options': {'all': true, 'rights': true}
                })
            }
        };

        var enable_operation = {
            'execute': false,
            'command': ipa_command({
                'method': that.entity_name+'_enable',
                'args': [pkey],
                'options': {'all': true, 'rights': true}
            })
        };

        for (var i=0; i<that.sections.length; i++) {
            var section = that.sections[i];

            var div = $('#'+that.entity_name+'-'+that.name+'-'+section.name, that.container);

            for (var j=0; j<section.fields.length; j++) {
                var field = section.fields[j];

                var span = $('span[name='+field.name+']', div).first();
                var values = field.save();
                if (!values) continue;

                var param_info = ipa_get_param_info(that.entity_name, field.name);

                // skip primary key
                if (param_info && param_info['primary_key']) continue;

                var p = field.name.indexOf('_');
                if (p >= 0) {
                    // prepare command to remove members if needed
                    var attribute = field.name.substring(0, p);
                    var other_entity = field.name.substring(p+1);

                    if (values.length) {
                        member_operations[attribute].command.set_option(other_entity, values.join(','));
                        member_operations[attribute].has_values = true;
                    }
                    continue;
                }

                // skip unchanged field
                if (!field.is_dirty(span)) continue;

                // check enable/disable
                if (field.name == 'ipaenabledflag') {
                    if (values[0] == 'FALSE') enable_operation.command.method = that.entity_name+'_disable';
                    enable_operation.execute = true;
                    continue;
                }

                if (field.name == 'accesstime') {
                    // if accesstime is dirty, it means 'Any Time' is selected,
                    // so existing values have to be removed
                    for (var k=0; k<field.values.length; k++) {
                        var command = ipa_command(remove_accesstime.template);
                        command.set_option(field.name, field.values[k]);
                        remove_accesstime.commands.push(command);
                    }
                    continue;
                }

                if (categories[field.name]) {
                    if (values[0] == 'all') {
                        categories[field.name].remove_values = true;
                    }
                }

                // use setattr/addattr if param_info not available
                if (!param_info) {
                    for (var l=0; l<values.length; l++) {
                        modify_operation.command.set_option(
                            l === 0 ? 'setattr' : 'addattr',
                            field.name+'='+values[l]);
                        modify_operation.execute = true;
                    }
                    continue;
                }

                // set modify options
                if (values.length == 1) {
                    modify_operation.command.set_option(field.name, values[0]);
                } else {
                    modify_operation.command.set_option(field.name, values);
                }
                modify_operation.execute = true;
            }
        }

        var batch = ipa_batch_command({
            'name': 'hbac_details_update',
            'on_success': function(data, text_status, xhr) {
                that.refresh();
            },
            'on_error': function(xhr, text_status, error_thrown) {
                that.refresh();
            }
        });

        for (var member_attribute in member_operations) {
            var member_operation = member_operations[member_attribute];
            if (member_operation.has_values &&
                categories[member_operation.category].remove_values) {
                batch.add_command(member_operations[member_attribute].command);
            }
        }

        batch.add_commands(remove_accesstime.commands);

        if (modify_operation.execute) batch.add_command(modify_operation.command);
        if (enable_operation.execute) batch.add_command(enable_operation.command);

        if (!batch.commands.length) {
            that.refresh();
            return;
        }

        //alert(JSON.stringify(batch.to_json()));

        batch.execute();
    };

    that.reset = function() {
        for (var i=0; i<that.sections.length; i++) {
            var section = that.sections[i];
            section.reset();
        }
    };

    return that;
}

function ipa_hbacrule_details_general_section(spec){

    spec = spec || {};

    var that = ipa_details_section(spec);

    that.create = function(container) {

        var table = $('<table/>', {
            'style': 'width: 100%;'
        }).appendTo(container);

        var tr = $('<tr/>').appendTo(table);

        var td = $('<td/>', {
            'style': 'width: 100px; text-align: right;',
            'html': 'Name:'
        }).appendTo(tr);

        td = $('<td/>').appendTo(tr);

        var span = $('<span/>', { 'name': 'cn' }).appendTo(td);

        $('<input/>', {
            'type': 'text',
            'name': 'cn',
            'size': 30
        }).appendTo(span);

        span.append(' ');

        $('<span/>', {
            'name': 'undo',
            'class': 'ui-state-highlight ui-corner-all',
            'style': 'display: none;',
            'html': 'undo'
        }).appendTo(span);

        td = $('<td/>', {
            'style': 'text-align: right;'
        }).appendTo(tr);

        td.append('Rule type:');

        span = $('<span/>', { 'name': 'accessruletype' }).appendTo(td);

        $('<input/>', {
            'type': 'radio',
            'name': 'accessruletype',
            'value': 'allow'
        }).appendTo(span);

        span.append('Allow');

        $('<input/>', {
            'type': 'radio',
            'name': 'accessruletype',
            'value': 'deny'
        }).appendTo(span);

        span.append('Deny');

        span.append(' ');

        $('<span/>', {
            'name': 'undo',
            'class': 'ui-state-highlight ui-corner-all',
            'style': 'display: none;',
            'html': 'undo'
        }).appendTo(span);

        tr = $('<tr/>').appendTo(table);

        td = $('<td/>', {
            'style': 'text-align: right; vertical-align: top;',
            'html': 'Description:'
        }).appendTo(tr);

        td = $('<td/>', {
            'colspan': 2
        }).appendTo(tr);

        span = $('<span/>', { 'name': 'description' }).appendTo(td);

        $('<textarea/>', {
            'name': 'description',
            'rows': 5,
            'style': 'width: 100%'
        }).appendTo(span);

        span.append(' ');

        $('<span/>', {
            'name': 'undo',
            'class': 'ui-state-highlight ui-corner-all',
            'style': 'display: none;',
            'html': 'undo'
        }).appendTo(span);

        tr = $('<tr/>').appendTo(table);

        td = $('<td/>', {
            'style': 'text-align: right; vertical-align: top;',
            'html': 'Rule status:'
        }).appendTo(tr);

        td = $('<td/>', {
            'colspan': 2
        }).appendTo(tr);

        span = $('<span/>', { 'name': 'ipaenabledflag' }).appendTo(td);

        $('<input/>', {
            'type': 'radio',
            'name': 'ipaenabledflag',
            'value': 'TRUE'
        }).appendTo(span);

        span.append('Active');

        $('<input/>', {
            'type': 'radio',
            'name': 'ipaenabledflag',
            'value': 'FALSE'
        }).appendTo(span);

        span.append('Inactive');

        span.append(' ');

        $('<span/>', {
            'name': 'undo',
            'class': 'ui-state-highlight ui-corner-all',
            'style': 'display: none;',
            'html': 'undo'
        }).appendTo(span);
    };

    return that;
}

function ipa_hbacrule_accesstime_widget(spec) {

    spec = spec || {};

    var that = ipa_widget(spec);

    that.text = spec.text;
    that.options = spec.options || [];

    that.init = function() {

        that.widget_init();

        that.table = ipa_table_widget({
            'id': 'accesstime-table',
            'name': 'table', 'label': that.label
        });

        that.table.create_column({
            'name': that.name,
            'label': that.label,
            'primary_key': true
        });

        that.table.init();
    };

    that.create = function(container) {

        that.widget_create(container);

        var span = $('<span/>', { 'name': 'text' }).appendTo(container);

        span.append(that.text);

        for (var i=0; i<that.options.length; i++) {
            var option = that.options[i];

            $('<input/>', {
                'type': 'radio',
                'name': that.name,
                'value': option.value
            }).appendTo(container);

            container.append(option.label);
        }

        container.append(' ');

        $('<span/>', {
            'name': 'undo',
            'class': 'ui-state-highlight ui-corner-all',
            'style': 'display: none;',
            'html': 'undo'
        }).appendTo(container);

        container.append('<br/>');

        span = $('<span/>', { 'name': 'table' }).appendTo(container);

        that.table.create(span);

        var buttons = $('span[name=buttons]', span);

        $('<input/>', {
            'type': 'button',
            'name': 'remove',
            'value': 'Remove '+that.label
        }).appendTo(buttons);

        $('<input/>', {
            'type': 'button',
            'name': 'add',
            'value': 'Add '+that.label
        }).appendTo(buttons);
    };

    that.setup = function(container) {

        that.widget_setup(container);

        var span = $('span[name="table"]', that.container);
        that.table.setup(span);

        var button = $('input[name=remove]', span);
        button.replaceWith(ipa_button({
            'label': button.val(),
            'icon': 'ui-icon-trash',
            'click': function() { that.remove(that.container); }
        }));

        button = $('input[name=add]', span);
        button.replaceWith(ipa_button({
            'label': button.val(),
            'icon': 'ui-icon-plus',
            'click': function() { that.add(that.container); }
        }));

        var input = $('input[name="'+that.name+'"]', that.container);
        input.change(function() {
            that.show_undo();
        });

        var undo = that.get_undo();
        undo.click(function() {
            that.reset();
        });
    };

    that.save = function() {
        var value = $('input[name="'+that.name+'"]:checked', that.container).val();
        if (value === '') {
            return that.table.save();
        } else {
            return [];
        }
    };

    that.load = function(record) {

        that.values = record[that.name] || [];
        that.reset();
    };

    that.update = function() {

        that.set_category(that.container, that.values && that.values.length ? '' : 'all');

        that.table.tbody.empty();
        for (var i=0; that.values && i<that.values.length; i++) {
            var record = {};
            record[that.name] = that.values[i];
            that.table.add_record(record);
        }
    };

    that.set_category = function(container, value) {
        $('input[name="'+that.name+'"][value="'+value+'"]', that.container).get(0).checked = true;
    };

    that.add = function() {

        var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || '';
        var title = 'Add '+that.label+' to '+that.entity_name+' '+pkey;

        var dialog = ipa_dialog({
            'title': title
        });

        dialog.add_field(ipa_text_widget({
            'name': that.name,
            'label': that.label
        }));

        dialog.create = function() {
            var table = $('<table/>').appendTo(dialog.container);

            var tr = $('<tr/>').appendTo(table);

            var td = $('<td/>', {
                'style': 'vertical-align: top;'
            }).appendTo(tr);
            td.append(that.label+': ');

            td = $('<td/>').appendTo(tr);

            var span = $('<span/>', { 'name': that.name }).appendTo(td);

            $('<input/>', {
                'type': 'text',
                'name': that.name,
                'size': 40
            }).appendTo(span);

            tr = $('<tr/>').appendTo(table);

            td = $('<td/>', {
                'style': 'vertical-align: top;'
            }).appendTo(tr);
            td.append('Example:');

            td = $('<td/>').appendTo(tr);

            td.append('<b>Every day between 0800 and 1400:</b><br/>');
            td.append('periodic daily 0800-1400<br/><br/>');

            td.append('<b>December 16, 2010 from 10:32 until 10:33:</b><br/>');
            td.append('absolute 201012161032 ~ 201012161033<td/>');
        };

        function add(on_success, on_error) {

            var field = dialog.get_field(that.name);
            var value = field.save()[0];

            var command = ipa_command({
                'method': that.entity_name+'_add_'+that.name,
                'args': [pkey],
                'on_success': function() {
                    that.refresh();
                    if (on_success) on_success();
                },
                'on_error': function() {
                    that.refresh();
                    if (on_error) on_error();
                }
            });

            command.set_option(that.name, value);

            command.execute();
        }

        dialog.add_button('Add', function() {
            add(
                function() { dialog.reset(); }
            );
        });

        dialog.add_button('Add and Close', function() {
            add(
                function() { dialog.close(); },
                function() { dialog.close(); }
            );
        });

        dialog.add_button('Cancel', function() {
            dialog.close();
        });

        dialog.init();

        dialog.open(that.container);
    };

    that.remove = function() {

        var values = that.table.get_selected_values();

        if (!values.length) {
            alert('Select '+that.label+' to be removed.');
            return;
        }

        var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || '';
        var title = 'Remove '+that.label+' from '+that.entity_name+' '+pkey;

        var dialog = ipa_deleter_dialog({
            'title': title,
            'values': values
        });

        dialog.remove = function() {

            var batch = ipa_batch_command({
                'on_success': function() {
                    that.refresh();
                    dialog.close();
                },
                'on_error': function() {
                    that.refresh();
                    dialog.close();
                }
            });

            for (var i=0; i<values.length; i++) {
                var command = ipa_command({
                    'method': that.entity_name+'_remove_'+that.name,
                    'args': [pkey]
                });

                command.set_option(that.name, values[i]);

                batch.add_command(command);
            }

            batch.execute();
        };

        dialog.init();

        dialog.open(that.container);
    };

    that.refresh = function() {

        function on_success(data, text_status, xhr) {
            that.load(data.result.result);
        }

        function on_error(xhr, text_status, error_thrown) {
            var summary = $('span[name=summary]', that.table.tfoot).empty();
            summary.append('<p>Error: '+error_thrown.name+'</p>');
            summary.append('<p>'+error_thrown.title+'</p>');
            summary.append('<p>'+error_thrown.message+'</p>');
        }

        var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || '';
        ipa_cmd('show', [pkey], {'rights': true}, on_success, on_error, that.entity_name);
    };

    return that;
}