CrugeAuthManager.php 64.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
<?php
/**
CrugeAuthManager

este modulo requiere instalacion en config/main.php :
'components'=>array(
'authManager' => array(
'class' => 'application.modules.cruge.components.CrugeAuthManager',
),
),

para acceder a el:
Yii::app()->authManager
o
Yii::app()->user->rbac

para consultar si el usuario actual tiene permiso para alguna operacion:
if(Yii::app()->user->checkAccess('createPost')){...}


FUNCIONES EXTENDIDAS
====================

permiten listar items. no estan declaradas como parte de la interfaz original de Yii

Yii::app()->user->rbac->roles;
Yii::app()->user->rbac->tasks;
Yii::app()->user->rbac->operations;

SINTAXIS PARA LA DESCRIPCION DEL AUTHITEM / MENU ITEMS
======================================================

Para obtener menu items en base a RBAC se usa el metodo:

Yii::app()->user->rbac->getMenu([$args]);

Para que los menu items funcionen deben contener una SINTAXIS en
la DESCRIPCION como describo a continuacion.

[$args] son argumentos en forma de array para adosarle a las URL
de los menu items aqui obtenidos, leer mas abajo en este mismo apartado.

SINTAXIS

La descripcion del CAuthItem puede venir en dos formas:

A) Estandar.   	"Mi Descripcion"

B) Extendia.	":2 Menu Usuario {menu_principal} {action_site_index}"

El caso (A) no tiene mayor explicación, la descripcion se usa como
un texto informativo y nada mas.

El caso (B) representa la "Sintaxis de la Descripcion"

SU FORMA ES:

[:]+[nn]+[Texto]+[{auth_item_padre}{acion_auth_item_name}]

en donde:

":" 		indica que el CAuthItem es un MENU o un SUB MENU

"nn"		indica la posicion ordinal, si es un MENU o u SUB MENU

"Texto"		indica el texto del menu o submenu

{parent}	el nombre del CAuthItem superior (al existir este argumento
se considera al CAuthItem como un SUBMENU de "parent"

{action}	el nombre del CAuthItem que servirá como ACTION para la
url del sub menu item.

FORMAS POSIBLES:

":texto"
menu de primer nivel con etiqueta "Texto"

":N texto"
menu de primer nivel con etiqueta "Texto" en posicion N

":texto {parent} {action}"
menu de segundo nivel con etiqueta "Texto" relativo a "parent" quien
debe ser otro CAuthItem con sintaxis de descripcion, pero de tipo Menu.
siendo action el nombre del CAuthItem que va a lanzar la URL

":texto {parent}" este caso no tiene sentido. (Un submenu sin URL ?!)


MANEJO DE LA URL / MAPPINGS

La url se toma del nombre de un CAuthItem que ha sido indicada
como la seleccionada para el submenu item usando la sintaxis aqui
indicada.

ejemplo:

action item:			usada como:

action_site_index		array('site/index')

action_ui_editprofile	array('/cruge/ui/editprofile')

nota acerca de donde salio: "/cruge/ui/":

esta clase tiene un atributo llamado "mappings" el cual
va a convertir patrones de URL,

por ejemplo:

"action_ui_XXX" sera convertida a "action_cruge_ui_XXX"


URL: CASO MODULOS

El uso de mapping tambien ayuda para cuando los actions
declarados como "actions de menu item" apuntan a aquellos
que estan definidos en un modulo de usuario,

por ejemplo, tienes un action que realmente esta definido
dentro de un modulo llamado "tumodulox"

action_default_index

si no usas un mapping, este action se generara relativo a
la aplicacion sin modulo, es decir:

array('default/index')  Y DARA UN ERROR, porque
asume que lo que "para tu modulo" era el controller default
se le pedira a la aplicacion base y fallará porque no existe.

por tanto, se resolveria en config main pasandole a esta clase
un nuevo mapping:

'mapping' => array(
'action_ui_'=>'action_cruge_ui_',
'action_default_' => 'action_tumodulox_default_',
),

ahora, al invocar el menu se producira correctamente la URL
apuntando a tu modulo:

array('/tumodulox/default/index'),


ARGUMENTOS DE LA URL ($args)

La url recibe argumentos cuando se invoca a Yii::app()->rbac->getMenu

Por ejemplo, queremos que todos los menu items tengas adosado un
parametro (o mas):

Yii::app()->user->rbac->getMenu(array('idempresa'=>123));

esto generara un array de menu items (para CMenu o MbMenu o EMenu etc)
cuya url será finalmente asi:

array('label'=>'cosa', 'url'=>'', 'items'=>
array('cosa menor','url'=>array('site/index','idempresa'=>123))
..
..
)


EJEMPLO USO DE ESTA CLASE
=========================

$auth=Yii::app()->authManager;

$auth->createOperation('createPost','create a post');
$auth->createOperation('readPost','read a post');
$auth->createOperation('updatePost','update a post');
$auth->createOperation('deletePost','delete a post');

$bizRule='return Yii::app()->user->id==$params["post"]->authID;';
$task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule);
$task->addChild('updatePost');

$role=$auth->createRole('reader');
$role->addChild('readPost');

$role=$auth->createRole('author');
$role->addChild('reader');
$role->addChild('createPost');
$role->addChild('updateOwnPost');

$role=$auth->createRole('editor');
$role->addChild('reader');
$role->addChild('updatePost');

$role=$auth->createRole('admin');
$role->addChild('editor');
$role->addChild('author');
$role->addChild('deletePost');

// se asignan los roles a los usuarios, aqui el iduser es el nombre, pero puede (y debe)
// ser el Yii::app()->user->id (id=que invoca a user->getId())

$auth->assign('reader','readerA');
$auth->assign('author','authorB');
$auth->assign('editor','editorC');
$auth->assign('admin','adminD');

@author: original de Maurizio Domba <mdomba@gmail.com>
@author: Christian Salazar H. <christiansalazarh@gmail.com> @salazarchris74
@license protected/modules/cruge/LICENSE
 */
class CrugeAuthManager extends CAuthManager implements IAuthManager
{

    // este mapping es usado para el caso de obtener una URL en base a
    // un CAuthItem.getDescription() usando el mecanismo de  sintaxis
    // descrito mas abajo.
    //
    //	resuelve el problema de 'action_ui_editprofile' el cual
    //	realmente representa al action action_cruge_ui_editprofile
    //  al usar getTaskUrl se obtendrá: array('/cruge/ui/editprofile')
    //  en vez de: array('/ui/editprofile')
    //
    //	Importante:
    //	  este es un mapping de patrones, no de indices directos.
    //
    public $mapping = array(
        'action_ui_' => 'action_cruge_ui_',
    );
    private $_enumcontrollers;
    private $_enumactions;

    /**
     * @var string the ID of the {@link CDbConnection} application component. Defaults to 'db'.
     * The database must have the tables as declared in "framework/web/auth/*.sql".
     */
    public $connectionID = 'db';
    /**
     * @var CDbConnection the database connection. By default, this is initialized
     * automatically as the application component whose ID is indicated as {@link connectionID}.
     */
    public $db;


    public function init()
    {
        parent::init();
        $this->getDbConnection(); // para inicializar db
    }

    /** retorna el nombre de una tabla configurandola para los prefijos definidos en el modulo
    $table: uno de {'authitem', 'authitemchild', 'authassignment'}
     */
    public function getTableName($table)
    {
        return CrugeUtil::getTableName($table);
    }

    public function usingSqlite()
    {
        return false;
    }


    /**
     * Performs access check for the specified user.
     * @param string $itemName the name of the operation that need access check
     * @param mixed $userId the user ID. This should can be either an integer and a string representing
     * the unique identifier of a user. See {@link IWebUser::getId}.
     * @param array $params name-value pairs that would be passed to biz rules associated
     * with the tasks and roles assigned to the user.
     * @return boolean whether the operations can be performed by the user.
     */
    public function checkAccess($itemName, $userId, $params = array())
    {
        $assignments = $this->getAuthAssignments($userId);
        return $this->checkAccessRecursive($itemName, $userId, $params, $assignments);
    }

    /**
     * Performs access check for the specified user.
     * This method is internally called by {@link checkAccess}.
     * @param string $itemName the name of the operation that need access check
     * @param mixed $userId the user ID. This should can be either an integer and a string representing
     * the unique identifier of a user. See {@link IWebUser::getId}.
     * @param array $params name-value pairs that would be passed to biz rules associated
     * with the tasks and roles assigned to the user.
     * @param array $assignments the assignments to the specified user
     * @return boolean whether the operations can be performed by the user.
     * @since 1.1.3
     */
    protected function checkAccessRecursive($itemName, $userId, $params, $assignments)
    {
        if (($item = $this->getAuthItem($itemName)) === null) {
            return false;
        }
        Yii::trace('Checking permission "' . $item->getName() . '"', 'system.web.auth.CDbAuthManager');
        if ($this->executeBizRule($item->getBizRule(), $params, $item->getData())) {
            if (in_array($itemName, $this->defaultRoles)) {
                return true;
            }
            if (isset($assignments[$itemName])) {
                $assignment = $assignments[$itemName];
                if ($this->executeBizRule($assignment->getBizRule(), $params, $assignment->getData())) {
                    return true;
                }
            }
            $parents = $this->db->createCommand()
                ->select('parent')
                ->from($this->getTableName('authitemchild'))
                ->where('child=:name', array(':name' => $itemName))
                ->queryColumn();
            foreach ($parents as $parent) {
                if ($this->checkAccessRecursive($parent, $userId, $params, $assignments)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Adds an item as a child of another item.
     * @param string $itemName the parent item name
     * @param string $childName the child item name
     * @throws CException if either parent or child doesn't exist or if a loop has been detected.
     */
    public function addItemChild($itemName, $childName)
    {
        if ($itemName === $childName) {
            throw new CException(Yii::t(
                'yii',
                'Cannot add "{name}" as a child of itself.',
                array('{name}' => $itemName)
            ));
        }

        $rows = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authitem'))
            ->where(
            'name=:name1 OR name=:name2',
            array(
                ':name1' => $itemName,
                ':name2' => $childName
            )
        )
            ->queryAll();

        if (count($rows) == 2) {
            if ($rows[0]['name'] === $itemName) {
                $parentType = $rows[0]['type'];
                $childType = $rows[1]['type'];
            } else {
                $childType = $rows[0]['type'];
                $parentType = $rows[1]['type'];
            }
            $this->checkItemChildType($parentType, $childType);
            if ($this->detectLoop($itemName, $childName)) {
                throw new CrugeException(Yii::t(
                    'yii',
                    'Cannot add "{child}" as a child of "{name}". A loop has been detected.',
                    array('{child}' => $childName, '{name}' => $itemName)
                ));
            }

            $this->db->createCommand()
                ->insert(
                $this->getTableName('authitemchild'),
                array(
                    'parent' => $itemName,
                    'child' => $childName,
                )
            );
        } else {
            throw new CrugeException(Yii::t(
                'yii',
                'Either "{parent}" or "{child}" does not exist.',
                array('{child}' => $childName, '{parent}' => $itemName)
            ));
        }
    }

    /**
     * Removes a child from its parent.
     * Note, the child item is not deleted. Only the parent-child relationship is removed.
     * @param string $itemName the parent item name
     * @param string $childName the child item name
     * @return boolean whether the removal is successful
     */
    public function removeItemChild($itemName, $childName)
    {
        return $this->db->createCommand()
            ->delete(
            $this->getTableName('authitemchild'),
            'parent=:parent AND child=:child',
            array(
                ':parent' => $itemName,
                ':child' => $childName
            )
        ) > 0;
    }

    /**
     * Returns a value indicating whether a child exists within a parent.
     * @param string $itemName the parent item name
     * @param string $childName the child item name
     * @return boolean whether the child exists
     */
    public function hasItemChild($itemName, $childName)
    {
        return $this->db->createCommand()
            ->select('parent')
            ->from($this->getTableName('authitemchild'))
            ->where(
            'parent=:parent AND child=:child',
            array(
                ':parent' => $itemName,
                ':child' => $childName
            )
        )
            ->queryScalar() !== false;
    }

    /**
     * Returns the children of the specified item.
     * @param mixed $names the parent item name. This can be either a string or an array.
     * The latter represents a list of item names (available since version 1.0.5).
     * @return array all child items of the parent
     */
    public function getItemChildren($names)
    {
        if (is_string($names)) {
            $condition = 'parent=' . $this->db->quoteValue($names);
        } else {
            if (is_array($names) && $names !== array()) {
                foreach ($names as &$name) {
                    $name = $this->db->quoteValue($name);
                }
                $condition = 'parent IN (' . implode(', ', $names) . ')';
            }
        }

        $rows = $this->db->createCommand()
            ->select('name, type, description, bizrule, data')
            ->from(
            array(
                $this->getTableName('authitem'),
                $this->getTableName('authitemchild')
            )
        )
            ->where($condition . ' AND name=child')
            ->queryAll();

        $children = array();
        foreach ($rows as $row) {
            if (($data = @unserialize($row['data'])) === false) {
                $data = null;
            }
            $children[$row['name']] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data);
        }
        return $children;
    }

    /**
     * Assigns an authorization item to a user.
     * @param string $itemName the item name
     * @param mixed $userId the user ID (see {@link IWebUser::getId})
     * @param string $bizRule the business rule to be executed when {@link checkAccess} is called
     * for this particular authorization item.
     * @param mixed $data additional data associated with this assignment
     * @return CAuthAssignment the authorization assignment information.
     * @throws CrugeException if the item does not exist or if the item has already been assigned to the user
     */
    public function assign($itemName, $userId, $bizRule = null, $data = null)
    {
        /*
        if($this->usingSqlite() && $this->getAuthItem($itemName)===null)
            throw new CrugeException(Yii::t('yii','The item "{name}" does not exist.',array('{name}'=>$itemName)));
        */

        // por christian salazar
        if ($userId == '' || $userId == null) {
            return null;
        }

        $this->db->createCommand()
            ->insert(
            $this->getTableName('authassignment'),
            array(
                'itemname' => $itemName,
                'userid' => $userId,
                'bizrule' => $bizRule,
                'data' => serialize($data)
            )
        );
        return new CAuthAssignment($this, $itemName, $userId, $bizRule, $data);
    }

    /**
     * Revokes an authorization assignment from a user.
     * @param string $itemName the item name
     * @param mixed $userId the user ID (see {@link IWebUser::getId})
     * @return boolean whether removal is successful
     */
    public function revoke($itemName, $userId)
    {
        return $this->db->createCommand()
            ->delete(
            $this->getTableName('authassignment'),
            'itemname=:itemname AND userid=:userid',
            array(
                ':itemname' => $itemName,
                ':userid' => $userId
            )
        ) > 0;
    }

    /**
     * Returns a value indicating whether the item has been assigned to the user.
     * @param string $itemName the item name
     * @param mixed $userId the user ID (see {@link IWebUser::getId})
     * @return boolean whether the item has been assigned to the user.
     */
    public function isAssigned($itemName, $userId)
    {
        return $this->db->createCommand()
            ->select('itemname')
            ->from($this->getTableName('authassignment'))
            ->where(
            'itemname=:itemname AND userid=:userid',
            array(
                ':itemname' => $itemName,
                ':userid' => $userId
            )
        )
            ->queryScalar() !== false;
    }

    /**
     * Returns the item assignment information.
     * @param string $itemName the item name
     * @param mixed $userId the user ID (see {@link IWebUser::getId})
     * @return CAuthAssignment the item assignment information. Null is returned if
     * the item is not assigned to the user.
     */
    public function getAuthAssignment($itemName, $userId)
    {
        $row = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authassignment'))
            ->where(
            'itemname=:itemname AND userid=:userid',
            array(
                ':itemname' => $itemName,
                ':userid' => $userId
            )
        )
            ->queryRow();
        if ($row !== false) {
            if (($data = @unserialize($row['data'])) === false) {
                $data = null;
            }
            return new CAuthAssignment($this, $row['itemname'], $row['userid'], $row['bizrule'], $data);
        } else {
            return null;
        }
    }


    /**
     * Retorna un array con los userid que tienen el item asignado
     * @param string $itemName el item a buscar
     * @return array con los $userid
     */
    public function getUsersAssigned($itemName)
    {
        $rows = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authassignment'))
            ->where(
            'itemname=:itemname ',
            array(
                ':itemname' => $itemName,
            )
        )
        //->group('userid')
            ->queryAll();
        $users = array();
        if ($rows != null) {
            foreach ($rows as $row) {
                if (!in_array($row['userid'], $users)) {
                    $users[] = $row['userid'];
                }
            }
        }
        return $users;
    }


    /**
     * Retorna un array con todos los "parents" de un item hallados en authitemchild
     *
     * este metodo permite ir hacia atras, lo opuesto a getChildrens, permitiendo conocer
     * quienes hacen referencia a un authItem
     *
     * @param string $itemName el item a buscar
     * @return array con los CAuthItems que hacen la referencia al item.
     */
    public function getParents($itemName)
    {
        $rows = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authitemchild'))
            ->where(
            'child=:itemname ',
            array(
                ':itemname' => $itemName,
            )
        )
            ->queryAll();
        $parents = array();
        if ($rows != null) {
            foreach ($rows as $row) {
                $parents[] = $this->getAuthItem($row['parent']);
            }
        }
        return $parents;
    }


    /**
     * Retorna el numero de userid's que tienen el item asignado
     * @param string $itemName el item a buscar
     * @return cantidad de usuarios asignados
     */
    public function getCountUsersAssigned($itemName)
    {
        // TODO: optimizar esto con una consulta de agrupacion y cuenta
        //
        $ar = $this->getUsersAssigned($itemName);
        return count($ar);
    }

    /**
     * Returns the item assignments for the specified user.
     * @param mixed $userId the user ID (see {@link IWebUser::getId})
     * @return array the item assignment information for the user. An empty array will be
     * returned if there is no item assigned to the user.
     */
    public function getAuthAssignments($userId)
    {
        $rows = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authassignment'))
            ->where('userid=:userid', array(':userid' => $userId))
            ->queryAll();
        $assignments = array();
        foreach ($rows as $row) {
            if (($data = @unserialize($row['data'])) === false) {
                $data = null;
            }
            $assignments[$row['itemname']] = new CAuthAssignment($this, $row['itemname'], $row['userid'], $row['bizrule'], $data);

        }
        return $assignments;
    }

    /**
     * Saves the changes to an authorization assignment.
     * @param CAuthAssignment $assignment the assignment that has been changed.
     */
    public function saveAuthAssignment($assignment)
    {
        $this->db->createCommand()
            ->update(
            $this->getTableName('authassignment'),
            array(
                'bizrule' => $assignment->getBizRule(),
                'data' => serialize($assignment->getData()),
            ),
            'itemname=:itemname AND userid=:userid',
            array(
                'itemname' => $assignment->getItemName(),
                'userid' => $assignment->getUserId()
            )
        );
    }

    /**
     * Returns the authorization items of the specific type and user.
     * @param integer $type the item type (0: operation, 1: task, 2: role). Defaults to null,
     * meaning returning all items regardless of their type.
     * @param mixed $userId the user ID. Defaults to null, meaning returning all items even if
     * they are not assigned to a user.
     * @return array the authorization items of the specific type.
     */
    public function getAuthItems($type = null, $userId = null)
    {
        if ($type === null && $userId === null) {
            $command = $this->db->createCommand()
                ->select()
                ->from($this->getTableName('authitem'));
        } else {
            if ($userId === null) {
                $command = $this->db->createCommand()
                    ->select()
                    ->from($this->getTableName('authitem'))
                    ->where('type=:type', array(':type' => $type));
            } else {
                if ($type === null) {
                    $command = $this->db->createCommand()
                        ->select('name,type,description,t1.bizrule,t1.data')
                        ->from(
                        array(
                            $this->getTableName('authitem') . ' t1',
                            $this->getTableName('authassignment') . ' t2'
                        )
                    )
                        ->where('name=itemname AND userid=:userid', array(':userid' => $userId));
                } else {
                    $command = $this->db->createCommand()
                        ->select('name,type,description,t1.bizrule,t1.data')
                        ->from(
                        array(
                            $this->getTableName('authitem') . ' t1',
                            $this->getTableName('authassignment') . ' t2'
                        )
                    )
                        ->where(
                        'name=itemname AND type=:type AND userid=:userid',
                        array(
                            ':type' => $type,
                            ':userid' => $userId
                        )
                    );
                }
            }
        }
        $items = array();
        foreach ($command->queryAll() as $row) {
            if (($data = @unserialize($row['data'])) === false) {
                $data = null;
            }
            $items[$row['name']] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data);
        }
        return $items;
    }

    /**
     * Creates an authorization item.
     * An authorization item represents an action permission (e.g. creating a post).
     * It has three types: operation, task and role.
     * Authorization items form a hierarchy. Higher level items inheirt permissions representing
     * by lower level items.
     * @param string $name the item name. This must be a unique identifier.
     * @param integer $type the item type (0: operation, 1: task, 2: role).
     * @param string $description description of the item
     * @param string $bizRule business rule associated with the item. This is a piece of
     * PHP code that will be executed when {@link checkAccess} is called for the item.
     * @param mixed $data additional data associated with the item.
     * @return CAuthItem the authorization item
     * @throws CrugeException if an item with the same name already exists
     */
    public function createAuthItem($name, $type, $description = '', $bizRule = null, $data = null)
    {
        $this->db->createCommand()
            ->insert(
            $this->getTableName('authitem'),
            array(
                'name' => $name,
                'type' => $type,
                'description' => $description,
                'bizrule' => $bizRule,
                'data' => serialize($data)
            )
        );
        return new CAuthItem($this, $name, $type, $description, $bizRule, $data);
    }

    /**
     * Removes the specified authorization item.
     * @param string $name the name of the item to be removed
     * @return boolean whether the item exists in the storage and has been removed
     */
    public function removeAuthItem($name)
    {
        if ($this->usingSqlite()) {
            $this->db->createCommand()
                ->delete(
                $this->getTableName('authitemchild'),
                'parent=:name1 OR child=:name2',
                array(
                    ':name1' => $name,
                    ':name2' => $name
                )
            );
            $this->db->createCommand()
                ->delete(
                $this->getTableName('authassignment'),
                'itemname=:name',
                array(
                    ':name' => $name,
                )
            );
        }

        return $this->db->createCommand()
            ->delete(
            $this->getTableName('authitem'),
            'name=:name',
            array(
                ':name' => $name
            )
        ) > 0;
    }

    /**
     * Returns the authorization item with the specified name.
     * @param string $name the name of the item
     * @return CAuthItem the authorization item. Null if the item cannot be found.
     */
    public function getAuthItem($name)
    {
        $row = $this->db->createCommand()
            ->select()
            ->from($this->getTableName('authitem'))
            ->where('name=:name', array(':name' => $name))
            ->queryRow();

        if ($row !== false) {
            if (($data = @unserialize($row['data'])) === false) {
                $data = null;
            }
            return new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data);
        } else {
            return null;
        }
    }

    /**
     * Saves an authorization item to persistent storage.
     * @param CAuthItem $item the item to be saved.
     * @param string $oldName the old item name. If null, it means the item name is not changed.
     */
    public function saveAuthItem($item, $oldName = null)
    {
        if ($this->usingSqlite() && $oldName !== null && $item->getName() !== $oldName) {
            $this->db->createCommand()
                ->update(
                $this->getTableName('authitemchild'),
                array(
                    'parent' => $item->getName(),
                ),
                'parent=:whereName',
                array(
                    ':whereName' => $oldName,
                )
            );
            $this->db->createCommand()
                ->update(
                $this->getTableName('authitemchild'),
                array(
                    'child' => $item->getName(),
                ),
                'child=:whereName',
                array(
                    ':whereName' => $oldName,
                )
            );
            $this->db->createCommand()
                ->update(
                $this->getTableName('authassignment'),
                array(
                    'itemname' => $item->getName(),
                ),
                'itemname=:whereName',
                array(
                    ':whereName' => $oldName,
                )
            );
        }

        $this->db->createCommand()
            ->update(
            $this->getTableName('authitem'),
            array(
                'name' => $item->getName(),
                'type' => $item->getType(),
                'description' => $item->getDescription(),
                'bizrule' => $item->getBizRule(),
                'data' => serialize($item->getData()),
            ),
            'name=:whereName',
            array(
                ':whereName' => $oldName === null ? $item->getName() : $oldName,
            )
        );

        // extra: manejo de sintaxis.
        //
        //	cuando un CAuthItem se guarda, se asegura que en caso de ser
        //  un item tipo TASK que usa sintaxis de descripcion entonces
        //	vigile el menuitem superior, asignando o desasignando automatica-
        //	-mente a este TASK con las tareas superior.


        if ($item->getType() == CAuthItem::TYPE_TASK) {
            if ($this->isTaskSubMenuItem($item)) {
                // es un submenu de otra tarea $parent
                $parent = $this->getParentMenuAuthItem($item);
                if ($parent != null) {
                    // no es huerfana
                    // a inserta a $item como hija de $parent
                    if (!$this->hasItemChild($parent->name, $item->name)) {
                        $this->addItemChild($parent->name, $item->name);
                    }
                }
            }
        }

    }

    /**
     * Saves the authorization data to persistent storage.
     */
    public function save()
    {
    }

    /**
     * Removes all authorization data.
     */
    public function clearAll()
    {
        $this->clearAuthAssignments();
        $this->db->createCommand()->delete($this->getTableName('authitemchild'));
        $this->db->createCommand()->delete($this->getTableName('authitem'));
    }

    /**
     * Removes all authorization assignments.
     */
    public function clearAuthAssignments()
    {
        $this->db->createCommand()->delete($this->getTableName('authassignment'));
    }

    /**
     * Checks whether there is a loop in the authorization item hierarchy.
     * @param string $itemName parent item name
     * @param string $childName the name of the child item that is to be added to the hierarchy
     * @return boolean whether a loop exists
     */
    public function detectLoop($itemName, $childName)
    {
        if ($childName === $itemName) {
            return true;
        }
        foreach ($this->getItemChildren($childName) as $child) {
            if ($this->detectLoop($itemName, $child->getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return CDbConnection the DB connection instance
     * @throws CrugeException if {@link connectionID} does not point to a valid application component.
     */
    protected function getDbConnection()
    {
        if ($this->db !== null) {
            return $this->db;
        } else {
            if (($this->db = Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection) {
                return $this->db;
            } else {
                throw new CrugeException(Yii::t(
                    'yii',
                    'CDbAuthManager.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
                    array('{id}' => $this->connectionID)
                ));
            }
        }
    }


    /* extension:  no pertenece a la interfaz

    */
    public function getAuthItemTypeName($type, $booleanPlural = false)
    {
        if ($type == CAuthItem::TYPE_ROLE) {
            return $booleanPlural == false ? "rol" : "roles";
        }
        if ($type == CAuthItem::TYPE_TASK) {
            return $booleanPlural == false ? "tarea" : "tareas";
        }
        if ($type == CAuthItem::TYPE_OPERATION) {
            return $booleanPlural == false ? "operacion" : "operaciones";
        }
        return $type;
    }

    public function nextType(CAuthItem $item)
    {
        if ($item->type == CAuthItem::TYPE_ROLE) {
            return CAuthItem::TYPE_TASK;
        }
        if ($item->type == CAuthItem::TYPE_TASK) {
            return CAuthItem::TYPE_OPERATION;
        }
        return null;
    }

    public function getRoles($userId = null)
    {
        return $this->getAuthItems(CAuthItem::TYPE_ROLE);
    }

    public function getTasks($userId = null)
    {
        return $this->getAuthItems(CAuthItem::TYPE_TASK);
    }

    public function getOperations($userId = null)
    {
        return $this->getAuthItems(CAuthItem::TYPE_OPERATION);
    }

    private function _filtroCruge($ops)
    {
        $crugeKey = "action_cruge_ui_";
        $ar = array();
        foreach ($ops as $op) {
            $actionFull = $this->_mapAction($op->name, $this->mapping);
            // ejemplo: convirtio action_ui_editprofile
            //			a : 	  action_cruge_ui_editprofile
            if (substr($actionFull, 0, strlen($crugeKey)) == $crugeKey) {
                $ar[] = $op;
            }
        }
        return $ar;
    }

    private function _filtroNoCruge($ops)
    {
        $crugeKey = "action_cruge_ui_";
        $ar = array();
        foreach ($ops as $op) {
            $actionFull = $this->_mapAction($op->name, $this->mapping);
            // ejemplo: convirtio action_ui_editprofile
            //			a : 	  action_cruge_ui_editprofile
            if (substr($actionFull, 0, strlen($crugeKey)) != $crugeKey) {
                $ar[] = $op;
            }
        }
        return $ar;
    }

    private function _filtroNoController($ops)
    {
        $ar = array();
        $arcontrollers = array();
        foreach ($this->enumControllers() as $controllerName) {
            $arcontrollers[] = strtolower("action_" . $controllerName . "_");
        }

        foreach ($ops as $op) {
            $found = false;
            // busca a ver si esta operacion esta en la lista
            // de controllers definidos.
            foreach ($arcontrollers as $cn) {
                if (substr($op->name, 0, strlen($cn)) == $cn) {
                    $found = true;
                    break;
                }
            }
            // es una operacion no asociada a un controller
            // por tanto le damos paso en este filtro
            if (!$found) {
                $ar[] = $op;
            }
        }
        return $ar;
    }

    // solo aquellas operaciones: "controller_site"
    // (acceso maestro a controllers)
    private function _filtroControllerMaestro($ops)
    {
        $ar = array();
        $arcontrollers = array();
        foreach ($this->enumControllers() as $controllerName) {
            $arcontrollers[] = strtolower("controller_" . $controllerName);
        }
        $arcontrollers[] = "controller_ui"; // cruge

        foreach ($ops as $op) {
            $found = false;
            // busca a ver si esta operacion esta en la lista
            // de controllers definidos.
            foreach ($arcontrollers as $cn) {
                if (substr($op->name, 0, strlen($cn)) == $cn) {
                    $found = true;
                }
            }
            // es una operacion no asociada a un controller
            // por tanto le damos paso en este filtro
            if ($found == true) {
                $ar[] = $op;
            }
        }
        return $ar;
    }

    private function _filtroNotControllerMaestro($ops)
    {
        $ar = array();
        $arcontrollers = array();
        foreach ($this->enumControllers() as $controllerName) {
            $arcontrollers[] = strtolower("controller_" . $controllerName);
        }
        $arcontrollers[] = "controller_ui"; // cruge
        foreach ($ops as $op) {
            $found = false;
            // busca a ver si esta operacion esta en la lista
            // de controllers definidos.
            foreach ($arcontrollers as $cn) {
                if (substr($op->name, 0, strlen($cn)) == $cn) {
                    $found = true;
                }
            }
            // es una operacion no asociada a un controller
            // por tanto le damos paso en este filtro
            if ($found == false) {
                $ar[] = $op;
            }
        }
        return $ar;
    }

    private function _filtroControllerName($ops, $controllerName)
    {
        $key = '_' . strtolower($controllerName) . '_';
        $ar = array();
        foreach ($ops as $item) {
            if (strstr($item->name, $key)) {
                $ar[] = $item;
            }
        }
        return $ar;
    }


    /**
     * getOperationsFiltered
     *    entrega un array de CAuthItem de tipo "operacion" pero en base a
     *    un filtro:
     *
     *        0 => todas
     *        1 => aquellas definidas en codigo
     *        2 => las de cruge
     *        3 => solo contollers maestro
     *        X => solo las del controller name = X
     *
     * @param string $filter
     * @access public
     * @return array de CAuthItem
     */
    public function getOperationsFiltered($filter, $oprList = null)
    {
        $ar = array();
        if ($oprList == null) {
            $oprList = $this->getOperations();
        }
        if (($filter == '') || ($filter == '0')) {
            // entrega el array completo de operaciones
            $ar = $oprList;
        } elseif ($filter == '1') {
            // OTRAS
            // aquellas que no pertenecen a ningun controller especifico
            // 	(porque no indican ningun "action_controller_" conocido)
            // y que tampoco son de Cruge..
            $ar = $this->_filtroNotControllerMaestro(
                $this->_filtroNoCruge(
                    $this->_filtroNoController($oprList)
                )
            );
        } elseif ($filter == '2') {
            // CRUGE
            // aqui se usa el $this->mapping tambien para reconocer
            // los actions de Cruge
            $ar = $this->_filtroCruge($oprList);
        } elseif ($filter == '3') {
            // CONTROLLERS
            // solo aquellos controllers maestros
            $ar = $this->_filtroControllerMaestro(
                $this->_filtroNoController($oprList)
            );
        } else {
            // CONTROLLER X
            // aquellas que coinciden con un nombre de controller seleccion.
            $ar = $this->_filtroControllerName($oprList, $filter);
        }
        return $ar;
    }


    public function getDataProviderRoles($pageSize = 20)
    {
        return new CArrayDataProvider($this->getRoles(), array(
            'keyField' => 'name',
            'sort' => array(
                'defaultOrder' => array('name'),
            ),
            'pagination' => array(
                'pageSize' => $pageSize,
            ),
        ));
    }

    public function getDataProviderTasks($pageSize = 50)
    {
        return new CArrayDataProvider(
            $this->reorderItemArray($this->getTasks())
            , array(
                'keyField' => 'name',
                'sort' => array(
                    'defaultOrder' => array('name'),
                ),
                'pagination' => array(
                    'pageSize' => $pageSize,
                ),
            ));
    }

    /**
     * getDataProviderOperations
     *    entrega un dataprovider segun el filtro seleccionado.
     *
     * @see getOperationsFiltered
     *
     * @param string $filter
     * @param int $pageSize
     * @access public
     * @return CArrayDataProvider de elementos CAuthItem
     */
    public function getDataProviderOperations($filter = '', $pageSize = 50)
    {
        return new CArrayDataProvider($this->getOperationsFiltered($filter)
            , array(
                'keyField' => 'name',
                'sort' => array(
                    'defaultOrder' => array('name'),
                ),
                'pagination' => array(
                    'pageSize' => $pageSize,
                ),
            ));
    }

    public function getRolesAsOptions($emptyLabel = null)
    {
        $ar = array();
        if ($emptyLabel != null) {
            $ar[''] = $emptyLabel;
        }

        foreach ($this->roles as $rol) {
            $ar[$rol->name] = $rol->name;
        }
        return $ar;
    }


    // FUNCIONES PARA EL MANEJO DE SINTAXIS APLICADA A LA DESCRIPCION
    // DEL AUTH ITEM:
    //
    //
    //

    /**
     * isItem
     *     detecta si la descripcion del item indica que es un menu (o submenu).
     *     lo hace buscando el simbolo ":" al inicio de la descripcion
     * @param mixed $obj
     * @access private
     * @return boolean true es un menu o un submenuitem.
     */
    private function isItem($obj)
    {
        $d = trim($obj->getDescription());
        if (strlen($d) > 0) {
            return ($d[0] == ':') ? true : false;
        }
        return false;
    }


    /**
     * isSubItem
     *    detecta si el item es un submenu, primero preguntando si isItem()
     *    y finalmente preguntando si contiene caracteres { }
     *
     * @param mixed $obj
     * @access private
     * @return bool true si es un subitem.
     */
    public function isSubItem($obj)
    {
        $d = trim($obj->getDescription());
        if (!$this->isItem($obj)) {
            return false;
        }
        // sin mucho analisis lexico-sintaxis...
        // asi facilito..
        return strstr($d, "{") && strstr($d, "}");
    }


    /**
     * getTaskText
     *    devuelve la descripcion pura de un CAuthItem considerando la
     *    sintaxis:
     *
     *        ":Descripcion Pura{menu_padre}{action_xxx}"
     *
     *        entregando de aqui solo a: "Descripcion Pura"
     *
     * @param mixed $obj
     * @access private
     * @return void
     */
    public function getTaskText($obj)
    {
        return $this->_getTextFromDescription($obj->getDescription());
    }

    /**
     * getItemPosition
     *    obtiene de la descripcion el argumento numerico de posicion tras
     *    el simbolo inicial ":".
     *      ejemplo ":123 mi item" devolvera: 123, sino 0
     *
     * @param mixed $obj
     * @access private
     * @return integer
     */
    public function getItemPosition($obj)
    {
        $d = trim(ltrim($obj->getDescription(), ':'));
        $dig = '';
        for ($i = 0; $i < strlen($d); $i++) {
            if (ctype_digit($d[$i])) {
                $dig .= $d[$i];
            } else {
                return ($dig) * 1;
            }
        }
        return null;
    }


    /**
     * _getParentMenuName
     *    de un texto ":hola {menu_item}" devolvera: "menu_item"
     * @param mixed $m
     * @access private
     * @return void
     */
    private function _getParentMenuName($m)
    {
        if ($m == null) {
            return "";
        }
        if (strlen($m) == 0) {
            return "";
        }
        $r = "";
        $s = 0;
        for ($i = 0; $i < strlen($m); $i++) {
            if ($s == 0) {
                if ($m[$i] == '{') {
                    $s = 1;
                }
            } elseif ($s == 1) {
                if ($m[$i] == '}') {
                    return trim($r);
                } elseif ($m[$i] == '{') {
                    $r = "";
                } else {
                    $r .= $m[$i];
                }
            }
        }
        return trim($r);
    }

    /**
     * _getActionItemName
     *    obtiene el contenido de la segunda llave en la sintaxis de la descr
     *        ejemplo:
     *            $m = "blabla {perrito} y {gatico}";
     *        retorna:
     *            "gatico" (la segunda llave)
     * @param string $m  contenido de la descripcion
     * @access protected
     * @return string
     */
    private function _getActionItemName($m)
    {
        if ($m == null) {
            return "";
        }
        if (strlen($m) == 0) {
            return "";
        }
        $r = "";
        $s = 0;
        for ($i = 0; $i < strlen($m); $i++) {
            if ($s == 0) {
                if ($m[$i] == '{') {
                    $s = 1;
                }
            } elseif ($s == 1) {
                if ($m[$i] == '}') {
                    $s = 2;
                }
            } elseif ($s == 2) {
                if ($m[$i] == '{') {
                    $s = 3;
                    $r = '';
                }
            } elseif ($s == 3) {
                if ($m[$i] == '}') {
                    return trim($r);
                } elseif ($m[$i] == '{') {
                    $r = '';
                } else {
                    $r .= $m[$i];
                }
            }
        }
        return trim($r);
    }

    private function _getTextFromDescription($description)
    {
        // limpia cualquier ":" delante
        $d = trim(ltrim($description, ':'));
        // pasa el numero que pudiese venir a continuacion
        $p = 0;
        for ($i = 0; $i < strlen($d); $i++) {
            if (ctype_digit($d[$i])) {
                continue;
            } else {
                $p = $i;
                break;
            }
        }

        if ($p == 0) {
            // no hay numero
        } else {
            // si hay numero, descripcion continua en posicion $p
            $d = substr($d, $p);
        }

        // busca hasta algun posible "{"
        $tmp = "";
        for ($i = 0; $i < strlen($d); $i++) {
            if ($d[$i] != '{') {
                $tmp .= $d[$i];
            } else {
                break;
            }
        }

        return trim($tmp);
    }

    private function _mapAction($action, $mappings)
    {
        if (trim($action) == "") {
            return "";
        }
        foreach ($mappings as $map => $xy) {
            if (substr($action, 0, strlen($map)) == $map) {
                return $xy . substr($action, strlen($map));
            }
        }
        return $action;
    }


    public function isTaskMenuItem($obj)
    {
        return $this->isItem($obj);
    }

    public function isTaskSubMenuItem($obj)
    {
        return $this->isSubItem($obj);
    }

    public function isTaskTopMenuItem($obj)
    {
        $n = trim($obj->getName());
        if ($this->isItem($obj)) {
            if (strpos($n, "menu_")===0) {
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    /**
     * getTaskParentMenuName
     *    de una tarea con descripcion: ":Menu1 {menu_principal}" devolvera:
     *        el string "menu_principal"
     * @param mixed $obj
     * @access public
     * @return string  o ""
     */
    public function getTaskParentMenuName($obj)
    {
        if ($this->isTaskSubMenuItem($obj)) {
            return $this->_getParentMenuName($obj->getDescription());
        } else {
            return "";
        }
    }

    /**
     * getParentMenuAuthItem
     *    pregunta si el CAuthItem $obj tiene un itename padre en su sintaxis
     *    de descripcion y si este item name existe como un CAuthItem.
     * @param CAuthItem $obj el authitem a consultar.
     * @access public
     * @return CAuthItem del padre dado por la sintaxis de la descripcion.
     */
    public function getParentMenuAuthItem($obj)
    {
        $itemname_padre = $this->getTaskParentMenuName($obj);
        return $this->getAuthItem($itemname_padre);
    }

    /**
     * getTaskActionItemName
     *    de una tarea con descripcion: ":Menu1 {menu_principal} {action_site_index}"
     *         devolvera:
     *            "action_site_index" (el itemname de aquella operacion "child"
    que sera usada como disparador del menu)
     *
     * @param mixed $obj
     * @access public
     * @return string o ""
     */
    public function getTaskActionItemName($obj)
    {
        if ($this->isTaskSubMenuItem($obj)) {
            return $this->_getActionItemName($obj->getDescription());
        } else {
            return "";
        }
    }

    /**
     * getTaskUrl
     *    de aquella tarea que usa sintaxis en su descripcion para manejar menues
     *    retorna la parte que hace referencia al auth item name del action
     *    seleccionado por el usuario para responder para este menu.
     *
     * @param CAuthItem $obj la tarea
     * @param array $args Argumentos en forma de array para adosar
     * @access public
     * @return array url en forma de array
     */
    public function getTaskUrl($obj, $args = null)
    {

        $itemname = $this->getTaskActionItemName($obj);
        if ($itemname == '') {
            return '';
        }

        // ver si hay un mapping para el action
        //	este no es un mapping tradicional, sino de patrones:
        //		ejemplo:
        //		mapping: "action_ui_" cambiara por "action_cruge_ui_"
        //
        $itemname = $this->_mapAction($itemname, $this->mapping);
        
        // ejemplo, recibe: action_site_index
        // lo descompone en array('site/index', ..$args..)
        $e = explode('_', $itemname);
        if (sizeof($e) == 3) {
            $controllerName = $e[1];
            $actionName = $e[2];
            $a = array();
            $a[] = "/" . $controllerName . "/" . $actionName;
            foreach ($args as $k => $v) {
                $a[$k] = $v;
            }
            return $a;
        } elseif (sizeof($e) == 4) {
            $moduleName = $e[1];
            $controllerName = $e[2];
            $actionName = $e[3];
            $a = array();
            $a[] = "/".$moduleName . '/' . $controllerName . "/" . $actionName;
            foreach ($args as $k => $v) {
                $a[$k] = $v;
            }
            return $a;
        } else {
            return array();
        }
    }

    /**
     * explodeTask
     *    descompone la descripcion de un TASK en sus partes segun sintaxis:
     *        ":POS descripcion{menu}{action}"
     *
     * @param CAuthItem $obj
     * @access public
     * @return array indexado array('description','position','menu','action')
     */
    public function explodeTask($obj)
    {
        return array(
            'description' => $this->getTaskText($obj),
            'position' => $this->getItemPosition($obj),
            'menu' => $this->getTaskParentMenuName($obj),
            'action' => $this->getTaskActionItemName($obj),
        );
    }

    /**
     * setTaskAction
     *    Actualiza un CAuthItem ($obj), le pondrá en su descripcion (usando la
     *    sintaxis para menues) al nuevo action, considerando cualquier action
     *    existente.
     *
     *    ejemplo:
     *        descripcion original: ":Subitem X {menu_prinicipal}"
     *        el $action_item_name es: "action_site_prueba"
     *        el resultado de la descripcion final sera:
     *            ":Subitem X {menu_prinicipal} {action_site_prueba}"
     *
     *            ***** EL CAuthItem sera Actualizado *****
     *
     * @param mixed $obj el objeto CAuthItem a quien se le modificara la descr.
     * @param mixed $action_item_name El action a usar (itemname del CAuthItem)
     * @access public
     * @return void
     */
    public function setTaskAction($obj, $action_item_name)
    {
        $ar = $this->explodeTask($obj);
        $newDescr = ":" . $ar['position'] . " " . $ar['description']
            . "{" . $ar['menu'] . "}{" . $action_item_name . "}";
        $obj->setDescription($newDescr);
        $this->saveAuthItem($obj);
    }

    /**
     * isTaskMenuItemChild
     *    detecta si un CAuthItem ($item) es un hijo de otro ($posibleSuperior)
     *  utiliza la sintaxis del atributo Description para detectarlo.
     *
     * @param CAuthItem $item
     * @param CAuthItem $posibleSuperior
     * @access public
     * @return void
     */
    public function isTaskMenuItemChild($item, $posibleSuperior)
    {
        return ($this->getTaskParentMenuName($item)
            == $posibleSuperior->getName());
    }


    /**
     * explodeTaskArray
     *    separa un array original de CAuthItem TASK en partes organizadas por
     *    tipo: Menu, MenuItems y Tareas Regulares.
     *
     *    ejemplo:
     *
     *        1. se le da una lista entera de todas las tareas del sistema
     *        usando $this->getTasks():
     *
     *        2. se invoca a este metodo
     *
     *        3. el metodo retorna un array con esta forma:
     *
     *            array(
     *                'topmenu'=>array(...),
     *                'childmenu'=>array('authitemname'=>array(), ... ),
     *                'regular'=>array(...)
     *            );
     *
     *        se ven 3 categorias (indices) de array aqui:
     *            topmenu
     *            childmenu
     *            regular
     *
     *        topmenu:     array de CAuthItem tipo TASK de todos aquellos
     *                     considerados Menu de 1er nivel al usar sintaxis.
     *
     *        childmenu:    array de arrays de CAuthItem indexado por el nombre del
     *                    CAuthItem padre del item usando la sintaxis de menu
     *        orphan:        array de CAuthItem de aquellos marcados con sintaxis
     *                    de sub menu item pero cuyo padre no existe.
     *
     *        regular:    array de CAuthItem de todas las tareas que no son menues.
     *
     *
     * @param array $originTaskList CAuthItem tipo TASK
     * @access public
     * @return array
     */
    public function explodeTaskArray($originTaskList)
    {

        $top = array(); // top menu
        $child = array(); // menuitems de alguien (index)
        $error = array(); // huerfanos
        $regular = array(); // tareas normales

        // detecta TOP menu tasks
        foreach ($originTaskList as $task) {
            if ($this->istaskTopMenuItem($task)) {
                $top[] = $task;
            }
        }

        // busca las tareas que son hijas de las primeras
        // halladas. (son hijas dada la sintaxis de descripcion del CAuthItem)
        //
        foreach ($top as $topmenuitem) {
            foreach ($originTaskList as $task) {
                if ($this->isTaskMenuItemChild($task, $topmenuitem)) {
                    $child[$topmenuitem->name][] = $task;
                }
            }
        }


        // agrega los huerfanos.
        // aquellas tareas marcadas como menuitems cuyo padre no existe
        //
        foreach ($originTaskList as $task) {
            if ($this->isTaskSubMenuItem($task)) {
                if (!$this->getParentMenuAuthItem($task)) {
                    $error[] = $task;
                }
            }
        }

        // agrega todas aquellas tareas que no son menuitems
        //
        foreach ($originTaskList as $task) {
            if (!$this->isTaskMenuItem($task)) {
                $regular[] = $task;
            }
        }

        return array(
            'topmenu' => $top,
            'childmenu' => $child,
            'orphan' => $error,
            'regular' => $regular
        );
    }

    /**
     * reorderItemArray
     *    ordernara un array de CAuthItem  de tipo TASK de modo que
     *    el array resultante este organizado asi:
     *
     *        MENU_ITEM_1
     *            SUB_MENU_ITEM {parent: MENU_ITEM_1}
     *            SUB_MENU_ITEM    "
     *            SUB_MENU_ITEM    "
     *        MENU_ITEM_2
     *            SUB_MENU_ITEM {parent: MENU_ITEM_2}
     *            SUB_MENU_ITEM    "
     *            SUB_MENU_ITEM    "
     *        NO_MENU_ITEM_1
     *        NO_MENU_ITEM_2
     *        NO_MENU_ITEM_3
     *        NO_MENU_ITEM_N
     *
     * @param mixed $itemArray array de CAuthItem
     * @access public
     * @return array de CAuthItem
     */
    public function reorderItemArray($itemArray)
    {

        $r = array();

        // busca aquellas operaciones que son tareas
        // y que son menu items, pero que no son sub menu items
        $r1 = array();
        foreach ($itemArray as $item) {
            if ($this->isTaskMenuItem($item)
                && !$this->isTaskSubMenuItem($item)
            ) {
                $r1[] = $item;
            }
        }

        // busca las tareas que son hijas de las primeras
        // halladas. (son hijas dada la sintaxis de descripcion del CAuthItem)
        //
        foreach ($r1 as $menuitem) {
            $r[] = $menuitem;
            foreach ($itemArray as $item) {
                if ($this->isTaskMenuItemChild($item, $menuitem)) {
                    $r[] = $item;
                }
            }
        }

        // agrega los huerfanos
        // aquellas tareas marcadas como menuitems cuyo padre no existe
        //
        foreach ($itemArray as $item) {
            if ($this->isTaskSubMenuItem($item)) {
                if (!$this->getParentMenuAuthItem($item)) {
                    $r[] = $item;
                }
            }
        }

        // agrega todas aquellas tareas que no son menuitems
        //
        foreach ($itemArray as $item) {
            if (!$this->isTaskMenuItem($item)) {
                $r[] = $item;
            }
        }

        return $r;
    }


    /**
     * getMenu
     *  devuelve un array indexado listo para usar en CMenu
     *      incluso con una entrada extra para subitems: 'items'
     *      para menues extendidos.
     *
     *    el array es obtenido usando la sintaxis de la descripcion.
     *
     * @access public
     * @param $arguments adjunta argumentos a la url, ej: array('abc'=>'123')
     * @return array indexado ('label','url' [,'items'=>array(...)])
     */
    public function getMenu($userid = -1, $arguments = array())
    {

        if ($userid == -1) {
            $userid = Yii::app()->user->id;
        }

        $r = array();

        // todas las TAREAS a las que puede acceder este usuario

        // este metodo no sirve porque solo lista elementos directamente
        // relacionados al userid y no lista aquellos derivados,
        //$itemArray = $this->getAuthItems(CAuthItem::TYPE_TASK,$userid);

        $tasklist = $this->tasks;

        // por tanto a lo anterior: listo todas las tareas de tipo menuitem
        // y pregunto si el usuario tiene acceso a ellas:
        $itemArray = array();
        foreach ($tasklist as $task) {
            if ($this->isTaskMenuItem($task) && !$this->isTaskSubMenuItem($task)) {
                if ($this->checkAccess($task->getName(), $userid)) {
                    $itemArray[] = $task;
                }
            }
        }

        // todas las tareas consideradas subitems, no importa
        // si estan asignadas al usuario
        //
        $allsubitems = array();
        foreach ($tasklist as $task) {
            if ($this->isTaskSubMenuItem($task)) {
                $allsubitems[] = $task;
            }
        }

        // Menues de Primer Nivel
        //
        // busca aquellas operaciones que son tareas
        // y que son menu items, pero que no son sub menu items
        $r1 = array();
        foreach ($itemArray as $item) {
            if ($this->isTaskMenuItem($item) && !$this->isTaskSubMenuItem($item)) {
                $r1[] = $item;
            }
        }

        // busca las tareas que son hijas de las primeras
        // halladas. (son hijas dada la sintaxis de descripcion del CAuthItem)
        //
        foreach ($r1 as $menuitem) {
            // child menu items
            $items = array();
            // agrega al menuitem de 1er nivel todas los subitems (tasks)
            // sin importar si fueron otorgadas al usuario con checkAccess
            //
            foreach ($allsubitems as $task) {
                if ($this->isTaskMenuItemChild($task, $menuitem)) {
                    $items[] = array(
                        'label' => $this->getTaskText($task),
                        'url' => $this->getTaskUrl($task, $arguments),
                    );
                }
            }
            // top level menu
            if (!sizeof($items)) {
                $items = null;
            }
            $r[] = array(
                'label' => $this->getTaskText($menuitem),
                'url' => '',
                'items' => $items,
            );
        }
        return $r;
    }


    /**
     * enumControllers
     *    lista los nombres de los controllers declarados.
     * @access public
     * @return array con nombre del controller
     */
    public function enumControllers()
    {
        if ($this->_enumcontrollers == null) {
            $this->_enumcontrollers = array();
            $p = Yii::app()->getControllerPath();
            foreach (scandir($p) as $f) {
                if ($f == '.' || $f == '..') {
                    continue;
                }
                if (strlen($f)) {
                    if ($f[0] == '.') {
                        continue;
                    }
                }
                if ($pos = strpos(strtolower($f), "controller.php")) {
                    $this->_enumcontrollers[] = substr($f, 0, $pos);
                }
            }
            return $this->_enumcontrollers;
        } else {
            return $this->_enumcontrollers;
        }
    }

    /**
     * enumActions
     *    devuelve un array con los nombres de los actions del controller
     * @param mixed $controllerName nombre del controller
     * @access public
     * @return array lista de actions.
     */
    public function enumActions($controllerName)
    {
        $this->_enumactions = array();
        $className = $controllerName . 'Controller';
        Yii::import('application.controllers.' . $className, true);
        $refx = new ReflectionClass($className);
        foreach ($refx->getMethods() as $method) {
            if ($method->name != 'actions') {
                if (substr($method->name, 0, 6) == "action") {
                    $this->_enumactions[] = substr($method->name, 6);
                }
            }
        }
        return $this->_enumactions;
    }

    /**
     * autoDetect
     *    lee todos los controllers y actions y los almacena si previamente
     *    no estaban registrados.
     * @access public
     * @return void
     */
    public function autoDetect()
    {

        // agrega cada actiond e cada controller detectado en codigo fuente
        //
        foreach ($this->enumControllers() as $c) {
            // cada controller
            $itemName = "controller_" . strtolower($c);
            if (!$this->getAuthItem($itemName)) {
                $this->createAuthItem(
                    $itemName,
                    CAuthItem::TYPE_OPERATION,
                    ""
                );
            }
            // cada action
            foreach ($this->enumActions($c) as $action) {
                $itemName = "action_" . strtolower($c) . "_" . strtolower($action);
                if (!$this->getAuthItem($itemName)) {
                    $this->createAuthItem(
                        $itemName,
                        CAuthItem::TYPE_OPERATION,
                        ""
                    );
                }

            }
        }

        $this->ensureMenuItemIntegrity();
    }


    /**
     * ensureMenuItemIntegrity
     *    se asegura que todas aquellas tareas que usan sintaxis de descripcion
     *    y que sean subitems de uno superior (debido a la sintaxis)
     *    se asegura que cada subitem este asignado como un child auth item a
     *  la tarea superior.
     *
     * @access public
     * @return void
     */
    public function ensureMenuItemIntegrity()
    {
        $data = $this->explodeTaskArray($this->getTasks());
        $submenues = $data['childmenu'];
        foreach ($submenues as $parentItemName => $tasks) {
            // determina si esta tarea esta asignada a su padre
            foreach ($tasks as $task) {
                if (!$this->hasItemChild($parentItemName, $task->name)) {
                    $this->addItemChild($parentItemName, $task->name);
                }
            }
        }
    }


}// finclase