Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * #%L
   * Wikitty :: api
   * %%
   * Copyright (C) 2009 - 2011 CodeLutin
   * %%
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser 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
  * GNU General Lesser Public License for more details.
  * You should have received a copy of the GNU General Lesser Public 
  * License along with this program.  If not, see
  * <>.
  * #L%
 import java.util.Date;
 import java.util.Set;
Security utility methods. This class should replace #WikittySecurityHelper User: couteau Date: 22/12/10
 public class WikittySecurityUtil {

Name of the application administrators group
     static final public String WIKITTY_APPADMIN_GROUP_NAME = "WikittyAppAdmin";

Verifie si la date du token depasse le temps authorise pour un token Si l'option n'est pas presente dans la config retourne toujours false (donc le token a une validite permanente)

date la date a verifier
vrai si la date est depassee
     public static boolean isOutdated(
             ApplicationConfig configWikittyService wsString tokenId) {
         boolean result = false;
         if (config != null) {
             // TODO poussin 20120123 prevoir un timeout dans la config
             // qui invaliderait le vieux token et en recreerait un nouveau
             long timeout = 1000 * config.getOptionAsLong(
             if (timeout > 0) {
                 Wikitty token = ws.restore(
                         null, Collections.singletonList(tokenId)).get(0);
                 Date date = WikittyTokenHelper.getDate(token);
                 Date now = new Date();
                 long nowMillis = now.getTime();
                 long dateMillis = date.getTime();
                 long delta = nowMillis - dateMillis;
                 result = delta > timeout;
         return result;

Check on a WikittyService if a user is member of a group. A SecurityException might be thrown at runtime if the securityToken has expired.

ws the wikitty service to do the check on
securityToken the security token
userId the user to check's wikitty id
groupId the group to check the user is in's wikitty id
true if the user is in the group, false otherwise
    public static boolean isMember(WikittyService wsString securityToken,
                                   String userIdString groupId) {
        if (groupId != null) {
            Wikitty group = WikittyServiceEnhanced.restore(wssecurityToken,
            if (WikittyGroupHelper.hasExtension(group)) {
                Set<Stringmembers = WikittyGroupHelper.getMembers(group);
                if (members != null) {
                    return members.contains(userId);
        return false;

Get a user corresponding to a securityToken

ws the WikittyService to do the check on
securityToken the token of the user to search for.
the wikitty Id of the user corresponding to the securityToken
    public static String getUserForToken(WikittyService ws,
                                              String securityToken) {
        // recuperation de l'utilisateur associe au securityToken
        // le securityToken est aussi l'id de l'objet
        String userId = null;
        if (securityToken != null) {
            //Get the token
            Wikitty securityTokenWikitty = WikittyServiceEnhanced.restore(ws,
            if (securityTokenWikitty == null) {
                throw new SecurityException("bad (obsolete ?) token");
            } else {
                //Get the user
                userId = WikittyTokenHelper.getUser(securityTokenWikitty);
        return userId;

Create appAdminGroup and add current user as first member. The group has to be stored after as it is not persisted in this method.

user the user that will be the first admin group member
the admin group
    static public WikittyGroup createAppAdminGroup(WikittyUser user) {
        WikittyGroup result = new WikittyGroupImpl();
        String firstUserId = user.getWikittyId();
        return result;
    static public WikittyGroup getAppAdminGroup(WikittyClient client) {
        WikittyQuery criteria = new WikittyQueryMaker().and()
        WikittyGroup result = client.findByQuery(WikittyGroup.classcriteria);
        return result;
New to GrepCode? Check out our FAQ X