Search code examples
javascriptc#visual-studio-lightswitchlightswitch-2013

adding data based on users login credentials (Lightswitch HTML)


I've been doing some research into how I can add data based on the login credentials. as an example scenario lets say I want a user to login to the application and then based on there login, add there hours they have done for that day, so like a timesheet application.

  • I don't want the user that is logged in to see any other names other than there own.
  • the browse screen would show only there times they have submitted rather than everyones aswell.

when using the insert call method in (_customnameDataService.cs) you can add in a username by associating a field within a table like below:

entity.Username =  Application.User.Name

so if this is possible there must be a way of calling this in JavaScript when logging in so any help or pointers would be great help. Adding a DataItem and displaying the username would be most preferable. (using edit render code) then from this I can pass it through the hierarchy and display only the information associated with the logged in user.


Solution

  • follow these steps to achieve the above question:

    1. Google GetUserName.ashx to get the code for this file to add to your Lightswitch HTML Project.
    2. copy the below function into the javascript file (in my case a Browse screen for my users)
    function CallGetUserName(operation) {
        $.ajax({
            type: 'post',
            data: {},
            url: '../web/GetUserName.ashx',
            success: operation.code(function AjaxSuccess(AjaxResult) {
                operation.complete(AjaxResult);
            })
        });
    }
    
    1. For the users that can login in and access the Lightswitch Application, the user information must be stored somewhere, in my case "tbl_Users". This table has a row called username. Using the below code this enables an administrator or someone high up in the hierarchy to access all the users, and also the specific user referenced in the table to access themselves.

    myapp.BrowseUsers.username_postRender = function (element, contentItem) {

    msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) {
    
        if (PromiseResult == 'TestUser' || PromiseResult == 'administrator') {
    
        } else {
            contentItem.value = PromiseResult;
        }
    
    });
    

    };

    What is actually happening?

    The function is calling the GetUserName.ashx file, which in turn retrieves the current user logged in. (from the aspnet_Users table which is automatically created) I have used a foreign key to associated my table (tbl_Users) and aspnet_Users together.

    in the debug or release environment if you were to add a data item (string) and display this information, it would return ("TestUser")

    myapp.BrowseUsers.displayUsername_postRender = function (element, contentItem) {

    msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) 
        {
           element.innerText = PromiseResult;
        }); };