com.kimbrelk.privileges.User.java Source code

Java tutorial

Introduction

Here is the source code for com.kimbrelk.privileges.User.java

Source

package com.kimbrelk.privileges;

/*
Copyright (c) 2014 Karson Kimbrel
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import java.util.ArrayList;
import org.json.JSONObject;

import com.kimbrelk.privileges.util.interfaces.IJson;

/**
 * User privilege entry
 */
public class User extends PrivilegeHolder {
    private String groupName;

    public User(JSONObject json, int version) throws Exception {
        if (fromJSON(json, version).hasFailed())
            throw new Exception("Conversion from JSON failed.");
    }

    public User(String name) {
        super(name);
        this.groupName = null;
    }

    public User(String name, String groupName) {
        super(name);
        this.groupName = groupName;
    }

    public User(String name, String groupName, ArrayList<String> allowances, ArrayList<String> denials) {
        super(name, allowances, denials);
        this.groupName = groupName;
    }

    public Result fromJSON(JSONObject json, int version) {
        try {
            if (version > Privileges.JSON_VERSION)
                return IJson.Result.OUTDATED_APP;

            Result tResult = super.fromJSON(json, version);
            if (tResult.hasFailed())
                return tResult;

            if (version >= 1) {
                groupName = json.optString("group");
            }

            if (version < Privileges.JSON_VERSION)
                return IJson.Result.OUTDATED_FORMAT;

            return IJson.Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return IJson.Result.FAILURE;
        }
    }

    /**
     * @return The privilege group the user belongs to.
     */
    public final String getGroupName() {
        return groupName;
    }

    public JSONObject toJSON() {
        try {
            JSONObject json = super.toJSON();
            if (json == null)
                return null;

            json.putOpt("group", groupName);

            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Sets the privilege group that the user belongs to
     * @param groupName The name of the user's privilege group
     */
    public final void setGroupName(String groupName) {
        this.groupName = groupName;
    }
}