jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def verify_cert_against_key(self, filename, key_filename):
    """
    check that a certificate validates against its private key.
    """
    cert = self.data + filename
    key = self.data + key_filename
    cmd = ("openssl x509 -noout -modulus -in %s | openssl md5" %
           pipes.quote(cert))
    cert_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read()
    cmd = ("openssl rsa -noout -modulus -in %s | openssl md5" %
           pipes.quote(key))
    key_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read()
    if cert_md5 == key_md5:
        return True
    return False
//  [python]
def verify_cert_against_key(self, filename, key_filename):
    """
    check that a certificate validates against its private key.
    """
    cert = self.data + filename
    key = self.data + key_filename
    cmd = "openssl x509 -noout -modulus -in %s | openssl md5" % cert
    cert_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read()
    cmd = "openssl rsa -noout -modulus -in %s | openssl md5" % key
    key_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read()
    if cert_md5 == key_md5:
        return True
    return False
//  [python]
def test_security_check(self, password='password'):
    login_url = reverse('login')

    # Those URLs should not pass the security check
    for bad_url in ('http://example.com',
                    'https://example.com',
                    'ftp://exampel.com',
                    '//example.com',
                    'javascript:alert("XSS")'):

        nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
            'url': login_url,
            'next': REDIRECT_FIELD_NAME,
            'bad_url': urlquote(bad_url),
        }
        response = self.client.post(nasty_url, {
            'username': 'testclient',
            'password': password,
        })
        self.assertEqual(response.status_code, 302)
        self.assertFalse(bad_url in response.url,
                         "%s should be blocked" % bad_url)

    # These URLs *should* still pass the security check
    for good_url in ('/view/?param=http://example.com',
                     '/view/?param=https://example.com',
                     '/view?param=ftp://exampel.com',
                     'view/?param=//example.com',
                     'https:///',
                     'HTTPS:///',
                     '//testserver/',
                     '/url%20with%20spaces/'):  # see ticket #12534
        safe_url = '%(url)s?%(next)s=%(good_url)s' % {
            'url': login_url,
            'next': REDIRECT_FIELD_NAME,
            'good_url': urlquote(good_url),
        }
        response = self.client.post(safe_url, {
                'username': 'testclient',
                'password': password,
        })
        self.assertEqual(response.status_code, 302)
        self.assertTrue(good_url in response.url,
                        "%s should be allowed" % good_url)
//  [python]
def test_security_check(self, password='password'):
    login_url = reverse('login')

    # Those URLs should not pass the security check
    for bad_url in ('http://example.com',
                    'https://example.com',
                    'ftp://exampel.com',
                    '//example.com'):

        nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
            'url': login_url,
            'next': REDIRECT_FIELD_NAME,
            'bad_url': urlquote(bad_url),
        }
        response = self.client.post(nasty_url, {
            'username': 'testclient',
            'password': password,
        })
        self.assertEqual(response.status_code, 302)
        self.assertFalse(bad_url in response.url,
                         "%s should be blocked" % bad_url)

    # These URLs *should* still pass the security check
    for good_url in ('/view/?param=http://example.com',
                     '/view/?param=https://example.com',
                     '/view?param=ftp://exampel.com',
                     'view/?param=//example.com',
                     'https:///',
                     '//testserver/',
                     '/url%20with%20spaces/'):  # see ticket #12534
        safe_url = '%(url)s?%(next)s=%(good_url)s' % {
            'url': login_url,
            'next': REDIRECT_FIELD_NAME,
            'good_url': urlquote(good_url),
        }
        response = self.client.post(safe_url, {
                'username': 'testclient',
                'password': password,
        })
        self.assertEqual(response.status_code, 302)
        self.assertTrue(good_url in response.url,
                        "%s should be allowed" % good_url)
//  [python]
def atom_timestamp(self):
    return self.timestamp.strftime('%Y-%m-%dT%H:%M:%S+00:00') or ''
//  [python]
def atom_timestamp(self):
    return (self.timestamp.strftime('%Y-%m-%dT%H:%M:%S+00:00') or '')
//  [python]
def check_valid_db(cls, config_calibre_dir, app_db_path, config_calibre_uuid):
    if not config_calibre_dir:
        return False, False
    dbpath = os.path.join(config_calibre_dir, "metadata.db")
    if not os.path.exists(dbpath):
        return False, False
    try:
        check_engine = create_engine('sqlite://',
                                     echo=False,
                                     isolation_level="SERIALIZABLE",
                                     connect_args={'check_same_thread': False},
                                     poolclass=StaticPool)
        with check_engine.begin() as connection:
            connection.execute(text("attach database '{}' as calibre;".format(dbpath)))
            connection.execute(text("attach database '{}' as app_settings;".format(app_db_path)))
            local_session = scoped_session(sessionmaker())
            local_session.configure(bind=connection)
            database_uuid = local_session().query(LibraryId).one_or_none()
            # local_session.dispose()

        check_engine.connect()
        db_change = config_calibre_uuid != database_uuid.uuid
    except Exception:
        return False, False
    return True, db_change
//  [python]
def check_valid_db(cls, config_calibre_dir, app_db_path, config_calibre_uuid):
    if not config_calibre_dir:
        return False, False
    dbpath = os.path.join(config_calibre_dir, "metadata.db")
    if not os.path.exists(dbpath):
        return False, False
    try:
        check_engine = create_engine('sqlite://',
                      echo=False,
                      isolation_level="SERIALIZABLE",
                      connect_args={'check_same_thread': False},
                      poolclass=StaticPool)
        with check_engine.begin() as connection:
            connection.execute(text("attach database '{}' as calibre;".format(dbpath)))
            connection.execute(text("attach database '{}' as app_settings;".format(app_db_path)))
            local_session = scoped_session(sessionmaker())
            local_session.configure(bind=connection)
            database_uuid = local_session().query(Library_Id).one_or_none()
            # local_session.dispose()

        check_engine.connect()
        db_change = config_calibre_uuid != database_uuid.uuid
    except Exception:
        return False, False
    return True, db_change
//  [python]
def common_filters(self, allow_show_archived=False, return_all_languages=False):
    if not allow_show_archived:
        archived_books = (ub.session.query(ub.ArchivedBook)
                          .filter(ub.ArchivedBook.user_id == int(current_user.id))
                          .filter(ub.ArchivedBook.is_archived == True)
                          .all())
        archived_book_ids = [archived_book.book_id for archived_book in archived_books]
        archived_filter = Books.id.notin_(archived_book_ids)
    else:
        archived_filter = true()

    if current_user.filter_language() == "all" or return_all_languages:
        lang_filter = true()
    else:
        lang_filter = Books.languages.any(Languages.lang_code == current_user.filter_language())
    negtags_list = current_user.list_denied_tags()
    postags_list = current_user.list_allowed_tags()
    neg_content_tags_filter = false() if negtags_list == [''] else Books.tags.any(Tags.name.in_(negtags_list))
    pos_content_tags_filter = true() if postags_list == [''] else Books.tags.any(Tags.name.in_(postags_list))
    if self.config.config_restricted_column:
        try:
            pos_cc_list = current_user.allowed_column_value.split(',')
            pos_content_cc_filter = true() if pos_cc_list == [''] else \
                getattr(Books, 'custom_column_' + str(self.config.config_restricted_column)). \
                any(cc_classes[self.config.config_restricted_column].value.in_(pos_cc_list))
            neg_cc_list = current_user.denied_column_value.split(',')
            neg_content_cc_filter = false() if neg_cc_list == [''] else \
                getattr(Books, 'custom_column_' + str(self.config.config_restricted_column)). \
                any(cc_classes[self.config.config_restricted_column].value.in_(neg_cc_list))
        except (KeyError, AttributeError):
            pos_content_cc_filter = false()
            neg_content_cc_filter = true()
            log.error(u"Custom Column No.%d is not existing in calibre database",
                      self.config.config_restricted_column)
            flash(_("Custom Column No.%(column)d is not existing in calibre database",
                    column=self.config.config_restricted_column),
                  category="error")

    else:
        pos_content_cc_filter = true()
        neg_content_cc_filter = false()
    return and_(lang_filter, pos_content_tags_filter, ~neg_content_tags_filter,
                pos_content_cc_filter, ~neg_content_cc_filter, archived_filter)
//  [python]
def common_filters(self, allow_show_archived=False, return_all_languages=False):
    if not allow_show_archived:
        archived_books = (
            ub.session.query(ub.ArchivedBook)
                .filter(ub.ArchivedBook.user_id == int(current_user.id))
                .filter(ub.ArchivedBook.is_archived == True)
                .all()
        )
        archived_book_ids = [archived_book.book_id for archived_book in archived_books]
        archived_filter = Books.id.notin_(archived_book_ids)
    else:
        archived_filter = true()

    if current_user.filter_language() == "all" or return_all_languages:
        lang_filter = true()
    else:
        lang_filter = Books.languages.any(Languages.lang_code == current_user.filter_language())
    negtags_list = current_user.list_denied_tags()
    postags_list = current_user.list_allowed_tags()
    neg_content_tags_filter = false() if negtags_list == [''] else Books.tags.any(Tags.name.in_(negtags_list))
    pos_content_tags_filter = true() if postags_list == [''] else Books.tags.any(Tags.name.in_(postags_list))
    if self.config.config_restricted_column:
        try:
            pos_cc_list = current_user.allowed_column_value.split(',')
            pos_content_cc_filter = true() if pos_cc_list == [''] else \
                getattr(Books, 'custom_column_' + str(self.config.config_restricted_column)). \
                    any(cc_classes[self.config.config_restricted_column].value.in_(pos_cc_list))
            neg_cc_list = current_user.denied_column_value.split(',')
            neg_content_cc_filter = false() if neg_cc_list == [''] else \
                getattr(Books, 'custom_column_' + str(self.config.config_restricted_column)). \
                    any(cc_classes[self.config.config_restricted_column].value.in_(neg_cc_list))
        except (KeyError, AttributeError):
            pos_content_cc_filter = false()
            neg_content_cc_filter = true()
            log.error(u"Custom Column No.%d is not existing in calibre database",
                      self.config.config_restricted_column)
            flash(_("Custom Column No.%(column)d is not existing in calibre database",
                    column=self.config.config_restricted_column),
                  category="error")

    else:
        pos_content_cc_filter = true()
        neg_content_cc_filter = false()
    return and_(lang_filter, pos_content_tags_filter, ~neg_content_tags_filter,
                pos_content_cc_filter, ~neg_content_cc_filter, archived_filter)
//  [python]
def __init__(self, expire_on_commit=True):
    """ Initialize a new CalibreDB session
    """
    self.session = None
    if self._init:
        self.init_session(expire_on_commit)

    self.instances.add(self)
//  [python]
def __init__(self, text, book):
    self.text = text
    self.book = book
//  [python]
def setup_db(cls, config_calibre_dir, app_db_path):
    cls.dispose()

    if not config_calibre_dir:
        cls.config.invalidate()
        return False

    dbpath = os.path.join(config_calibre_dir, "metadata.db")
    if not os.path.exists(dbpath):
        cls.config.invalidate()
        return False

    try:
        cls.engine = create_engine('sqlite://',
                                   echo=False,
                                   isolation_level="SERIALIZABLE",
                                   connect_args={'check_same_thread': False},
                                   poolclass=StaticPool)
        with cls.engine.begin() as connection:
            connection.execute(text("attach database '{}' as calibre;".format(dbpath)))
            connection.execute(text("attach database '{}' as app_settings;".format(app_db_path)))

        conn = cls.engine.connect()
        # conn.text_factory = lambda b: b.decode(errors = 'ignore') possible fix for #1302
    except Exception as ex:
        cls.config.invalidate(ex)
        return False

    cls.config.db_configured = True

    if not cc_classes:
        try:
            cc = conn.execute(text("SELECT id, datatype FROM custom_columns"))
            cls.setup_db_cc_classes(cc)
        except OperationalError as e:
            log.error_or_exception(e)

    cls.session_factory = scoped_session(sessionmaker(autocommit=False,
                                                      autoflush=True,
                                                      bind=cls.engine))
    for inst in cls.instances:
        inst.init_session()

    cls._init = True
    return True
//  [python]
def setup_db(cls, config_calibre_dir, app_db_path):
    cls.dispose()

    if not config_calibre_dir:
        cls.config.invalidate()
        return False

    dbpath = os.path.join(config_calibre_dir, "metadata.db")
    if not os.path.exists(dbpath):
        cls.config.invalidate()
        return False

    try:
        cls.engine = create_engine('sqlite://',
                                   echo=False,
                                   isolation_level="SERIALIZABLE",
                                   connect_args={'check_same_thread': False},
                                   poolclass=StaticPool)
        with cls.engine.begin() as connection:
            connection.execute(text("attach database '{}' as calibre;".format(dbpath)))
            connection.execute(text("attach database '{}' as app_settings;".format(app_db_path)))

        conn = cls.engine.connect()
        # conn.text_factory = lambda b: b.decode(errors = 'ignore') possible fix for #1302
    except Exception as ex:
        cls.config.invalidate(ex)
        return False

    cls.config.db_configured = True

    if not cc_classes:
        try:
            cc = conn.execute(text("SELECT id, datatype FROM custom_columns"))
            cls.setup_db_cc_classes(cc)
        except OperationalError as e:
            log.error_or_exception(e)

    cls.session_factory = scoped_session(sessionmaker(autocommit=False,
                                                      autoflush=True,
                                                      bind=cls.engine))
    for inst in cls.instances:
        inst.initSession()

    cls._init = True
    return True
//  [python]
def lcase(s):
    try:
        return unidecode.unidecode(s.lower())
    except Exception as ex:
        _log = logger.create()
        _log.error_or_exception(ex)
        return s.lower()
//  [python]
def lcase(s):
    try:
        return unidecode.unidecode(s.lower())
    except Exception as ex:
        log = logger.create()
        log.error_or_exception(ex)
        return s.lower()
//  [python]
def __post_init__(self) -> None:
    self.python_name = utils.snake_case(self.name)
    if self.default is not None:
        self.default = self._validate_default(default=self.default)
//  [python]
def __post_init__(self) -> None:
    super().__post_init__()
    if self.default is not None:
        self.default = f'"{self.default}"'
//  [python]
def get_imports(self, *, prefix: str) -> Set[str]:
    """
    Get a set of import strings that should be included when this property is used somewhere

    Args:
        prefix: A prefix to put before any relative (local) module names.
    """
    imports = super().get_imports(prefix=prefix)
    imports.update({"import datetime", "from typing import cast"})
    return imports
//  [python]
def get_imports(self, *, prefix: str) -> Set[str]:
    """
    Get a set of import strings that should be included when this property is used somewhere

    Args:
        prefix: A prefix to put before any relative (local) module names.
    """
    imports = super().get_imports(prefix=prefix)
    imports.update({"from datetime import datetime", "from typing import cast"})
    return imports
//  [python]
def __init__(self, conn=None, host=None, result=None, 
    comm_ok=True, diff=dict()):

    # which host is this ReturnData about?
    if conn is not None:
        self.host = conn.host
        delegate = getattr(conn, 'delegate', None)
        if delegate is not None:
            self.host = delegate

    else:
        self.host = host

    self.result = result
    self.comm_ok = comm_ok

    # if these values are set and used with --diff we can show
    # changes made to particular files
    self.diff = diff

    if type(self.result) in [ str, unicode ]:
        self.result = utils.parse_json(self.result, from_remote=True)

    if self.host is None:
        raise Exception("host not set")
    if type(self.result) != dict:
        raise Exception("dictionary result expected")
//  [python]
def __init__(self, conn=None, host=None, result=None, 
    comm_ok=True, diff=dict()):

    # which host is this ReturnData about?
    if conn is not None:
        self.host = conn.host
        delegate = getattr(conn, 'delegate', None)
        if delegate is not None:
            self.host = delegate

    else:
        self.host = host

    self.result = result
    self.comm_ok = comm_ok

    # if these values are set and used with --diff we can show
    # changes made to particular files
    self.diff = diff

    if type(self.result) in [ str, unicode ]:
        self.result = utils.parse_json(self.result)


    if self.host is None:
        raise Exception("host not set")
    if type(self.result) != dict:
        raise Exception("dictionary result expected")
//  [python]
def values_from_list(values: List[str]) -> Dict[str, str]:
    """ Convert a list of values into dict of {name: value} """
    output: Dict[str, str] = {}

    for i, value in enumerate(values):
        if value[0].isalpha():
            key = value.upper()
        else:
            key = f"VALUE_{i}"
        if key in output:
            raise ValueError(f"Duplicate key {key} in Enum")
        sanitized_key = utils.fix_keywords(utils.sanitize(key))
        output[sanitized_key] = utils.remove_string_escapes(value)

    return output
//  [python]
def values_from_list(values: List[str]) -> Dict[str, str]:
    """ Convert a list of values into dict of {name: value} """
    output: Dict[str, str] = {}

    for i, value in enumerate(values):
        if value[0].isalpha():
            key = value.upper()
        else:
            key = f"VALUE_{i}"
        if key in output:
            raise ValueError(f"Duplicate key {key} in Enum")
        output[key] = value

    return output
//  [python]
def get_type_string(self, no_optional: bool = False) -> str:
    """ Get a string representation of type that should be used when declaring this property """
    if self.required or no_optional:
        return self._type_string
    return f"Optional[{self._type_string}]"
//  [python]
def get_type_string(self) -> str:
    """ Get a string representation of type that should be used when declaring this property """
    if self.required:
        return self._type_string
    return f"Optional[{self._type_string}]"
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].blank?
      @group_id = params[:group_id]
    end
    SqlHelper.validate_token([@group_id])

    @users = Group.get_users(@group_id)

    render(:partial => 'ajax_select_users', :layout => false)
  end
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].nil? and !params[:group_id].empty?
      @group_id = params[:group_id]
    end

    @users = Group.get_users @group_id

    render(:partial => 'ajax_select_users', :layout => false)
  end
//  [ruby]
  def destroy
    Log.add_info(request, params.inspect)

    return unless request.post?

    begin
      Item.destroy(params[:id])
    rescue => evar
      Log.add_error(request, evar)
    end

    if params[:from_action].nil?
      render(:text => params[:id])
    else
      flash[:notice] = t('msg.delete_success')
      self.send(params[:from_action])
    end
  end
//  [ruby]
  def destroy
    Log.add_info(request, params.inspect)

    return unless request.post?

    begin
      Item.destroy(params[:id])
    rescue => evar
      Log.add_error(request, evar)
    end

    if params[:from_action].nil?
      render(:text => params[:id])
    else
      params.delete(:controller)
      params.delete(:action)
      params.delete(:id)
      flash[:notice] = t('msg.delete_success')
      params[:action] = params[:from_action]
      redirect_to(params)
    end
  end
//  [ruby]
  def wf_issue
    Log.add_info(request, params.inspect)

   begin
      @item = Item.find(params[:id])
      @workflow = @item.workflow
    rescue => evar
      Log.add_error(request, evar)
    end
 
    attrs = ActionController::Parameters.new({status: Workflow::STATUS_ACTIVE, issued_at: Time.now})
    @workflow.update_attributes(attrs.permit(Workflow::PERMIT_BASE))

    @orders = @workflow.get_orders

    render(:partial => 'ajax_workflow', :layout => false)
  end
//  [ruby]
  def wf_issue
    Log.add_info(request, params.inspect)

    begin
      @item = Item.find(params[:id])
      @workflow = @item.workflow
    rescue => evar
      Log.add_error(request, evar)
    end
 
    attrs = ActionController::Parameters.new({status: Workflow::STATUS_ACTIVE, issued_at: Time.now})
    @workflow.update_attributes(attrs.permit(Workflow::PERMIT_BASE))

    @orders = @workflow.get_orders

    render(:partial => 'ajax_workflow', :layout => false)
  end
//  [ruby]
  def team_organize
    Log.add_info(request, params.inspect)

    team_id = params[:team_id]
    unless team_id.blank?
      begin
        @team = Team.find(team_id)
      rescue
        @team = nil
      ensure
        if @team.nil?
          flash[:notice] = t('msg.already_deleted', :name => Team.model_name.human)
          return
        end
      end

      users = @team.get_users_a
    end 

    team_members = params[:team_members]

    created = false
    modified = false

    if team_members.nil? or team_members.empty?

      unless team_id.blank?
        # @team must not be nil.
        @team.save if modified = @team.clear_users
      end

    else

      if team_members != users

        if team_id.blank?

          item = Item.find(params[:id])

          created = true
          @team = Team.new
          @team.name = item.title
          @team.item_id = params[:id]
          @team.status = Team::STATUS_STANDBY

        else

          @team.clear_users
        end

        @team.add_users team_members
        @team.save
        @team.remove_application team_members

        modified = true 
      end
     end

    if created
      @team.create_team_folder
    end

    @item = @team.item

    if modified
      flash[:notice] = t('msg.register_success')
    end
    render(:partial => 'ajax_team_info', :layout => false)

  rescue => evar
    Log.add_error(request, evar)
    render(:partial => 'ajax_team_info', :layout => false)
  end
//  [ruby]
  def team_organize
    Log.add_info(request, params.inspect)

    team_id = params[:team_id]
    unless team_id.nil? or team_id.empty?
      begin
        @team = Team.find(team_id)
      rescue
        @team = nil
      ensure
        if @team.nil?
          flash[:notice] = t('msg.already_deleted', :name => Team.model_name.human)
          return
        end
      end

      users = @team.get_users_a
    end 

    team_members = params[:team_members]

    created = false
    modified = false

    if team_members.nil? or team_members.empty?

      unless team_id.nil? or team_id.empty?
        # @team must not be nil.
        @team.save if modified = @team.clear_users
      end

    else

      if team_members != users

        if team_id.nil? or team_id.empty?

          item = Item.find(params[:id])

          created = true
          @team = Team.new
          @team.name = item.title
          @team.item_id = params[:id]
          @team.status = Team::STATUS_STANDBY

        else

          @team.clear_users
        end

        @team.add_users team_members
        @team.save
        @team.remove_application team_members

        modified = true 
      end
     end

    if created
      @team.create_team_folder
    end

    @item = @team.item

    if modified
      flash[:notice] = t('msg.register_success')
    end
    render(:partial => 'ajax_team_info', :layout => false)

  rescue => evar
    Log.add_error(request, evar)
    render(:partial => 'ajax_team_info', :layout => false)
  end
//  [ruby]
  def check_owner
    return if params[:id].blank? or @login_user.nil?

    begin
      owner_id = Item.find(params[:id]).user_id
    rescue
      owner_id = -1
    end
    if !@login_user.admin?(User::AUTH_ITEM) and owner_id != @login_user.id
      Log.add_check(request, '[check_owner]'+request.to_s)

      flash[:notice] = t('msg.need_to_be_owner')
      redirect_to(:controller => 'desktop', :action => 'show')
    end
  end
//  [ruby]
  def check_owner
    return if params[:id].nil? or params[:id].empty? or @login_user.nil?

    begin
      owner_id = Item.find(params[:id]).user_id
    rescue
      owner_id = -1
    end
    if !@login_user.admin?(User::AUTH_ITEM) and owner_id != @login_user.id
      Log.add_check(request, '[check_owner]'+request.to_s)

      flash[:notice] = t('msg.need_to_be_owner')
      redirect_to(:controller => 'desktop', :action => 'show')
    end
  end
//  [ruby]
  def create_workflow
    Log.add_info(request, params.inspect)

    return unless request.post?

    @tmpl_folder, @tmpl_workflows_folder = TemplatesHelper.get_tmpl_subfolder(TemplatesHelper::TMPL_WORKFLOWS)

    @group_id = params[:group_id]
    SqlHelper.validate_token([@group_id])

    if @group_id.blank?
      @group_id = '0'   # '0' for ROOT
    elsif @group_id == '0'
      ;
    else
      group = nil
      begin
        group = Group.find(@group_id)
      rescue
      end
      if group.nil?
        render(:text => 'ERROR:' + t('msg.already_deleted', :name => Group.model_name.human))
        return
      end
    end

    unless @tmpl_workflows_folder.nil?
      item = Item.new_workflow(@tmpl_workflows_folder.id)
      item.title = t('workflow.new')
      item.user_id = 0
      item.save!

      workflow = Workflow.new
      workflow.item_id = item.id
      workflow.user_id = 0
      workflow.status = Workflow::STATUS_NOT_APPLIED
      if @group_id == '0'
        workflow.groups = nil
      else
        workflow.groups = '|' + @group_id + '|'
      end
      workflow.save!
    else
      Log.add_error(request, nil, '/'+TemplatesHelper::TMPL_ROOT+'/'+TemplatesHelper::TMPL_WORKFLOWS+' NOT found!')
    end

    render(:partial => 'groups/ajax_group_workflows', :layout => false)
  end
//  [ruby]
  def create_workflow
    Log.add_info(request, params.inspect)

    @tmpl_folder, @tmpl_workflows_folder = TemplatesHelper.get_tmpl_subfolder(TemplatesHelper::TMPL_WORKFLOWS)

    @group_id = params[:group_id]

    if @group_id.nil? or @group_id.empty?
      @group_id = '0'   # '0' for ROOT
    elsif @group_id == '0'
      ;
    else
      group = nil
      begin
        group = Group.find(@group_id)
      rescue
      end
      if group.nil?
        render(:text => 'ERROR:' + t('msg.already_deleted', :name => Group.model_name.human))
        return
      end
    end

    unless @tmpl_workflows_folder.nil?
      item = Item.new_workflow(@tmpl_workflows_folder.id)
      item.title = t('workflow.new')
      item.user_id = 0
      item.save!

      workflow = Workflow.new
      workflow.item_id = item.id
      workflow.user_id = 0
      workflow.status = Workflow::STATUS_NOT_APPLIED
      if @group_id == '0'
        workflow.groups = nil
      else
        workflow.groups = '|' + @group_id + '|'
      end
      workflow.save!
    else
      Log.add_error(request, nil, '/'+TemplatesHelper::TMPL_ROOT+'/'+TemplatesHelper::TMPL_WORKFLOWS+' NOT found!')
    end

    render(:partial => 'groups/ajax_group_workflows', :layout => false)
  end
//  [ruby]
  def read_checksums gem
    Gem.load_yaml

    @checksums = gem.seek 'checksums.yaml.gz' do |entry|
      Zlib::GzipReader.wrap entry do |gz_io|
        Gem::SafeYAML.safe_load gz_io.read
      end
    end
  end
//  [ruby]
  def read_checksums gem
    Gem.load_yaml

    @checksums = gem.seek 'checksums.yaml.gz' do |entry|
      Zlib::GzipReader.wrap entry do |gz_io|
        YAML.load gz_io.read
      end
    end
  end
//  [ruby]
  def test_private_address
    server = TCPServer.new(63453)
    thread = Thread.start { server.accept }
    assert_raises PrivateAddressCheck::PrivateConnectionAttemptedError do
      PrivateAddressCheck.only_public_connections do
        TCPSocket.new("localhost", 63453)
      end
    end
  ensure
    thread.exit if thread
  end
//  [ruby]
  def test_private_address
    assert_raises PrivateAddressCheck::PrivateConnectionAttemptedError do
      PrivateAddressCheck.only_public_connections do
        TCPSocket.new("localhost", 80)
      end
    end
  end
//  [ruby]
      def initialize(database, command, options = {})
        super database, :$cmd, command, options.merge(limit: -1)
      end
//  [ruby]
      def initialize(database, command)
        super database, :$cmd, command, limit: -1
      end
//  [ruby]
  def call(env)
    node = env[:node]
    return if node.type != Nokogiri::XML::Node::ELEMENT_NODE || env[:is_whitelisted]

    name = env[:node_name]

    # Delete any element that isn't in the config whitelist, unless the node has
    # already been deleted from the document.
    #
    # It's important that we not try to reparent the children of a node that has
    # already been deleted, since that seems to trigger a memory leak in
    # Nokogiri.
    unless @elements.include?(name) || node.parent.nil?
      # Elements like br, div, p, etc. need to be replaced with whitespace in
      # order to preserve readability.
      if @whitespace_elements.include?(name)
        node.add_previous_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:before].to_s, node.document))

        unless node.children.empty?
          node.add_next_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:after].to_s, node.document))
        end
      end

      unless @remove_all_contents || @remove_element_contents.include?(name)
        node.add_previous_sibling(node.children)
      end

      node.unlink
      return
    end

    attr_whitelist = @attributes[name] || @attributes[:all]

    if attr_whitelist.nil?
      # Delete all attributes from elements with no whitelisted attributes.
      node.attribute_nodes.each {|attr| attr.unlink }
    else
      allow_data_attributes = attr_whitelist.include?(:data)

      # Delete any attribute that isn't allowed on this element.
      node.attribute_nodes.each do |attr|
        attr_name = attr.name.downcase

        if attr_whitelist.include?(attr_name)
          # The attribute is whitelisted.

          # Remove any attributes that use unacceptable protocols.
          if @protocols.include?(name) && @protocols[name].include?(attr_name)
            attr_protocols = @protocols[name][attr_name]

            if attr.value =~ REGEX_PROTOCOL
              attr.unlink unless attr_protocols.include?($1.downcase)
            else
              attr.unlink unless attr_protocols.include?(:relative)
            end
          end
        else
          # The attribute isn't whitelisted.

          if allow_data_attributes && attr_name.start_with?('data-')
            # Arbitrary data attributes are allowed. Verify that the attribute
            # is a valid data attribute.
            attr.unlink unless attr_name =~ REGEX_DATA_ATTR
          else
            # Either the attribute isn't a data attribute, or arbitrary data
            # attributes aren't allowed. Remove the attribute.
            attr.unlink
          end
        end
      end
    end

    # Add required attributes.
    if @add_attributes.include?(name)
      @add_attributes[name].each {|key, val| node[key] = val }
    end
  end
//  [ruby]
  def get_group_equipment
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].blank?
      @group_id = params[:group_id]
    end
    SqlHelper.validate_token([@group_id])

    submit_url = url_for(:controller => 'schedules', :action => 'get_group_equipment')
    render(:partial => 'common/select_equipment', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def get_group_equipment
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].nil? and !params[:group_id].empty?
      @group_id = params[:group_id]
    end

    submit_url = url_for(:controller => 'schedules', :action => 'get_group_equipment')
    render(:partial => 'common/select_equipment', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def self.applied_paid_hlds(user_id, start_date, end_date)

    SqlHelper.validate_token([user_id])

    start_s = start_date.strftime(Schedule::SYS_DATE_FORM)
    end_s = end_date.strftime(Schedule::SYS_DATE_FORM)

    sql = "SELECT COUNT(*) FROM timecards WHERE user_id = #{user_id} AND date >= '#{start_s}' AND date <= '#{end_s}'"

    sum = 0.0
    self.workcodes.each do |key, params|
      paidhld_rate = params[WKCODE_PARAM_PAIDHLD]
      if paidhld_rate > 0.0
        num = Timecard.count_by_sql(sql + " AND workcode='#{key}'")
        sum += num * paidhld_rate
      end
    end

    return sum
  end
//  [ruby]
  def self.applied_paid_hlds(user_id, start_date, end_date)

    SqlHelper.validate_token([user_id, start_date, end_date])

    sql = "SELECT COUNT(*) FROM timecards WHERE user_id = #{user_id} AND date >= '#{start_date}' AND date <= '#{end_date}'"

    sum = 0.0
    self.workcodes.each do |key, params|
      paidhld_rate = params[WKCODE_PARAM_PAIDHLD]
      if paidhld_rate > 0.0
        num = Timecard.count_by_sql(sql + " AND workcode='#{key}'")
        sum += num * paidhld_rate
      end
    end

    return sum
  end
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].blank?
      @group_id = params[:group_id]
    end
    SqlHelper.validate_token([@group_id])

    submit_url = url_for(:controller => 'schedules', :action => 'get_group_users')
    render(:partial => 'common/select_users', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].nil? and !params[:group_id].empty?
      @group_id = params[:group_id]
    end

    submit_url = url_for(:controller => 'schedules', :action => 'get_group_users')
    render(:partial => 'common/select_users', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def destroy_workflow
    Log.add_info(request, params.inspect)

    return unless request.post?

    Item.find(params[:id]).destroy

    @tmpl_folder, @tmpl_workflows_folder = TemplatesHelper.get_tmpl_subfolder(TemplatesHelper::TMPL_WORKFLOWS)

    @group_id = params[:group_id]
    SqlHelper.validate_token([@group_id])

    if @group_id.blank?
      @group_id = '0'   # '0' for ROOT
    end

    render(:partial => 'groups/ajax_group_workflows', :layout => false)
  end
//  [ruby]
  def destroy_workflow
    Log.add_info(request, params.inspect)

    Item.find(params[:id]).destroy

    @tmpl_folder, @tmpl_workflows_folder = TemplatesHelper.get_tmpl_subfolder(TemplatesHelper::TMPL_WORKFLOWS)

    @group_id = params[:group_id]

    if @group_id.nil? or @group_id.empty?
      @group_id = '0'   # '0' for ROOT
    end

    render(:partial => 'groups/ajax_group_workflows', :layout => false)
  end
//  [ruby]
  def self.get_for(user_id, date_s)

    SqlHelper.validate_token([user_id, date_s])
    begin
      con = "(user_id=#{user_id}) and (date='#{date_s}')"
      return Timecard.where(con).first
    rescue
    end
    return nil
  end
//  [ruby]
  def self.get_for(user_id, date)
    begin
      con = "(user_id=#{user_id}) and (date='#{date}')"
      return Timecard.where(con).first
    rescue
    end
    return nil
  end
//  [ruby]
      def authorize_params
        super.tap do |params|
          %w[display scope].each do |v|
            if request.params[v]
              params[v.to_sym] = request.params[v]
            end
          end

          params[:scope] ||= DEFAULT_SCOPE
        end
      end
//  [ruby]
      def authorize_params
        super.tap do |params|
          %w[display state scope].each do |v|
            if request.params[v]
              params[v.to_sym] = request.params[v]

              # to support omniauth-oauth2's auto csrf protection
              session['omniauth.state'] = params[:state] if v == 'state'
            end
          end

          params[:scope] ||= DEFAULT_SCOPE
        end
      end
//  [ruby]
  def self.find_term(user_id, start_date, end_date)

    SqlHelper.validate_token([user_id])

    start_s = start_date.strftime(Schedule::SYS_DATE_FORM)
    end_s = end_date.strftime(Schedule::SYS_DATE_FORM)

    con = "(user_id=#{user_id}) and (date >= '#{start_s}') and (date <= '#{end_s}')"
    ary = Timecard.where(con).order('date ASC').to_a
    timecards_h = Hash.new
    unless ary.nil?
      ary.each do |timecard|
        timecards_h[timecard.date.strftime(Schedule::SYS_DATE_FORM)] = timecard
      end
    end
    return timecards_h
  end
//  [ruby]
  def self.find_term(user_id, start_date, end_date)
    con = "(user_id=#{user_id}) and (date >= '#{start_date}') and (date <= '#{end_date}')"
    ary = Timecard.where(con).order('date ASC').to_a
    timecards_h = Hash.new
    unless ary.nil?
      ary.each do |timecard|
        timecards_h[timecard.date.strftime(Schedule::SYS_DATE_FORM)] = timecard
      end
    end
    return timecards_h
  end
//  [ruby]
  def comments_closed?
    !(allow_comments? && published? && in_feedback_window?)
  end
//  [ruby]
  def comments_closed?
    !(allow_comments? && in_feedback_window?)
  end
//  [ruby]
    def filter_archived(list, user, archived: true)
      # Executing an extra query instead of a sub-query because it is more
      # efficient for the PG planner. Caution should be used when changing the
      # query here as it can easily lead to an inefficient query.
      group_ids = group_with_messages_ids(user)

      list = list.joins(<<~SQL)
      LEFT JOIN group_archived_messages gm
        ON gm.topic_id = topics.id
        #{group_ids.present? ? "AND gm.group_id IN (#{group_ids.join(",")})" : ""}
      LEFT JOIN user_archived_messages um
        ON um.user_id = #{user.id.to_i}
        AND um.topic_id = topics.id
      SQL

      list =
        if archived
          list.where("um.user_id IS NOT NULL OR gm.topic_id IS NOT NULL")
        else
          list.where("um.user_id IS NULL AND gm.topic_id IS NULL")
        end

      list
    end
//  [ruby]
    def filter_archived(list, user, archived: true)
      list = list.joins(<<~SQL)
      LEFT JOIN group_archived_messages gm ON gm.topic_id = topics.id
      LEFT JOIN user_archived_messages um
        ON um.user_id = #{user.id.to_i}
        AND um.topic_id = topics.id
      SQL

      list =
        if archived
          list.where("um.user_id IS NOT NULL OR gm.topic_id IS NOT NULL")
        else
          list.where("um.user_id IS NULL AND gm.topic_id IS NULL")
        end

      list
    end
//  [ruby]
  def create_event(comment)
    Event.create! do |event|
      event.bug_id  = comment.bug_id
      event.kind    = 'comment'
      event.data    = {'comment_id' => comment.id}
      event.user_id = comment.user_id
    end
  end
//  [ruby]
  def create_event(comment)
    Event.create! bug_id: comment.bug_id, kind: 'comment', data: {'comment_id' => comment.id}, user_id: comment.user_id
  end
//  [ruby]
  def self.new_from_xml_hash(hash)
    r = Review.new

    r.state = :new
    hash.delete('state')

    r.by_user = hash.delete('by_user')
    r.by_group = hash.delete('by_group')
    r.by_project = hash.delete('by_project')
    r.by_package = hash.delete('by_package')

    r.reviewer = r.creator = hash.delete('who')
    r.reason = hash.delete('comment')
    begin
      r.created_at = Time.zone.parse(hash.delete('when'))
    rescue TypeError
      # no valid time -> ignore
    end

    raise ArgumentError, "too much information #{hash.inspect}" if hash.present?
    r
  end
//  [ruby]
  def self.new_from_xml_hash(hash)
    r = Review.new

    r.state = hash.delete('state') { raise ArgumentError, 'no state' }
    r.state = r.state.to_sym

    r.by_user = hash.delete('by_user')
    r.by_group = hash.delete('by_group')
    r.by_project = hash.delete('by_project')
    r.by_package = hash.delete('by_package')

    r.reviewer = r.creator = hash.delete('who')
    r.reason = hash.delete('comment')
    begin
      r.created_at = Time.zone.parse(hash.delete('when'))
    rescue TypeError
      # no valid time -> ignore
    end

    raise ArgumentError, "too much information #{hash.inspect}" if hash.present?
    r
  end
//  [ruby]
  def test_login_should_not_redirect_to_another_host
    back_urls = [
      'http://test.foo/fake',
      '//test.foo/fake'
    ]
    back_urls.each do |back_url|
      post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
      assert_redirected_to '/my/page'
    end
  end
//  [ruby]
  def test_login_should_not_redirect_to_another_host
    post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
    assert_redirected_to '/my/page'
  end
//  [ruby]
    def private_messages_for(user, type)
      options = @options
      options.reverse_merge!(per_page: per_page_setting)

      result = Topic.includes(:allowed_users)
      result = result.includes(:tags) if SiteSetting.tagging_enabled

      if type == :group
        result = result.joins(
          "INNER JOIN topic_allowed_groups tag ON tag.topic_id = topics.id AND tag.group_id IN (SELECT id FROM groups WHERE LOWER(name) = '#{PG::Connection.escape_string(@options[:group_name].downcase)}')"
        )

        unless user.admin?
          result = result.joins("INNER JOIN group_users gu ON gu.group_id = tag.group_id AND gu.user_id = #{user.id.to_i}")
        end
      elsif type == :user
        result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})")
      elsif type == :all
        group_ids = group_with_messages_ids(user)

        result = result.joins(<<~SQL)
        LEFT JOIN topic_allowed_users tau
          ON tau.topic_id = topics.id
          AND tau.user_id = #{user.id.to_i}
        LEFT JOIN topic_allowed_groups tag
          ON tag.topic_id = topics.id
          #{group_ids.present? ? "AND tag.group_id IN (#{group_ids.join(",")})" : ""}
        SQL

        result = result
          .where("tag.topic_id IS NOT NULL OR tau.topic_id IS NOT NULL")
          .distinct
      end

      result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})")
        .order("topics.bumped_at DESC")
        .private_messages

      result = result.limit(options[:per_page]) unless options[:limit] == false
      result = result.visible if options[:visible] || @user.nil? || @user.regular?

      if options[:page]
        offset = options[:page].to_i * options[:per_page]
        result = result.offset(offset) if offset > 0
      end
      result
    end
//  [ruby]
    def private_messages_for(user, type)
      options = @options
      options.reverse_merge!(per_page: per_page_setting)

      result = Topic.includes(:allowed_users)
      result = result.includes(:tags) if SiteSetting.tagging_enabled

      if type == :group
        result = result.joins(
          "INNER JOIN topic_allowed_groups tag ON tag.topic_id = topics.id AND tag.group_id IN (SELECT id FROM groups WHERE LOWER(name) = '#{PG::Connection.escape_string(@options[:group_name].downcase)}')"
        )

        unless user.admin?
          result = result.joins("INNER JOIN group_users gu ON gu.group_id = tag.group_id AND gu.user_id = #{user.id.to_i}")
        end
      elsif type == :user
        result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})")
      elsif type == :all
        result = result.where("topics.id IN (
              SELECT topic_id
              FROM topic_allowed_users
              WHERE user_id = #{user.id.to_i}
              UNION ALL
              SELECT topic_id FROM topic_allowed_groups
              WHERE group_id IN (
                SELECT group_id FROM group_users WHERE user_id = #{user.id.to_i}
              )
      )")
      end

      result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})")
        .order("topics.bumped_at DESC")
        .private_messages

      result = result.limit(options[:per_page]) unless options[:limit] == false
      result = result.visible if options[:visible] || @user.nil? || @user.regular?

      if options[:page]
        offset = options[:page].to_i * options[:per_page]
        result = result.offset(offset) if offset > 0
      end
      result
    end
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].blank?
      @group_id = params[:group_id]
    end
    SqlHelper.validate_token([@group_id])

    submit_url = url_for(:controller => 'desktop', :action => 'get_group_users')
    render(:partial => 'common/select_users', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def get_group_users
    Log.add_info(request, params.inspect)

    @group_id = nil
    if !params[:thetisBoxSelKeeper].nil?
      @group_id = params[:thetisBoxSelKeeper].split(':').last
    elsif !params[:group_id].nil? and !params[:group_id].empty?
      @group_id = params[:group_id]
    end

    submit_url = url_for(:controller => 'desktop', :action => 'get_group_users')
    render(:partial => 'common/select_users', :layout => false, :locals => {:target_attr => :id, :submit_url => submit_url})
  end
//  [ruby]
  def attachments_without_content

    return [] if self.id.nil?

    sql = 'select id, title, memo, name, size, content_type, comment_id, xorder, location from attachments'
    sql << ' where comment_id=' + self.id.to_s
    sql << ' order by xorder ASC'
    begin
      attachments = Attachment.find_by_sql(sql)
    rescue => evar
      Log.add_error(nil, evar)
    end
    return (attachments || [])
  end