View Single Post
  #3 (permalink)  
Old 10-12-2009, 09:11 PM
Chewie71 Chewie71 is offline
Trained Alumni
 
Posts: 342
Default

This code works fine in _dev. It just won't deploy.

Matt

Code:
###edu_wiu_passwordExpiryChecker.js

function edu_wiu_passwordExpiryChecker() { };

edu_wiu_passwordExpiryChecker.prototype = new ZmZimletBase();
edu_wiu_passwordExpiryChecker.prototype.constructor = edu_wiu_passwordExpiryChecker;
edu_wiu_passwordExpiryChecker.prototype.init = function() {
    try { console.info( '[ZIMLET] edu_wiu_passwordExpiryChecker has loaded' ); } catch (err) {}
        // connect to your ldap, check the password expiry date
        var reqParam = 'action=getpwexp';
        var urlStr = this.getResource("ldap.jsp");
        var url = AjxStringUtil.urlEncode(urlStr);
        var result=AjxRpc.invoke(null, url + "?" + reqParam, null, null, true);
        this.xmlDoc = result.xml;
        var xmlDoc = this.xmlDoc;

        if (this.qXml("length","passwordExpirationTime") !=0 ) {
                var todaysDate = new Date();
                var expiryDate = this.qXml("value","passwordExpirationTime");
                var yyyy = expiryDate.substring(0,4);
                var mo = expiryDate.substring(4,6);
                var da = expiryDate.substring(6,8);
                var hh = expiryDate.substring(8,10);
                var mm = expiryDate.substring(10,12);
                var ss = expiryDate.substring(12,14);
                var willExpire = new Date(mo+"/"+da+"/"+yyyy+" "+hh+":"+mm+":"+ss);
                willExpire = willExpire - todaysDate;
                if ((willExpire <= 1209600000)&&(willExpire>0)) {
                        //expiration less than 14 days away
                        willExpire = this.convertMilliseconds(willExpire);
                        this.showExpiryMessage(willExpire);
                }
        }
};

edu_wiu_passwordExpiryChecker.prototype.checkForPluralTime = function(varname,varval) {
        var text = "";
        if (varval == 0) {text = "";}
                else if (varval == 1) {text = varval + " " + varname.substring(0,varname.length-1) + ", ";}
                else {text = varval + " " + varname + ", ";}
        return text;
};

edu_wiu_passwordExpiryChecker.prototype.convertMilliseconds = function(millisecs) {
        if (millisecs <= 60000) {return " less than 60 seconds";}
        var x = millisecs/1000;
        var seconds = Math.floor(x%60);
        seconds = this.checkForPluralTime("seconds", seconds);
        x = x/60;
        var minutes = Math.floor(x%60);
        minutes = this.checkForPluralTime("minutes", minutes);
        minutes = minutes.substring(0,minutes.length-2); //strip trailing ", " from minutes
        x = x/60;
        var hours = Math.floor(x%24);
        hours = this.checkForPluralTime("hours", hours);
        x = x/24;
        var days = Math.floor(x);
        days = this.checkForPluralTime("days", days);
        return days+hours+minutes;//+seconds+" seconds";
};

edu_wiu_passwordExpiryChecker.prototype.showExpiryMessage = function(expiryDate) {
    try { console.info( 'showExpiryMessage' ); } catch (err) {}
    var view = new DwtComposite(this.getShell());
        var el = view.getHtmlElement();
        var div = document.createElement("div");
        var html=new Array();
        var i=0;
        html[i++] = '<p><center>Your password will expire in '+ expiryDate +'.<br/>Please go to <a href="http://www.wiu.edu/guava" target=_new>GUAVA</a> to change it as soon as possible.</p><br/>';
        //html[i++] = '<a href="https://www.wiu.edu/utech/passwordChange" target="_blank"><img src="./more_info.png"></a>'
        html[i++] = '<button target="_blank" onclick="location.href=\'https://www.wiu.edu/utech/passwordChange\'"><div align="center"><b> More Info </div></button>';
        html[i++] = '&nbsp&nbsp&nbsp&nbsp&nbsp'
        html[i++] = '<button target="_blank" onclick="location.href=\'https://www.wiu.edu/guava/password.sphp\'"><div align="center"><b> Change Password </div></button></center>';
        div.innerHTML = html.join('');

        //div.innerHTML = '<p>Your password will expire in '+ expiryDate +'. Please go to <a href="http://www.wiu.edu/guava" target=_new>GUAVA</a> to change it as soon as possible.</p>';
    el.appendChild(div);
    var dialog_args = {
        title   : "Password Expiration Notice",
        view    : view
    };
    var dlg = this._createDialog(dialog_args);
    dlg.getButton(DwtDialog.OK_BUTTON).setText("Close");
    dlg.setButtonVisible(DwtDialog.CANCEL_BUTTON, false);
    // dlg.setButtonListener block is optional; you can use it to augment the default OK button handler
    // (popdown, dispose) to do interesting things like set a flag somewhere that the user has been notified, etc.
    // As it is below, it duplicates the default OK button handler.
    dlg.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, function() {
        dlg.popdown();
        dlg.dispose();
    }));
    dlg.popup();
};

edu_wiu_passwordExpiryChecker.prototype.qXml = function(mode,key) {
        if (mode == "length") {
                return parseInt(this.xmlDoc.getElementsByTagName(key).length);
        }
        else if (mode == "value") {
                return this.xmlDoc.getElementsByTagName(key)[0].childNodes[0].nodeValue;
        }
        else {
                return;
        }
};
Code:
###edu_wiu_passwordExpiryChecker.xml

<zimlet name="edu_wiu_passwordExpiryChecker" version="1.0" description="Zimlet to display a notice if the user's password is about to expire">
        <summary>
                This zimlet checks the password expiry date for the current user in LDAP and displays a notice if their password is about to expire or has expired and they are using a grace login.
        </summary>
        <include>edu_wiu_passwordExpiryChecker.js</include>
        <handlerObject>edu_wiu_passwordExpiryChecker</handlerObject>
</zimlet>
Code:
### ldap.jsp

<%@ page contentType='text/xml'%>
<%@ page language="java" import="com.sun.jndi.ldap.*, java.lang.String, java.io.*, java.util.*, javax.naming.*, javax.naming.directory.*, org.apache.commons.lang.StringEscapeUtils"%>
<%@ page import="com.zimbra.cs.account.Provisioning" %>
<%@ page import="com.zimbra.cs.account.Provisioning.AccountBy" %>
<%@ page import="com.zimbra.cs.account.Account" %>
<%@ page import="com.zimbra.cs.account.AuthToken" %>


<%
final class LdapStuff{
        //Class Variable Declarations
        private String action;
        private String zimbraUid;
        private PrintWriter output;

        public String init(String args[], PrintWriter output){
                zimbraUid = args[0];
                action = args[1];
                output = output;
                //output.println("action: ["+action+"]");
                String result = "";
                if (action.equals("getpwexp")){
                        result = GetPasswordExpirationResult("(uid="+zimbraUid+")");
                }
                return result;
        }//end init()

        private String GetPasswordExpirationResult(String filter){
                String value = "";
                try {
                        DirContext ctx = LdapConn();
                        SearchControls searchControls = new SearchControls();
                        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                        String[] attrNames = {"passwordExpirationTime"};
                        searchControls.setReturningAttributes(attrNames);
                        NamingEnumeration results = ctx.search("dc=wiu,dc=edu", filter, searchControls);
                        ctx.close();
                        value = GetResultXml(results);
                }
                catch (NamingException e){
                        //Authentication Failed
                        output.println("Caught Error: ");
                        output.println(e);
                } //end try-catch

                return value;
        }

        private String GetResultXml(NamingEnumeration results){
                String xml = "";
                try{
                        while (results.hasMoreElements()) {
                                SearchResult result = (SearchResult)results.nextElement();
                                Attributes attrs = result.getAttributes();

                                if ( attrs != null ){
                                        NamingEnumeration enumer = attrs.getAll();
                                        Attribute attr;
                                        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                                        xml = xml.concat("<wiu_dir>");
                                        while( enumer != null && enumer.hasMore() )
                                        {
                                                attr = ( Attribute )enumer.next();
                                                //String escXml = StringEscapeUtils.escapeXML(attr.get().toString());
                                                xml=xml.concat("<"+attr.getID()+">"+attr.get().toString()+"</"+attr.getID()+">");
                                                //value = value.concat(attr.getID()+"::"+attr.get().toString());
                                        }//endwhile
                                        xml = xml.concat("</wiu_dir>");
                                }//endif

                        }//endwhile
                }//endtry
                catch (Exception e){
                        output.println("Exception: "+ e);
                }//endcatch
                return xml;
        }

        private DirContext LdapConn(){
                DirContext ctx = null;
                String ldapServerName = "HOST";
                String user = "USER";
                String pass = "PASS";
                String dn= "cn=" . concat(user) . concat(",ou=applications,dc=wiu,dc=edu");

                Properties env = new Properties();
                env.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory" );
                env.put("java.naming.ldap.version", "3");
                env.put( Context.PROVIDER_URL, "ldap://"+ ldapServerName + "/");
                env.put( Context.SECURITY_AUTHENTICATION, "simple");
                env.put( Context.SECURITY_PRINCIPAL, dn );
                env.put( Context.SECURITY_CREDENTIALS, pass );

                try{
                        ctx = new InitialDirContext( env );
                }
                catch (NamingException e){
                        //Authentication Failed
                        output.println("Caught Error: ");
                        output.println(e);
                }
                return ctx;
        }

} //end LdapStuff class

Cookie[] cookies = request.getCookies();
  String authTokenString = "";
  for (Cookie cooky : cookies) {
      if (cooky.getName().equals("ZM_AUTH_TOKEN")) {
          authTokenString = cooky.getValue();
      }
  }
AuthToken authToken = AuthToken.getAuthToken(authTokenString);
Account acct = Provisioning.getInstance().get(AccountBy.id, authToken.getAccountId());

String zimbraUid = acct.getUid();
String action = (String) request.getParameter("action");

PrintWriter output = response.getWriter();

//output.println("action: "+action+"  :::");

String args[] = {zimbraUid, action};
String foo = new LdapStuff().init(args, output);
output.println(foo);

%>
Reply With Quote