OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.users.rights;
15
 
41 ilm 16
import org.openconcerto.sql.model.SQLName;
17 ilm 17
import org.openconcerto.sql.model.SQLTable;
18
import org.openconcerto.sql.users.rights.UserRightsManager.RightTuple;
41 ilm 19
import org.openconcerto.utils.CompareUtils.Equalizer;
17 ilm 20
 
21
import java.util.ArrayList;
93 ilm 22
import java.util.Arrays;
17 ilm 23
import java.util.List;
24
 
25
public class TableAllRights extends MacroRight {
26
 
27
    public static final String CODE = "TABLE_ALL_RIGHTS";
28
    public static final String CODE_MODIF = "TABLE_ALL_MODIF_RIGHTS";
29
    public static final String DELETE_ROW_TABLE = "DELETE_ROW";
30
    public static final String MODIFY_ROW_TABLE = "UPDATE_ROW";
31
    public static final String ADD_ROW_TABLE = "INSERT_ROW";
32
    public static final String VIEW_ROW_TABLE = "SELECT_ROW";
41 ilm 33
    public static final String SAVE_ROW_TABLE = "SAVE_ROW";
174 ilm 34
    public static final String USER_UI_LOCK_ROW = "LIST_UI_LOCK_ROWS";
35
    public static final String USER_UI_UNLOCK_ROW = "LIST_UI_UNLOCK_ROWS";
17 ilm 36
 
93 ilm 37
    public static final List<String> getCodes() {
174 ilm 38
        return Arrays.asList(CODE, CODE_MODIF, DELETE_ROW_TABLE, MODIFY_ROW_TABLE, ADD_ROW_TABLE, VIEW_ROW_TABLE, SAVE_ROW_TABLE, USER_UI_UNLOCK_ROW, USER_UI_LOCK_ROW);
93 ilm 39
    }
40
 
17 ilm 41
    public static RightTuple createRight(final SQLTable t, final boolean b) {
42
        return createRight(CODE, t, b);
43
    }
44
 
45
    public static RightTuple createRight(final String code, final SQLTable t, final boolean b) {
46
        return new RightTuple(code, tableToString(t), b);
47
    }
48
 
41 ilm 49
    public static boolean currentUserHasRight(final String code, final SQLTable t) {
50
        return hasRight(UserRightsManager.getCurrentUserRights(), code, t);
51
    }
52
 
17 ilm 53
    public static boolean hasRight(final UserRights u, final String code, final SQLTable t) {
41 ilm 54
        return hasRightOnTableName(u, code, tableName(t, true));
17 ilm 55
    }
56
 
41 ilm 57
    static boolean hasRightOnTableName(final UserRights u, final String code, final SQLName tName) {
58
        final int correctNameCount = tName == null ? -1 : tName.getItemCount();
59
        assert tName == null || correctNameCount == 2 : "root.table";
60
        return u.haveRight(code, tName == null ? null : tName.quote(), new Equalizer<String>() {
61
 
62
            private SQLName[] names;
63
 
64
            public SQLName getName(int length) {
65
                if (this.names == null) {
66
                    this.names = new SQLName[correctNameCount];
67
                    SQLName current = tName;
68
                    for (int i = this.names.length - 1; i >= 0; i--) {
69
                        this.names[i] = current;
70
                        assert current.getItemCount() - 1 == i;
71
                        current = current.getRest();
72
                    }
73
                }
74
                return this.names[length - 1];
75
            }
76
 
77
            @Override
78
            public boolean equals(String o1, String o2) {
79
                // inexpensive match (not sufficient since name could have been quoted differently)
80
                if (o1.equals(o2))
81
                    return true;
82
 
83
                final SQLName rightTableName = SQLName.parse(o1);
84
                final int rightNameCount = rightTableName.getItemCount();
85
                if (rightNameCount > correctNameCount)
86
                    throw new IllegalArgumentException("names should not contain system root : " + rightTableName);
87
                return rightTableName.equals(getName(rightNameCount));
88
            }
89
        });
17 ilm 90
    }
91
 
41 ilm 92
    private static SQLName tableName(final SQLTable t, final boolean exact) {
93
        if (t == null)
94
            return null;
95
        return exact ? t.getSQLName(t.getDBRoot()) : new SQLName(t.getName());
96
    }
97
 
98
    public static String tableToString(final SQLTable t) {
99
        return tableToString(t, true);
100
    }
101
 
102
    public static String tableToString(final SQLTable t, final boolean exact) {
103
        return t == null ? null : tableName(t, exact).quote();
104
    }
105
 
17 ilm 106
    public TableAllRights() {
107
        this(true);
108
    }
109
 
110
    public TableAllRights(final boolean includeView) {
111
        super(includeView ? CODE : CODE_MODIF);
112
    }
113
 
114
    public void load(UserRights userRights, Boolean b) {
115
        throw new IllegalStateException();
116
    }
117
 
118
    @Override
119
    public List<RightTuple> expand(UserRightsManager mngr, String rightCode, String object, boolean haveRight) {
120
        final List<RightTuple> res = new ArrayList<RightTuple>();
121
        res.add(new RightTuple(DELETE_ROW_TABLE, object, haveRight));
122
        res.add(new RightTuple(MODIFY_ROW_TABLE, object, haveRight));
123
        res.add(new RightTuple(ADD_ROW_TABLE, object, haveRight));
174 ilm 124
        res.add(new RightTuple(USER_UI_LOCK_ROW, object, haveRight));
125
        res.add(new RightTuple(USER_UI_UNLOCK_ROW, object, haveRight));
17 ilm 126
        if (getCode() == CODE)
127
            res.add(new RightTuple(VIEW_ROW_TABLE, object, haveRight));
128
        return res;
129
    }
130
}