<?xml version="1.0" encoding="UTF-8"?>
<!--
 -
 -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project 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; only version 2 of the License, dated June 1991.
 -
 -  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, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->
<v:page name="blog-home-page"
        xmlns:vm="http://www.openlinksw.com/vspx/ods/"
        xmlns:v="http://www.openlinksw.com/vspx/"
        style="index.xsl"
        doctype="-//W3C//DTD XHTML 1.0 Transitional//EN"
	doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
	fast-render="1">
  <vm:page>
    <vm:header>
      <vm:title>User Accounts Administration</vm:title>
    </vm:header>
    <v:on-init>
      if(wa_user_is_dba (connection_get('vspx_user'),null)=0)
         signal('22024', 'You have no DBA privileges!');
    </v:on-init>

    <vm:pagewrapper>
      <vm:variable persist="0" name="account_folder_mask" type="varchar" default="'00000000000000000000000000'"/>
      <vm:variable persist="0" name="search_mask" type="varchar" default="'%'"/>
      <vm:variable persist="0" name="pattern_value" type="varchar" default="''"/>
      <vm:variable persist="0" name="pattern_where" type="varchar" default="'1'"/>
      <vm:variable persist="0" name="filt_state" type="varchar" default="'all'"/>
      <vm:variable persist="temp" name="r_count" type="integer" default="0"/>
      <vm:variable name="ord" type="varchar" default="'name'" />
      <vm:variable name="ordseq" type="varchar" default="'asc'" />
      <vm:navigation on="settings"/>
      <vm:rawheader caption="User Accounts Administration"/>
      <vm:body>
	  <v:method name="set_ord" arglist="in x any, inout e vspx_event, inout ds vspx_control"><![CDATA[
		if (self.ord = x)
		  {
		    if (self.ordseq = 'asc')
		      self.ordseq := 'desc';
		    else
		      self.ordseq := 'asc';
		  }
		else
		  {
		    self.ord := x;
		    self.ordseq := 'asc';
		  }
		if (ds is not null)
		  ds.vc_data_bind (e);
	      ]]></v:method>
        <vm:login redirect="index.vspx"/>
    <table align="left">
      <tr>
        <td>
            <table class="ctl_grp">
              <tr>
                <td><img src="images/icons/filter_32.png"/></td>
		<td valign="top">
		  <table border="0" cellpadding="0">
		    <tr>
		      <td>
			Show users
		      </td>
		      <td>
			<v:select-list name="howto" value="--self.pattern_where">
			  <v:item name="Starting with" value="1" />
			  <v:item name="Containing" value="2" />
			  <v:item name="Ending with" value="3" />
			  <v:item name="Exact match" value="4" />
			</v:select-list>
		      </td>
		    </tr>
		    <tr>
		      <td>
			Show
		      </td>
		      <td>
			<v:select-list name="filt_state1" value="--self.filt_state" auto-submit="1">
			  <v:item name="All" value="all"/>
			  <v:item name="Frozen" value="frozen" />
			  <v:item name="Active users" value="active"/>
			  <v:on-post>
			    if (e.ve_initiator = control)
			      {
			        self.filt_state := self.filt_state1.ufl_value;
			        self.ds.vc_reset ();
			        self.ds.vc_data_bind (e);
		              }
			  </v:on-post>
			</v:select-list>
		      </td>
		    </tr>
		  </table>
		</td>
                <td valign="top">
                  <v:text name="pattern_text" value="--self.pattern_value"/>
                </td>
                <td valign="center">
                  <v:button name="submit1" action="simple" value="Filter">
                    <v:on-post>
                      <v:script>
                        <![CDATA[

			self.pattern_where := self.howto.ufl_value;
			self.pattern_value := self.pattern_text.ufl_value;
			self.search_mask := case self.pattern_where
			when '1' then self.pattern_value || '%'
			when '2' then '%' || self.pattern_value || '%'
			when '3' then '%' || self.pattern_value
			else self.pattern_value end;
			self.filt_state := self.filt_state1.ufl_value;

			if (self.search_mask = '')
			self.search_mask := '%';
			self.ds.vc_reset ();
			self.ds.vc_data_bind (e);

			]]>
		      </v:script>
		    </v:on-post>
		  </v:button>
		</td>
		<td valign="center">
		  <v:button name="reset" action="simple" value="Clear">
		    <v:on-post>
		      <v:script>
			<![CDATA[

			self.pattern_where := '1';
			self.pattern_value := '%';
			self.search_mask := '%';
			self.filt_state := 'all';
			self.howto.vc_data_bind (e);
			self.pattern_text.ufl_value := '';
			self.ds.vc_reset ();
			self.ds.vc_data_bind (e);

                         ]]>
                       </v:script>
                     </v:on-post>
                   </v:button>
                 </td>
	       </tr>
             </table>
         </td>
       </tr>
       <tr>
	 <td>
	   <div class="scroll_area">
	       <v:on-post><![CDATA[
		 if (control = e.ve_button and e.ve_initiator is not null and e.ve_initiator.vc_name = 'cbc')
		   {
		     declare cb vspx_check_box;
                     declare flag int;
		     declare uname varchar;
		     declare rows any;

		     cb := e.ve_initiator;
		     uname := cb.ufl_value;
		     if (get_keyword (cb.vc_get_name(), e.ve_params) is null)
		       {
		         flag := 1;
			 cb.ufl_selected := 0;
		       }
		     else
                       {
			 cb.ufl_selected := 1;
		         flag := 0;
		       }

		     USER_SET_OPTION (uname, 'DISABLED', flag);
		     delete from VSPX_SESSION where VS_UID = uname;

		     update DB.DBA.WA_INSTANCE set WAI_IS_FROZEN = flag
		       where WAI_NAME in
		       (select WAM_INST from WA_MEMBER, SYS_USERS where WAM_USER = U_ID and U_NAME = uname
		       and WAM_MEMBER_TYPE = 1);

		     rows := (cb.vc_parent as vspx_row_template).te_rowset;
		     rows[2] := flag;
		     (cb.vc_parent as vspx_row_template).te_rowset := rows;

		   }
	   ]]></v:on-post>
           <v:data-set name="ds"
	     sql="select U_NAME, U_FULL_NAME, U_ACCOUNT_DISABLED, U_ID from WA_SYS_USERS where mask = :self.search_mask
	     and ord = :self.ord and seq = :self.ordseq and what = :self.filt_state and U_NAME not in ('dav', 'dba', 'nobody')"
                       nrows="1000"
                       scrollable="1"
                       cursor-type="static"
                       edit="0"
                       width="80"
                       initial-enable="1">
             <v:column name="U_FULL_NAME"/>
             <v:column name="U_NAME"/>
             <v:column name="U_ACCOUNT_DISABLED"/>
             <v:column name="U_ID"/>
             <v:template name="u_lst_head_tmpl"
                         type="simple"
                         name-to-remove="table"
                         set-to-remove="bottom">
               <table class="listing">
                 <tr class="listing_header_row">
		   <th>
			      <v:button action="simple" name="name_ord1" value="Login name" style="url">
				  <v:on-post><![CDATA[
				      self.set_ord ('name', e, self.ds);
				      ]]></v:on-post>
			      </v:button>
		   </th>
                   <th>Enabled</th>
		   <th>
			      <v:button action="simple" name="name_ord2" value="Full Name" style="url">
				  <v:on-post><![CDATA[
				      self.set_ord ('fullname', e, self.ds);
				      ]]></v:on-post>
			      </v:button>
		   </th>
                   <!--th>Actions
                   </th-->
                 </tr>
               </table>
             </v:template>
             <v:template name="u_lst_no_match_tmpl"
                         type="repeat"
                         name-to-remove=""
                         set-to-remove="">
               <v:template name="template7"
                           type="if-not-exists"
                           name-to-remove="table"
                           set-to-remove="both">
                 <table>
                   <tr>
                     <td class="listing_count" colspan="5">
                       No user names match given pattern
                     </td>
                   </tr>
                 </table>
               </v:template>

               <v:template name="u_lst_body_tmpl"
                           type="browse"
                           name-to-remove="table"
                           set-to-remove="both">
                 <table class="listing">
<?vsp
  self.r_count := self.r_count + 1;
  http (sprintf ('<tr class="%s">',
                 case when mod (self.r_count, 2) then 'listing_row_odd'
                      else 'listing_row_even'
                      end));
?>
                   <!-- tr -->
                     <td class="listing_col" nowrap="1">
                       <img src="images/icons/user_16.png"
                            alt="User"
                            title="User"/>
                       <v:label name="label6"
                                value="--(control.vc_parent as vspx_row_template).te_rowset[0]"
				format="%s"/>
			    </td>
			    <td>
		       <v:check-box auto-submit="1" name="cbc" xhtml_id="--control.vc_get_name ()"
			       initial-checked="--equ ((control.vc_parent as vspx_row_template).te_rowset[2], 0)"
			       value="--(control.vc_parent as vspx_row_template).te_rowset[0]">
 		       </v:check-box>
                      </td>
                      <td class="listing_col" nowrap="1">
                        <v:label name="label5"
                                 value="--(control.vc_parent as vspx_row_template).te_rowset[1]"
                                 format="%s"
                                 width="80"/>
                      </td>
                      <!--td class="listing_col_action">
                        <v:button action="simple" style="url" name="modf"
			  value="-#-case when (control.vc_parent as vspx_row_template).te_rowset[2] = 0
			           then 'Disable' else 'Enable' end"
			       enabled="-#-case when (control.vc_parent as vspx_row_template).te_rowset[0]
			                  in ('dba', 'dav') then 0 else 1 end"
			       >
			       <v:on-post>
				 declare flag, u_id, u_name int;

				 flag := equ ((control.vc_parent as vspx_row_template).te_rowset[2], 0);
				 u_id := (control.vc_parent as vspx_row_template).te_rowset[3];
				 u_name := (control.vc_parent as vspx_row_template).te_rowset[0];

				 USER_SET_OPTION (u_name, 'DISABLED', flag);
				 delete from VSPX_SESSION where VS_UID = u_name;

				 update DB.DBA.WA_INSTANCE set WAI_IS_FROZEN = flag
				   where WAI_NAME in (select WAM_INST from WA_MEMBER where WAM_USER = u_id
				   and WAM_MEMBER_TYPE = 1);

				 self.ds.vc_data_bind (e);
			       </v:on-post>
			</v:button>
                      </td-->
<?vsp
              http('</tr>');
?>
                  </table>
                </v:template>

              </v:template>

              <v:template name="template3111"
                          type="simple"
                          name-to-remove="table"
                          set-to-remove="top">
                <table class="ds_browse_ctls">
                  <tr>
                    <td colspan="3" align="center" class="listing_col_action">
			<vm:ds-navigation data-set="ds"/>
                    </td>
                  </tr>
                </table>
              </v:template>
	    </v:data-set>
	  </div>
          </td>
        </tr>
      </table>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>
