File indexing completed on 2024-05-12 05:17:12

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andras Mantia <amantia@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "acl.h"
0008 
0009 #include <QByteArray>
0010 #include <QMap>
0011 
0012 namespace KIMAP
0013 {
0014 namespace Acl
0015 {
0016 class RightsMap
0017 {
0018 public:
0019     RightsMap()
0020     {
0021         map['l'] = Lookup;
0022         map['r'] = Read;
0023         map['s'] = KeepSeen;
0024         map['w'] = Write;
0025         map['i'] = Insert;
0026         map['p'] = Post;
0027         map['c'] = Create; // TODO: obsolete, keep it?
0028         map['d'] = Delete; // TODO: obsolete, keep it?
0029         map['k'] = CreateMailbox;
0030         map['x'] = DeleteMailbox;
0031         map['t'] = DeleteMessage;
0032         map['e'] = Expunge;
0033         map['a'] = Admin;
0034         map['n'] = WriteShared;
0035         map['0'] = Custom0;
0036         map['1'] = Custom1;
0037         map['2'] = Custom2;
0038         map['3'] = Custom3;
0039         map['4'] = Custom4;
0040         map['5'] = Custom5;
0041         map['6'] = Custom6;
0042         map['7'] = Custom7;
0043         map['8'] = Custom8;
0044         map['9'] = Custom9;
0045     }
0046 
0047     QMap<char, Right> map;
0048 };
0049 
0050 Q_GLOBAL_STATIC(RightsMap, globalRights)
0051 
0052 }
0053 }
0054 
0055 KIMAP::Acl::Rights KIMAP::Acl::rightsFromString(const QByteArray &string)
0056 {
0057     Rights result;
0058 
0059     if (string.isEmpty()) {
0060         return result;
0061     }
0062 
0063     int pos = 0;
0064     if (string[0] == '+' || string[0] == '-') { // Skip modifier if any
0065         pos++;
0066     }
0067 
0068     for (int i = pos; i < string.size(); i++) {
0069         if (globalRights->map.contains(string[i])) {
0070             result |= globalRights->map[string[i]];
0071         }
0072     }
0073 
0074     return result;
0075 }
0076 
0077 QByteArray KIMAP::Acl::rightsToString(Rights rights)
0078 {
0079     QByteArray result;
0080 
0081     for (int right = Lookup; right <= Custom9; right <<= 1) {
0082         if (rights & right) {
0083             result += globalRights->map.key(static_cast<Right>(right));
0084         }
0085     }
0086 
0087     return result;
0088 }
0089 
0090 KIMAP::Acl::Rights KIMAP::Acl::normalizedRights(KIMAP::Acl::Rights rights)
0091 {
0092     Rights normalized = rights;
0093     if (normalized & Create) {
0094         normalized |= (CreateMailbox | DeleteMailbox);
0095         normalized &= ~Create;
0096     }
0097     if (normalized & Delete) {
0098         normalized |= (DeleteMessage | Expunge);
0099         normalized &= ~Delete;
0100     }
0101     return normalized;
0102 }
0103 
0104 KIMAP::Acl::Rights KIMAP::Acl::denormalizedRights(KIMAP::Acl::Rights rights)
0105 {
0106     Rights denormalized = normalizedRights(rights);
0107     if (denormalized & (CreateMailbox | DeleteMailbox)) {
0108         denormalized |= Create;
0109     }
0110     if (denormalized & (DeleteMessage | Expunge)) {
0111         denormalized |= Delete;
0112     }
0113     return denormalized;
0114 }