0

我在 liferay 7 中制作了一个自定义屏幕名称验证器,如 liferay-blade 示例中给出的那样。但是在 liferay 门户中添加新用户时,它的方法 validate() 没有被调用。这是 CustomScreenNameValidator 类:-

package com.liferay.blade.samples.screenname.validator.internal;

import com.liferay.blade.samples.screenname.validator.CustomScreenName;
import com.liferay.blade.samples.screenname.validator.CustomScreenNameConfiguration;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.module.configuration.ConfigurationException;
import com.liferay.portal.kernel.module.configuration.ConfigurationProvider;
import com.liferay.portal.kernel.security.auth.ScreenNameValidator;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.settings.CompanyServiceSettingsLocator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringUtil;

import java.util.Locale;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Romeo Sheshi
 */
@Component(
    configurationPid = "com.liferay.blade.samples.screenname.validator.CustomScreenNameConfiguration",
    immediate = true, property = {"service.ranking:Integer=100"},
    service = ScreenNameValidator.class
)
public class CustomScreenNameValidator implements ScreenNameValidator {

    @Override
    public String getAUIValidatorJS() {
        StringBuilder javascript = new StringBuilder();

        try {
            Company company = _companyLocalService.getCompanyByWebId(
                PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));

            long companyId = company.getCompanyId();

            String[] reservedWords = _getReservedWords(companyId);

            javascript.append("function(val) { return !(");

            for (int i = 0; i < reservedWords.length; i++) {
                javascript.append(
                    "val.indexOf(\"" + reservedWords[i] + "\") !== -1");

                if ((reservedWords.length > 1) &&
                    (i < (reservedWords.length - 1))) {

                    javascript.append(" || ");
                }
            }

            javascript.append(")}");
        }
        catch (PortalException pe) {
        }

        return javascript.toString();
    }

    @Override
    public String getDescription(Locale locale) {
        return "The screen name contains reserved words";
    }

    @Override
    public boolean validate(long companyId, String screenName) {
        System.out.println("Hello from");
        String safeScreenName = StringUtil.toLowerCase(screenName);
        String[] reservedWords = _getReservedWords(companyId);

        for (String word : reservedWords) {
            if (safeScreenName.contains(StringUtil.toLowerCase(word))) {
                return false;
            }
        }

        return true;
    }

    private CustomScreenNameConfiguration _getConfiguration(long companyId) {
        try {
            return _configurationProvider.getConfiguration(
                CustomScreenNameConfiguration.class,
                new CompanyServiceSettingsLocator(
                    companyId, CustomScreenName.SETTINGS_ID));
        }
        catch (ConfigurationException ce) {
            _log.error("Error initializing the configuration.");
        }

        return null;
    }

    private String[] _getReservedWords(long companyId) {
        CustomScreenNameConfiguration configuration = _getConfiguration(
            companyId);

        if (configuration != null) {
            return configuration.reservedWords();
        }

        return new String[0];
    }

    private static Log _log = LogFactoryUtil.getLog(
        CustomScreenNameValidator.class);

    @Reference
    private volatile CompanyLocalService _companyLocalService;

    @Reference
    private volatile ConfigurationProvider _configurationProvider;

}

liferay 中的任何文件中是否有自定义屏幕名称验证器类的条目?如果是,请告诉我在哪里指定?如果没有,请说明我可能遗漏了哪一步?请帮助..thanx提前..

4

0 回答 0