UiController.php 44.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
<?php
/**
UiController

Controladora comun para todas las vistas predefinidas en views/ui

dependencias:

La controladora no accede de ninguna manera a la capa de CrugeFactory,
todo lo hace mediante los componentes:

Yii::app()->user->um
Yii::app()->user->rbac
Yii::app()->user->ui

igualmente, no hay instancias directas a modelos de datos, esto es para ayudar
a la insercion de diferentes ORDBM.

@author: Christian Salazar H. <christiansalazarh@gmail.com> @salazarchris74
@license protected/modules/cruge/LICENSE
 */
class UiController extends Controller
{
    public $basePath; // usada por CrugeUi::getResource()

    public function init()
    {
        $this->registerScripts();
        $this->layout = CrugeUtil::config()->generalUserManagementLayout;
    }

    public function registerScripts()
    {
        $this->basePath = Yii::app()->getAssetManager()->publish(
            dirname(__FILE__) . "/../resources"
        ) . "/";

        $cs = Yii::app()->getClientScript();

        $cs->registerCoreScript('jquery');

        $cs->registerCssFile($this->basePath . "estilos.css");
    }

    private function _publicActionsList()
    {
        return array(
            'captcha',
            'registration',
            'login',
            'logout',
            'pwdrec'
        ,
            'activationurl',
            'ajaxgeneratenewpassword',
            'welcome'
        );
    }

    public function filters()
    {
        return array_merge(
            array(
                // con accessControl se garantiza que un usuario NO autenticado NO tenga acceso
                // a las funciones NO públicas.
                'accessControl',
                // con CrugeUiAccessControlFilter se garantiza que a los actions no publicos
                // solo accedan aquellos usuarios que tengan la operacion asignada
                // directamente o mediante una tarea o un rol.
                //
                // al usar este filtro Y si la configuracion del modulo indica que:
                //
                //	SI rbacSetupEnabled es TRUE entonces:
                //
                //		1. 	el filtro "creará" si es necesario la operacion en base
                //			al controller/action
                //
                //		2.	el filtro concede paso si la operacion controller/action esta
                //		  	asignada al usuario.
                //
                //	SI rbacSetupEnabled es FALSE entonces:
                //
                //		1.	el filtro concede paso si la operacion controller/action esta
                //		  	asignada al usuario.
                //
                array('CrugeUiAccessControlFilter', 'publicActions' => self::_publicActionsList()),

            )
      //     ,parent::filters()		// esta linea causa problemas en una instalacion estandar de Cruge
        );
    }

    public function accessRules()
    {
        return array(
            array(
                'allow',
                'actions' => self::_publicActionsList(),
                'users' => array('*'),
            ),
            array(
                'allow',
                'users' => array('@'),
            ),
            array(
                'deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
        );
    }

    public function actionLogin()
    {

        $this->layout = CrugeUtil::config()->loginLayout;

        $model = Yii::app()->user->um->getNewCrugeLogon('login');

        // por ahora solo un metodo de autenticacion por vez es usado, aunque sea un array en config/main
        //
        $model->authMode = CrugeFactory::get()->getConfiguredAuthMethodName();

        Yii::app()->user->setFlash('loginflash', null);

        Yii::log(__CLASS__ . "\nactionLogin\n", "info");

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeLogon']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeLogon']];
            if ($model->validate()) {
                if ($model->login(false) == true) {

                    Yii::log(__CLASS__ . "\nCrugeLogon->login() returns true\n", "info");

                    // a modo de conocimiento, Yii::app()->user->returnUrl es
                    // establecida automaticamente por CAccessControlFilter cuando
                    // preFilter llama a accessDenied quien a su vez llama a
                    // CWebUser::loginRequired que es donde finalmente se llama a setReturnUrl
                    $this->redirect(Yii::app()->user->returnUrl);
                } else {
                    Yii::app()->user->setFlash('loginflash', Yii::app()->user->getLastError());
                }
            } else {
                Yii::log(
                    __CLASS__ . "\nCrugeUser->validate es false\n" . CHtml::errorSummary($model)
                    ,
                    "error"
                );
            }
        }
        $this->render('login', array('model' => $model));
    }

    public function actionPwdRec()
    {

        $this->layout = CrugeUtil::config()->loginLayout;

        $model = Yii::app()->user->um->getNewCrugeLogon('pwdrec');

        Yii::app()->user->setFlash('pwdrecflash', null);

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeLogon']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeLogon']];
            if ($model->validate()) {
                $newPwd = CrugeUtil::passwordGenerator();
                Yii::app()->user->um->changePassword($model->getModel(), $newPwd);
                Yii::app()->crugemailer->sendPasswordTo($model->getModel(), $newPwd);
                Yii::app()->user->um->save($model->getModel());

                Yii::app()->user->setFlash(
                    'pwdrecflash'
                    ,
                    CrugeTranslator::t('Una nueva clave ha sido enviada a su correo')
                );
            }
        }
        $this->render('pwdrec', array('model' => $model));
    }

    public function actionLogout()
    {
		// retorna false si ocurrio un error O si el filtro de sesion
		// dispone de onBeforeLogin el cual ha retornado false.
        if(Yii::app()->user->logout() == false){
			// se devuelve a la URL de donde vino
	        $this->redirect(Yii::app()->user->returnUrl);
			return;
		}else{
			
        	$this->redirect(Yii::app()->user->ui->loginurl);
		}
    }

    public function actionUserManagementAdmin()
    {
        $model = Yii::app()->user->um->getSearchModelICrugeStoredUser();
        $model->unsetAttributes();
        if (isset($_GET[CrugeUtil::config()->postNameMappings['CrugeStoredUser']])) {
            $model->attributes = $_GET[CrugeUtil::config()->postNameMappings['CrugeStoredUser']];
        }
        $dataProvider = $model->search();
        $this->render("usermanagementadmin", array('model' => $model, 'dataProvider' => $dataProvider));
    }

    public function actionEditProfile()
    {

        $this->layout = CrugeUtil::config()->editProfileLayout;

        if (!Yii::app()->user->isGuest) {
            $this->_editUserProfile(Yii::app()->user->user, false);
        } else {
            throw new CrugeException("necesita iniciar sesion para editar su perfil");
        }
    }

    public function actionUserManagementUpdate($id)
    {
        $this->_editUserProfile(Yii::app()->user->um->loadUserById($id), true);
    }

    public function _editUserProfile(ICrugeStoredUser $model, $boolIsUserManagement)
    {
        // carga los campos definidos por el administrador
        // trayendo consigo el atributo "value" accesible mediante $xx->fieldvalue
        Yii::app()->user->um->loadUserFields($model);
        $this->performAjaxValidation('crugestoreduser-form', $model);
        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']];
            if ($model->validate()) {
                // el modelo ICrugeStoredUser ha validado bien, incluso cada uno de sus campos extra

                /*
                    si se ha especificado algun valor en $model->newPassword se asume
                    que se quiere cambiar la clave:
                */
                $newPwd = trim($model->newPassword);
                Yii::log("deteccion de nueva clave: newPassword: [" . $newPwd . "]", "info");
                if ($newPwd != '') {
                    Yii::log("\n\n***NUEVA CLAVE***\n\n", "info");
                    Yii::app()->user->um->changePassword($model, $newPwd);
                    Yii::app()->crugemailer->sendPasswordTo($model, $newPwd);
                }

                if (Yii::app()->user->um->save($model, 'update')) {
                    if ($boolIsUserManagement == true) {
                        $this->redirect(array('usermanagementadmin'));
                    } else {
                        $this->redirect(array('usersaved', 'layout' => $this->layout));
                    }
                }
            }
        }
        $this->render(
            "usermanagementupdate",
            array(
                'model' => $model
            ,
                'boolIsUserManagement' => $boolIsUserManagement
            )
        );
    }

    /*
        solo se crea el ICrugeStoredUser, no todo el perfil.
    */
    public function actionUserManagementCreate()
    {
        $model = Yii::app()->user->um->createBlankUser();

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']];

            $model->terminosYCondiciones = true;

            $model->scenario = 'manualcreate';

            if ($model->validate()) {

                $newPwd = trim($model->newPassword);
                Yii::app()->user->um->changePassword($model, $newPwd);

                Yii::app()->user->um->generateAuthenticationKey($model);

                if (Yii::app()->user->um->save($model, 'insert')) {

                    $this->onNewUser($model, $newPwd);

                    $this->redirect(array('usermanagementadmin'));
                }
            }
        }
        $this->render("usermanagementcreate", array('model' => $model));
    }

    public function actionRegistration($datakey = '')
    {

        $this->layout = CrugeUtil::config()->registrationLayout;


        $model = Yii::app()->user->um->createBlankUser();
		$model->bypassCaptcha = false;
        $model->terminosYCondiciones = false;
        if (Yii::app()->user->um->getDefaultSystem()->getn('registerusingterms') == 0) {
            $model->terminosYCondiciones = true;
        }

        // para que cargue los campos del usuario
        Yii::app()->user->um->loadUserFields($model);

        // 'datakey' es el nombre de una variable de sesion
        // establecida por alguna parte que invoque a actionRegistration
        // y que se le pasa a este action para de ahi se lean datos.
        //
        // el dato esperado alli es un array indexado ('attribuye'=>'value')
        // tales valores deberan usarse para inicializar el formulario
        // del usuario como se indica aqui:
        //
        // ejemplo de array en sesion:
        //	array('username'=>'csalazar','email'=>'micorreo@x.com'
        //	,'nombre'=>'christian', 'apellido'=>'salazar')
        //
        // siendo: "nombre" y "apellido" los nombre de campos personalizados
        //	que inmediantamente tras registro seran inicializados.
        //
        if ($datakey != null) {
            // leo la data de la varibale de sesion
            $s = new CHttpSession();
            $s->open();
            $values = $s[$datakey];
            $s->close();
            // asumo que es un array, asi que aqui vamos
            //
            $model->username = $values['username'];
            $model->email = $values['email'];
            // ahora, procesa los campos personalizados,
            // rellenando aquellos mapeados contra los campos existentes:
            foreach ($model->getFields() as $f) {
                if (isset($values[$f->fieldname])) {
                    $f->setFieldValue($values[$f->fieldname]);
                }
            }
        }

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']];
            if ($model->validate()) {

                $newPwd = trim($model->newPassword);
                Yii::app()->user->um->changePassword($model, $newPwd);

                Yii::app()->user->um->generateAuthenticationKey($model);

                if (Yii::app()->user->um->save($model, 'insert')) {

                    $this->onNewUser($model, $newPwd);

                    $this->redirect(array('welcome'));
                }
            }
        }
        $this->render("registration", array('model' => $model));
    }

    /* este es un evento emitido por actionRegistration y actionUserManagementCreate
        el cual informa que un nuevo usuario ha sido creado.

        segun la configuracion general del sistema este usuario
        sera activado de inmediato, o por email, o manualmente.
    */
    private function onNewUser(ICrugeStoredUser $model, $newPwd = "")
    {
        Yii::log(__METHOD__ . "\n", "info");

        $opt = Yii::app()->user->um->getDefaultSystem()->getn("registerusingactivation");

        $role = Yii::app()->user->um->getDefaultSystem()->get("defaultroleforregistration");
        Yii::log(__METHOD__ . "\n role: " . $role, "info");
        if (Yii::app()->user->rbac->getAuthItem($role) != null) {
            Yii::log(
                __METHOD__ . "\n asignando role: " . $role . " a userid:"
                    . $model->getPrimaryKey(),
                "info"
            );
            Yii::app()->user->rbac->assign($role, $model->getPrimaryKey());
        }

        if ($opt == CRUGE_ACTIVATION_OPTION_INMEDIATE) {
            // lo activa inmediatamente y le manda la clave al usuario
            $model->state = CRUGEUSERSTATE_ACTIVATED;
            Yii::app()->user->um->save($model);
            Yii::app()->crugemailer->sendPasswordTo($model, $newPwd);
        }
        if ($opt == CRUGE_ACTIVATION_OPTION_EMAIL) {
            // queda en estado no activado, pero envia un email para que
            // el usuario lo active
            Yii::app()->crugemailer->sendRegistrationEmail($model, $newPwd);
        }
        if ($opt == CRUGE_ACTIVATION_OPTION_MANUAL) {
            // lo activa manualmente, envia un email de espera por activacion manual
            Yii::app()->crugemailer->sendWaitForActivation($model, $newPwd);
        }
    }


    public function actionWelcome()
    {
        $this->layout = CrugeUtil::config()->registrationLayout;
        $this->render("welcome");
    }

    public function actionUserSaved($layout = null)
    {
        if ($layout != null) {
            $this->layout = $layout;
        }
        $this->render("usersaved");
    }


    public function actionUserManagementDelete($id)
    {
        $model = Yii::app()->user->um->loadUserById($id);
        $model->scenario = 'delete';
        $model->deleteConfirmation = 0;

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']])) {
            if (isset($_POST['cancelar'])) {
                $this->redirect(array('usermanagementadmin'));
            }

            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeStoredUser']];
            if ($model->validate()) {
                if ($model->deleteConfirmation == 1) {
                    if ($model->delete()) {
                        $this->redirect(array('usermanagementadmin'));
                    }
                }
            } else {
                //error, no ha confirmado con la casilla
            }
        }

        $this->render("usermanagementdelete", array('model' => $model));
    }


    public function actionFieldsAdminList()
    {
        $model = Yii::app()->user->um->getSearchModelICrugeField();
        $model->unsetAttributes();
        if (isset($_GET[CrugeUtil::config()->postNameMappings['CrugeField']])) {
            $model->attributes = $_GET[CrugeUtil::config()->postNameMappings['CrugeField']];
        }
        $dataProvider = $model->search();
        $this->render("fieldsadminlist", array('model' => $model, 'dataProvider' => $dataProvider));
    }

    public function actionFieldsAdminUpdate($id)
    {
        $model = Yii::app()->user->um->loadFieldById($id);
		if($model != null){
			$this->_fieldAdminForm($model);
		}else
			throw new CrugeException("Identificador de campo es invalido");
    }

    public function actionFieldsAdminCreate()
    {
        $model = Yii::app()->user->um->createEmptyField();
		$this->_fieldAdminForm($model);
    }
	
	private function _fieldAdminForm($model){
        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeField']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeField']];
            if ($model->save()) {
                $this->redirect(array('fieldsadminlist'));
            }
        }
        $this->render("fieldsadminupdate", array('model' => $model));
	}

    public function actionFieldsAdminDelete($id)
    {
        $model = Yii::app()->user->um->loadFieldById($id);
        if ($model != null) {
            if (Yii::app()->request->isAjaxRequest) {
                $model->delete();
            }
        }
    }


    public function actionRbacListRoles()
    {
        Yii::app()->user->rbac->autoDetect();
        $dataProvider = Yii::app()->user->rbac->getDataProviderRoles();
        $this->render('rbaclistroles', array('dataProvider' => $dataProvider));
    }

    public function actionRbacListTasks()
    {
        Yii::app()->user->rbac->autoDetect();
        $dataProvider = Yii::app()->user->rbac->getDataProviderTasks();
        $this->render('rbaclisttasks', array('dataProvider' => $dataProvider));
    }

    public function actionRbacListOps($filter = '0')
    {
        Yii::app()->user->rbac->autoDetect();
        $dataProvider = Yii::app()->user->rbac->getDataProviderOperations(
            $filter
        );
        $this->render('rbaclistops', array('dataProvider' => $dataProvider));
    }


    /**
     * actionAjaxRbacItemDescr
     *    cambia la descripcion de un $itemname.
     *
     *    Cruge incorpora una sintaxis para la descripcion de los CAuthItem.
     *        esa descripcion incorpora entre otras cosas el "$action"
     *        que se usara para el $itemname que opera en modo de SubMenuItem.
     *
     *    Este action es invocado via ajax desde:
     *        view/ui/_listauthitems.php
     *
     *    Para saber mas consulta acerca del funcionamiento de la sintaxis
     *    de la descripcion de un CAuthItem.  Info en CrugeAuthItemManager.php
     * PARAMS VIA POST:
     * @param string $action  el nombre action del cual se adosara a la descr.
     * @param string $itemname el item cuya descripcion se alteara
     * @access public
     * @return void
     */
    public function actionAjaxRbacItemDescr()
    {
		$action = $_POST['action']; 
		$itemname = $_POST['itemname'];
        $item = Yii::app()->user->rbac->getAuthItem($itemname);
        Yii::app()->user->rbac->setTaskAction($item, $action);
        header("Content-type: application/json");
        echo CJSON::encode(array('description' => $item->getDescription()));
    }

    // aqui type es uno de los valores de
    // CAuthItem::TYPE_ROLE,CAuthItem::TYPE_TASK,CAuthItem::TYPE_OPERATION
    // parametro llamado 'extra' es usado y enviado por CrugeAuthManager
    // para indicar la creacion de una tarea enlazada a otra
    public function actionRbacAuthItemCreate($type)
    {

        $editor = new CrugeAuthItemEditor('insert');
        $editor->name = "";
        $editor->description = "";
        $editor->businessRule = "";
        $editor->categoria = Yii::app()->user->rbac->getAuthItemTypeName($type);
        $editor->isNewRecord = true;

        if (($type == CAuthItem::TYPE_TASK) && isset($_GET['extra'])) {
            if (strlen($_GET['extra']) > 0) {
                // enlaza esta tarea con otra superior.
                // aqui se esta usando el mecanism de sintaxis de descripcion
                // del authitem definido en la documentacion de CrugeAuthManager
                $editor->description = ":Item Label{" . $_GET['extra'] . "}";
            }
        }

        if (isset($_POST['CrugeAuthItemEditor'])) {
            if (isset($_POST['volver'])) {
                if ($type == CAuthItem::TYPE_ROLE) {
                    $this->redirect(array('rbaclistroles'));
                }
                if ($type == CAuthItem::TYPE_TASK) {
                    $this->redirect(array('rbaclisttasks'));
                }
                if ($type == CAuthItem::TYPE_OPERATION) {
                    $this->redirect(array('rbaclistops'));
                }
            }

            $editor->attributes = $_POST['CrugeAuthItemEditor'];
            if ($editor->validate()) {


                $newAi = Yii::app()->user->rbac->createAuthItem(
                    $editor->name,
                    $type,
                    $editor->description,
                    $editor->businessRule
                );


                // se va de vuelta a la lista de donde vino
                if ($type == CAuthItem::TYPE_ROLE) {
                    $this->redirect(array('rbaclistroles'));
                }
                if ($type == CAuthItem::TYPE_TASK) {
                    $this->redirect(array('rbaclisttasks'));
                }
                if ($type == CAuthItem::TYPE_OPERATION) {
                    $this->redirect(array('rbaclistops'));
                }
            }
        }
        $this->render('rbacauthitemcreate', array('model' => $editor));
    }


    public function actionRbacAuthItemUpdate($id)
    {

        $aiModel = Yii::app()->user->rbac->getAuthItem($id);
        if ($aiModel == null) {
            throw new CrugeException("el item de autenticacion senalado no existe");
        }

        $editor = new CrugeAuthItemEditor('update');
        $editor->isNewRecord = false;
        $editor->name = $aiModel->name;
        $editor->description = $aiModel->description;
        $editor->businessRule = $aiModel->bizRule;
        $editor->categoria = Yii::app()->user->rbac->getAuthItemTypeName($aiModel->type);

        if (isset($_POST['CrugeAuthItemEditor'])) {
            if (isset($_POST['volver'])) {
                if ($aiModel->type == CAuthItem::TYPE_ROLE) {
                    $this->redirect(array('rbaclistroles'));
                }
                if ($aiModel->type == CAuthItem::TYPE_TASK) {
                    $this->redirect(array('rbaclisttasks'));
                }
                if ($aiModel->type == CAuthItem::TYPE_OPERATION) {
                    $this->redirect(array('rbaclistops'));
                }
            }

            $editor->attributes = $_POST['CrugeAuthItemEditor'];
            if ($editor->validate()) {
                // la guarda de regreso al aiModel
                $oldName = $aiModel->name;
                $aiModel->name = $editor->name;
                $aiModel->description = $editor->description;
                $aiModel->bizRule = $editor->businessRule;
                Yii::app()->user->rbac->saveAuthItem($aiModel, $oldName);

                // se va de vuelta a la lista de donde vino
                if ($aiModel->type == CAuthItem::TYPE_ROLE) {
                    $this->redirect(array('rbaclistroles'));
                }
                if ($aiModel->type == CAuthItem::TYPE_TASK) {
                    $this->redirect(array('rbaclisttasks'));
                }
                if ($aiModel->type == CAuthItem::TYPE_OPERATION) {
                    $this->redirect(array('rbaclistops'));
                }
            }
        }

        $this->render('rbacauthitemupdate', array('model' => $editor));
    }

    public function actionRbacAuthItemDelete($id)
    {
        $aiModel = Yii::app()->user->rbac->getAuthItem($id);
        if ($aiModel == null) {
            throw new CrugeException("el item de autenticacion senalado no existe");
        }

        $editor = new CrugeAuthItemEditor('delete');
        $editor->deleteConfirmation = false;
        $editor->isNewRecord = false;
        $editor->name = $aiModel->name;
        $editor->description = $aiModel->description;
        $editor->businessRule = $aiModel->bizRule;
        $editor->categoria = Yii::app()->user->rbac->getAuthItemTypeName($aiModel->type);

        if (isset($_POST['CrugeAuthItemEditor'])) {
            if (isset($_POST['volver'])) {
                // nada
            } else {
                $editor->attributes = $_POST['CrugeAuthItemEditor'];
                if ($editor->validate()) {
                    // elimina el CAuthItem
                    if ($editor->deleteConfirmation == 1) {
                        Yii::app()->user->rbac->removeAuthItem($id);
                    }
                } else {
                    $this->render('rbacauthitemdelete', array('model' => $editor));
                    return;
                }
            }
            if ($aiModel->type == CAuthItem::TYPE_ROLE) {
                $this->redirect(array('rbaclistroles'));
            }
            if ($aiModel->type == CAuthItem::TYPE_TASK) {
                $this->redirect(array('rbaclisttasks'));
            }
            if ($aiModel->type == CAuthItem::TYPE_OPERATION) {
                $this->redirect(array('rbaclistops'));
            }
        }

        $this->render('rbacauthitemdelete', array('model' => $editor));
    }

    public function actionRbacAuthItemChildItems($id)
    {
        Yii::app()->user->rbac->autoDetect();
        $aiModel = Yii::app()->user->rbac->getAuthItem($id);
        if ($aiModel == null) {
            throw new CrugeException("el item de autenticacion senalado no existe");
        }
        $this->render('rbacauthitemchilditems', array('model' => $aiModel));
    }

    /**
    este action debe ser invocado bajo request POST, y su postdata debe contener
    un objeto JSON de esta forma:

    ejemplo:

    { parent: 'updateOwnPost' , child: 'updatePost' , setflag: true }

    esto indica que al 'parent' se le agregara o removera (setflag), el item 'child'

    cualquier error debe ser reportado bajo una excepcion.
     */
    public function actionRbacAjaxSetChildItem()
    {
        if (Yii::app()->request->isAjaxRequest) {
            if (Yii::app()->request->isPostRequest) {

                $rbac = Yii::app()->user->rbac;

                $jsondata = trim(file_get_contents('php://input'));

                Yii::log(__CLASS__ . "\nactionRbacAjaxSetChildItem\njsondata:\n" . $jsondata, "info");

                $obj = CJSON::decode($jsondata);
                $parent = $obj['parent'];
                $child = $obj['child'];
                $setflag = ($obj['setflag'] == 1 ? "true" : "false");

                Yii::log(
                    __CLASS__ . "\nactionRbacAjaxSetChildItem\natributos leidos:\n"
                        . "parent=" . $parent . "\n"
                        . "child=" . $child . "\n"
                        . "setflag='" . $setflag . "'\n"
                    ,
                    "info"
                );

                // parent y child deben existir
                $_parent = $rbac->getAuthItem($parent);
                $_child = $rbac->getAuthItem($child);
                if ($_parent == null) {
                    throw new CrugeException("parent: no existe.[" . $parent . "]");
                }
                if ($_child == null) {
                    throw new CrugeException("child: no existe.[" . $child . "]");
                }

                $accion = '...';
                if ($setflag == "true") {
                    $accion = '[try add..]';
                    if (!$rbac->hasItemChild($parent, $child)) {
                        $rbac->addItemChild($parent, $child);
                        $accion .= 'addItemChild';
                    }
                } else {
                    $accion = '[try remove..]';
                    if ($rbac->hasItemChild($parent, $child)) {
                        $rbac->removeItemChild($parent, $child);
                        $accion .= 'removeItemChild';
                    }
                }

                // OK
                Yii::log(__CLASS__ . "\nactionRbacAjaxSetChildItem\nRESULTADO OK {$accion}\n", "info");

                $result = array();
                $result['result'] = $rbac->hasItemChild($parent, $child);
                $result['parent'] = $parent;
                $result['child'] = $child;
                header("Content-type: application/json");
                echo CJSON::encode($result);
            } else {
                throw new CrugeException("por favor no invoque este action manualmente");
            }
        } else {
            throw new CrugeException("por favor no invoque este action manualmente");
        }
    }


    /* asigna o revoca un authitem a un usuario en particular. puede ser un rol, una tarea o una operacion, pero por razones de orden en la vista que llama a este action solo se habilitan roles para ser agregados.

    la contraparte de este action es:
    actionRbacUsersAssignments()  la cual maneja la vista que asigna/revoca un rol de forma masiva
    a varios usuarios.

        funciona via ajax y post, espera que el cuerpo del post traiga lo siguiente:
        { authitem: 'nombrerol' , userid: 123 , setflag: true }

        lo que hara con ese objeto json es:

        asignar (<setflag> es true=asignar) el <authitem>, al usuario con primaryKey <userid>,
        si no puede emite una excepcion
    */
    public function actionRbacAjaxAssignment()
    {
        if (Yii::app()->request->isAjaxRequest) {
            if (Yii::app()->request->isPostRequest) {
                $rbac = Yii::app()->user->rbac;
                $jsondata = trim(file_get_contents('php://input'));
                Yii::log(__CLASS__ . "\nactionRbacAjaxAssignment\njsondata:\n" . $jsondata, "info");

                $obj = CJSON::decode($jsondata);
                $authitemName = $obj['authitem'];
                $userId = $obj['userid'];
                $setflag = ($obj['setflag'] == 1 ? "true" : "false");

                Yii::log(
                    __CLASS__ . "\nactionRbacAjaxAssignment\natributos leidos:\n"
                        . "authitemName=" . $authitemName . "\n"
                        . "userId=" . $userId . "\n"
                        . "setflag='" . $setflag . "'\n"
                    ,
                    "info"
                );

                // comprueba que el authitem exista
                $_ai = $rbac->getAuthItem($authitemName);
                if ($_ai == null) {
                    throw new CrugeException("authitem: no existe.[" . $authitemName . "]");
                }

                // verifica al usuario
                $user = Yii::app()->user->um->loadUserById($userId);
                if ($user == null) {
                    throw new CrugeException("userId: no existe.[" . $userId . "]");
                }

                Yii::log(__CLASS__ . "\nactionRbacAjaxAssignment\nprocede a asignar o a revocar\n", "info");

                $accion = "";

                if ($setflag == 'true') {
                    // quiere asignar
                    if ($rbac->isAssigned($authitemName, $userId)) {
                        $accion .= "[ya estaba previamente asignado]";
                    } else {
                        if ($rbac->assign($authitemName, $userId)) {
                            $accion .= "[asignado exitosamente]";
                        } else {
                            Yii::log(
                                __CLASS__ . "\nactionRbacAjaxAssignment\n"
                                    . $accion . "\n[no se pudo asignar]\n",
                                "error"
                            );
                            throw new CrugeException("no se pudo asignar");
                        }
                    }
                } else {
                    // quiere revocar
                    if (!$rbac->isAssigned($authitemName, $userId)) {
                        $accion .= "[no estaba previamente asignado]";
                    } else {
                        if ($rbac->revoke($authitemName, $userId)) {
                            $accion .= "[revocado exitosamente]";
                        } else {
                            Yii::log(
                                __CLASS__ . "\nactionRbacAjaxAssignment\n"
                                    . $accion . "\n[no se pudo revocar]\n",
                                "error"
                            );
                            throw new CrugeException("no se pudo revocar");
                        }
                    }
                }

                Yii::log(
                    __CLASS__ . "\nactionRbacAjaxAssignment\n"
                        . $accion . "\n[operacion finalizada]\n",
                    "info"
                );

                $result = array();
                $result['result'] = $rbac->isAssigned($authitemName, $userId);
                header("Content-type: application/json");
                echo CJSON::encode($result);
            } else {
                throw new CrugeException("por favor no invoque este action manualmente");
            }
        } else {
            throw new CrugeException("por favor no invoque este action manualmente");
        }
    }


    public function actionRbacAjaxGetAssignmentBizRule()
    {
        if (Yii::app()->request->isAjaxRequest) {
            if (Yii::app()->request->isPostRequest) {
                $rbac = Yii::app()->user->rbac;
                $jsondata = trim(file_get_contents('php://input'));

                $obj = CJSON::decode($jsondata);
                $authitemName = $obj['authitem'];
                $userId = $obj['userid'];

                // comprueba que el authitem exista
                $_ai = $rbac->getAuthItem($authitemName);
                if ($_ai == null) {
                    throw new CrugeException("authitem: no existe.[" . $authitemName . "]");
                }

                // verifica al usuario
                $user = Yii::app()->user->um->loadUserById($userId);
                if ($user == null) {
                    throw new CrugeException("userId: no existe.[" . $userId . "]");
                }

                $aa = $rbac->getAuthAssignment($authitemName, $userId);
                if ($aa == null) {
                    throw new CrugeException("asignacion no hallada");
                }

                $result = array();
                $result['bz'] = $aa->bizRule;
                $result['obj'] = $aa;
                header("Content-type: application/json");
                echo CJSON::encode($result);
            } else {
                throw new CrugeException("por favor no invoque este action manualmente");
            }
        } else {
            throw new CrugeException("por favor no invoque este action manualmente");
        }
    }

    public function actionRbacAjaxSetAssignmentBizRule()
    {
        if (Yii::app()->request->isAjaxRequest) {
            if (Yii::app()->request->isPostRequest) {
                $rbac = Yii::app()->user->rbac;
                $jsondata = trim(file_get_contents('php://input'));

                $obj = CJSON::decode($jsondata);
                $authitemName = $obj['authitem'];
                $userId = $obj['userid'];
                $nuevoBz = $obj['bz']; // el business rule modificado

                // comprueba que el authitem exista
                $_ai = $rbac->getAuthItem($authitemName);
                if ($_ai == null) {
                    throw new CrugeException("authitem: no existe.[" . $authitemName . "]");
                }

                // verifica al usuario
                $user = Yii::app()->user->um->loadUserById($userId);
                if ($user == null) {
                    throw new CrugeException("userId: no existe.[" . $userId . "]");
                }

                $aa = $rbac->getAuthAssignment($authitemName, $userId);
                if ($aa == null) {
                    throw new CrugeException("asignacion no hallada");
                }

                $aa->bizRule = $nuevoBz;
                $rbac->saveAuthAssignment($aa);

                $result = array();
                $result['bz'] = $aa->bizRule;
                $result['obj'] = $aa;
                header("Content-type: application/json");
                echo CJSON::encode($result);
            } else {
                throw new CrugeException("por favor no invoque este action manualmente");
            }
        } else {
            throw new CrugeException("por favor no invoque este action manualmente");
        }
    }

    /**
    maneja la asignacion masiva de usuarios a roles.  su contraparte es el action:
    actionRbacAjaxAssignment.

    como funciona este action:

    La vista 'rbacusersassignments.php' tiene dos CGridView, cada uno llenado
    con los DataProviders que aqui se emiten. El primer es la lista de usuarios
    asignados a un rol, el segundo es la lista completa de usuarios.

    por eso en la llamada cruda a la vista se entregan dos data providers,
    ,array(
    'roleUsersDataProvider'=>$roleUsersDataProvider,
    'allUsersDataProvider'=>$allUsersDataProvider,
    )

    una vez en la vista se muestra una -lista de roles- a la cual al hacerle click en un
    rol se actualizara al CGridView que presenta al dataprovider: roleUsersDataProvider,
    mientras que el segundo CGridView permanece inalterado.

    el CGridView que aloja a roleUsersDataProvider sera actualizado cada vez que se haga
    click en un rol, eso generara una llamada con modalidad ajaxRequest a este action,
    y aqui se recibira entonces un argumento extra llamado: itemName

    // invocado cuando se hace click en un ROL (itemName)
    $.fn.yiiGridView.update('_lista1',{ data : "itemName="+itemName });

    cuando un usuario hace click en el link "#asignarSeleccion", lo que se hace
    basicamente es volver a invocar a $.fn.yiiGridView.update('_lista1',{ data : "itemName="+itemName });,  pero con mas argumentos:

    mode	: puede ser 'assign' : 'revoke'
    userid	: lista de userid

    por esta razon se ve que se hace:
    if(Yii::app()->request->isAjaxRequest && isset($_GET['mode']))

    para poder reconocer el ajaxRequest de:
    $.fn.yiiGridView.update('_lista1',{ data : "itemName="+itemName });
    del otro que trae los argumentos extra.

    cuando se detecta que el argumento MODE viene en la URL entonces se sabe
    que se quiere asignar o revocar un permiso (itemName) a una lista de usuarios (userid).

     */
    public function actionRbacUsersAssignments()
    {

        $pageSize = 20;
        $rbac = Yii::app()->user->rbac;
        $um = Yii::app()->user->um;

        $debug = "";
        if (Yii::app()->request->isAjaxRequest && isset($_GET['mode'])) {

            $debug .= "AJAX REQUEST EN CURSO CON INDICACION DE ASIGNAR O REVOCAR\n";
            $mode = $_GET['mode'];
            $itemName = $_GET['itemName'];
            $userid = isset($_GET['userid']) ? $_GET['userid'] : '0';
            $ids = explode(",", $userid);

            $debug .= "-itemName es: {$itemName}\n";
            $debug .= "-modalidad es: {$mode}\n";
            $debug .= "-userid es: {$userid}\n";

            foreach ($ids as $uid) {
                $debug .= "OPERANDO '{$mode}' {$uid} AL AUTHITEM: {$itemName}\n";
                if ($mode == 'assign') {
                    if (!$rbac->isAssigned($itemName, $uid)) {
                        $rbac->assign($itemName, $uid);
                    }
                } else {
                    if ($mode == 'revoke') {
                        if ($rbac->isAssigned($itemName, $uid)) {
                            $rbac->revoke($itemName, $uid);
                        }
                    }
                }
            }
        }


        // entrega los usuarios asignados a este CAuthItem
        //
        $authItemName = "";
        if (isset($_GET['itemName'])) {
            $authItemName = $_GET['itemName'];
        }

        $debug .= "arg: itemName={$authItemName}\n";

        Yii::log(__CLASS__ . "\nactionRbacUsersAssignments\ndebug:\n" . $debug, "info");

		$boolLoadCustomFields = true;

        $roleUsersDataProvider = $um->listUsersDataProviderFromArray(
            $rbac->getUsersAssigned($authItemName),
            $pageSize, $boolLoadCustomFields
        );
        $allUsersDataProvider = $um->listAllUsersDataProvider(
			array(), $pageSize, $boolLoadCustomFields);

        $this->render(
            'rbacusersassignments'
            ,
            array(
                'roleUsersDataProvider' => $roleUsersDataProvider,
                'allUsersDataProvider' => $allUsersDataProvider,
            )
        );
    }

    public function actionSessionAdmin()
    {
        $model = Yii::app()->user->um->getSearchModelICrugeSession();
        $model->unsetAttributes();
        if (isset($_GET[CrugeUtil::config()->postNameMappings['CrugeSession']])) {
            $model->attributes = $_GET[CrugeUtil::config()->postNameMappings['CrugeSession']];
        }
        $dataProvider = $model->search();
        $this->render("sessionadmin", array('model' => $model, 'dataProvider' => $dataProvider));
    }

    public function actionSessionAdminDelete($id)
    {
        if (Yii::app()->request->isAjaxRequest) {
            $model = Yii::app()->user->um->loadSessionById($id);
            if ($model != null) {
                $model->delete();
            }
        }
    }

    public function actionSystemUpdate()
    {
        $model = Yii::app()->user->um->getDefaultSystem();

        Yii::app()->user->setFlash('systemFormFlash', null);

        if (isset($_POST[CrugeUtil::config()->postNameMappings['CrugeSystem']])) {
            $model->attributes = $_POST[CrugeUtil::config()->postNameMappings['CrugeSystem']];
            if ($model->validate()) {
                if ($model->save()) {
                    Yii::app()->user->setFlash(
                        'systemFormFlash'
                        ,
                        CrugeTranslator::t("Los datos del sistema han sido actualizados.")
                    );
                }
            }
        }

        $this->render('systemupdate', array('model' => $model));
    }

    /*
        este action debera tener acceso directo desde la calle para activar una cuenta.

        en el caso de este action, quiza no se quiera presentar el mismo layout que
        los demas actions, en este caso se puede tomar un valor de config del modulo
        para indicar que layout se quiere usar.
    */
    public function actionActivationUrl($key)
    {

        $this->layout = CrugeUtil::config()->activateAccountLayout;

        $model = Yii::app()->user->um->loadUserByKey($key);
        if ($model != null) {
            if ($model->state == CRUGEUSERSTATE_NOTACTIVATED) {

                $resp = CrugeTranslator::t("disculpe, no se pudo activar su cuenta");

                Yii::app()->user->um->activateAccount($model);

                if (Yii::app()->user->um->save($model)) {
                    $resp = CrugeTranslator::t(
                        "su cuenta ha sido activada, ahora debe iniciar sesion con las credenciales otorgadas"
                    );
                }

                $this->renderText($resp);
            }
        }
    }

    public function actionAjaxResendRegistrationEmail($id)
    {
        $newPassword = CrugeUtil::passwordGenerator();
        $model = Yii::app()->user->um->loadUserById($id);
        if ($model != null) {
            Yii::app()->user->um->changePassword($model, $newPassword);
            Yii::app()->user->um->generateAuthenticationKey($model);
            Yii::app()->user->um->save($model);
            Yii::app()->crugemailer->sendRegistrationEmail($model, $newPassword);
            echo CrugeTranslator::t("correo enviado");
        } else {
            echo CrugeTranslator::t("usuario no hallado");
        }
    }

    public function actionAjaxGenerateNewPassword()
    {
        echo CrugeUtil::passwordGenerator();
    }

    protected function performAjaxValidation($formid, $model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === $formid) {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}